请问怎样编程打印魔方阵?

hohoxuhao115 2003-04-24 09:22:06
我们课本上有一到作业,是让编程打印魔方阵,我想了半天也没有想出个头绪,特请教大家:魔方阵想来大家都知道,就是一各方阵,各个对角线,行,列上的元素之和相等。请问这个程序怎样编?
...全文
165 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
linchwu 2003-04-25
  • 打赏
  • 举报
回复
好像还是不能用于偶数吧?
hohoxuhao115 2003-04-25
  • 打赏
  • 举报
回复
怎样用数组搞定?
Soledad 2003-04-24
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <iomanip.h> //使用其中的setw()设置输出数据的宽度
const int MAX_LEN=17+1;
static int fill_magic(int magic[][MAX_LEN],int magic_len);
static int print_magic(int magic[][MAX_LEN],int magic_len);
int main()
{
int magic[MAX_LEN][MAX_LEN];
int magic_len;
do {
cout<<"Please intput and odd positive integer";
cout<<"(<="<<(MAX_LEN-1)<<"):";
cin>>magic_len;
} while((magic_len<=0)||(magic_len>=MAX_LEN)||(magic_len%2==0));
fill_magic(magic,magic_len);
print_magic(magic,magic_len);
return 0;
}
int fill_magic(int magic[][MAX_LEN],int magic_len)
{
int I,j,row,col,count;
count=1;
row=1;
col=(magic_len+1)/2; //第1个数排在第一行的中间列上
for(I=1;I<=magic_len;I++)
{
for(j=1;j<=magic_len;j++)
{
magic[row][col]=count; //填入当前数字
count ++; //计数加1
if(--row<1) row=magic_len;//循环找行
if(++col>magic_len) col=1; //循环找列
}
row +=2;
if(row>magic_len) row-=magic_len;//循环找行
col--;
if(col<1) col+=magic_len; //循环找列
}
return 1;
}
int print_magic(int magic[][MAX_LEN],int magic_len)
{
int row,col;
cout<<"Find"<<magic_len<<"*"<<magic_len<<"magic matrix:\n";
for(col=1;col<=4*magic_len+1;col++) cout<<"-";
cout<<"\n";
for(row=1;row<=magic_len;row++){
for(col=1;col<=magic_len;col++){
cout<<setw(4)<<magic[row][col];
}
cout<<"\n";
}
for(col=1;col<=4*magic_len+1;col++) cout<<"-";
cout<<"\n";
return 1;
}


c++实现
shishiXP 2003-04-24
  • 打赏
  • 举报
回复
乖乖,这么长
lifanxi 2003-04-24
  • 打赏
  • 举报
回复
我的算法只能对付奇数,不过我想用来对付书上的习题算是可以了。
偶数的幻方不是任何阶都有解的。
fangjinhuo 2003-04-24
  • 打赏
  • 举报
回复
这只能按奇数,偶数来算,好象偶数中还要分
linchwu 2003-04-24
  • 打赏
  • 举报
回复
第二楼的好像只可以是 奇数的吧.
linchwu 2003-04-24
  • 打赏
  • 举报
回复
//#pragma hdrstop
//#pragma argsused
#include <iostream>
#include <list>
//#include <cstdlib>
using namespace std;
#define MSIZE 100
int temp1[MSIZE+1][MSIZE+1];
int temp2[MSIZE+1][MSIZE+1];
int temp3[MSIZE+1][MSIZE+1];
int temp4[MSIZE+1][MSIZE+1];
int Magic[MSIZE+1][MSIZE+1];
//****************************
int mmod(int a,int b)
{
if(a>=0)
return a%b;
else
return b-(-a%b);
}
//**********************
void Swap(int& a,int& b)
{
int t=a;
a=b;
b=t;
}
//***********************
void SetMagic(int size)
{
int i,j;
if(size%2==1)
{
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
{
temp1[i][j]=i;
temp2[i][j]=j;
}
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
{
temp3[i][j]=mmod(temp1[i][j]+temp2[i][j]-(size+3)/2,size);
temp4[i][j]=mmod(temp1[i][j]+2*temp2[i][j]-2,size);
}
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
Magic[i][j]=size*temp3[i][j]+temp4[i][j]+1;
}
else if(size%4==0)
{
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
{
temp1[i][j]=i;
temp2[i][j]=j;
}
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
temp3[i][j]=(temp1[i][j]%4)/2==(temp2[i][j]%4)/2?1:0;
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
temp4[i][j]=size*(i-1)+j;
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
if(temp3[i][j]==1)
Magic[i][j]=size*size+1-temp4[i][j];
else
Magic[i][j]=temp4[i][j];
}
else
{
int p=size/2;
SetMagic(p);
for(i=1;i<=size;i++)
for(j=1;j<=size;j++)
if(i<=p&&j<=p)
temp1[i][j]=Magic[i][j];
else if(i<=p&&j>p)
temp1[i][j]=Magic[i][j-p]+2*p*p;
else if(i>p&&j<=p)
temp1[i][j]=Magic[i-p][j]+3*p*p;
else
temp1[i][j]=Magic[i-p][j-p]+p*p;
int k=(size-2)/4;
list<int> lj;
for(i=1;i<=k;i++)
lj.push_back(i);
if(k==2)
{
lj.push_back(size);
}
else if(k>2)
{
lj.push_back(size-k+2);
lj.push_back(size);
}
else
{//do nothing
}
for(list<int>::iterator itrj=lj.begin();itrj!=lj.end();itrj++)
for(i=1;i<=p;i++)
Swap(temp1[i][*itrj],temp1[p+i][*itrj]);
lj.clear();
lj.push_back(1);
lj.push_back(k+1);
for(itrj=lj.begin();itrj!=lj.end();itrj++)
Swap(temp1[k+1][*itrj],temp1[k+1+p][*itrj]);
memcpy(Magic,temp1,(MSIZE+1)*(MSIZE+1)*sizeof(int));
}
}
//***********************************
/*void prin()
{
printf("func: %sn",__FUNC__);
}*/
void main()
{
int n;
cout<<"Please input a integer:";
cin>>n;
if(n==2||n<1)
{
cout<<"The magic square of size "<<n<<" does not exist."<<endl;
return;
}
SetMagic(n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout.width(4);
cout<<Magic[i][j]<<" ";
}
cout<<endl;
}
// prin();
// cout<<endl;
// cin>>n;
return;
}
要记得给我分啊.!:)
我没有分了.
lifanxi 2003-04-24
  • 打赏
  • 举报
回复
我的程序:
可参考http://expert.csdn.net/Expert/topic/1401/1401764.xml?temp=.5990412
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
int n;
cin >> n;
int *p = new int [n * n];
for (int t = 0; t < n * n; t++)
p[t] = 0;
int x, y;//记录当前填写的位置。
x = (n - 1) / 2;
y = 0;
p[y * n + x] = 1;//在第一行中间填上1。
for (int i = 2; i <= n * n; i++)
{
x++;//当前格移到右一列
if (x >= n)
x = 0; //移出格就到第一列
y--;//当前格到上一行
if (y < 0)
y = n - 1;//移出格就到最后一行
if (p[y * n + x] != 0)
{
y++;//如果当前格被占用就到下一行
}
p[y * n + x] = i;//当前格填入值
}
//下面是输出
for (int yy = 0; yy < n; yy++)
{
for (int xx = 0; xx < n; xx++)
{
cout << setw(4) << p[yy * n + xx] << " ";
}
cout << endl;
}
delete [] p;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧