第一篇:C语言程序编程:输入奇数,输出n阶幻方矩阵
#include
void huanFang(int n){
int a[MAX][MAX]={0};//初始化数组都为0 int i,j;int m,k;//当前位置 int p,q;//下一个位置 int data=0;m=0;k=n/2;while(data q=k+1;//右 if(p<0&&q //printf(“qian shang chu: p=%d,q=%dn”,p,q); p=n-1;//下边放 //printf(“hou shang chu: p=%d,q=%dn”,p,q);}else if(p>=0&&p //printf(“qian youchu: p=%d,q=%dn”,p,q); q=0;//左边放 //printf(“hou youchu: p=%d,q=%dn”,p,q);}else if(p<0&&q==n){//斜出框 //printf(“qian xiechu: p=%d,q=%dn”,p,q); p=m+1;//下格填 q=k; //printf(“hou xiechu: p=%d,q=%dn”,p,q);} if(a[p][q]!=0){//排重 //printf(“qian chongpai: p=%d,q=%dn”,p,q); p=m+1;//下格填 q=k; //printf(“hou chongpai: p=%d,q=%dn”,p,q);} m=p;k=q;} for(i=0;i printf(“%d ”,a[i][j]); } printf(“n”);} } void main(){ int n;//判断是否输入的是奇数 while(1){ printf(“please input n jie,n is oddn”); scanf(“%d”,&n); if(n%2==1) break;} huanFang(n);}