标题:[讨论]方阵问题(蛮有难度噢)
只看楼主
我是拉登
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-7-20
 问题点数:0 回复次数:6 
[讨论]方阵问题(蛮有难度噢)

【题目】方阵填数:在一个N×N的方阵中,填入1,2,.....N×N个数,并要求

构成如下的格式:

如 N=4 N=5

10 11 12 1 13 14 15 16 1

9 16 13 2 12 23 24 17 2

8 15 14 3 11 22 25 18 3

7 6 5 4 10 21 20 19 4

9 8 7 6 5

请用WIN-TC或C遍一个程序把方阵给编出来。

[此贴子已经被作者于2004-08-05 12:09:07编辑过]

搜索更多相关主题的帖子: 方阵 难度 
2004-08-05 12:08
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
晕,又是矩阵的问题~~~乌鸦和live41已经讨论得不亦乐乎了。具体方法在C语言教室里面有,很详细。

淘宝杜琨
2004-08-05 18:00
chengstone
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:562
专家分:226
注 册:2004-4-3
得分:0 

qq:69558139
2004-08-05 19:00
我是拉登
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-7-20
得分:0 

我编了出来,但就是有问题,能给小弟指明指明吗?

#include "stdio.h" #define N 100 main() {int i,j,k,s,j1,j2,j3,j4,a[N][N],n; printf("please imput a number:"); scanf("%d",&n); if(n%2==1){k=(n+1)/2;a[k][k]=1 for(s=1;s<=k-1;s++) a[k-s][k+s]=(1+2*s)*(1+2*s); {for(j1=1;j1<=2*s;j1++) a[k-s+j1][k+s]=a[k-s][k+s]-j1; for(j2=1;j2<=2*s;j2++) a[k-s+j1][k+s-j2]=a[k-s+j1][k+s]-j2; for(j3=1;j3<=2*s;j3++) a[k-s+j1-j3][k+s-j2]=a[k-s+j1][k+s-j2]-j3; for(j4=1;j4<=2*s-1;j4++) a[k-s+j1-j3][k+s-j2+j4]=a[k-s+j1-j3][k+s-j2]-j4; } } else{k=n/2; a[k][k+1]=4; for(s=1;s<=k-1;s++) {a[k-s][k+s]=(2+2*s)*(2+2*s); for(j1=1;j1<=2*s-1;j1++) a[k-s+j1][k+s]=a[k-s][k+s]-j1; for(j2=1;j2<=2*s-1;j2++) a[k-s+j1][k+s-j2]=a[k-s+j1][k+s]-j2; for(j3=1;j3<=2*s-1;j3++) a[k-s+j1-j3][k+s-j2]=a[k-s+j1][k+s-j2]-j3; for(j4=1;j4<=2*s-2;j4++) a[k-s+j1-j3][k+s-j2+j4]=a[k-s+j1-j3][k+s-j2]-j4; } } for(i=1;i<=n;i++) {for(j=1;j<=n;j++) printf("%3d ",a[i][j]); printf("\n\n"); } getch(); }


2004-08-06 09:36
我是拉登
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2004-7-20
得分:0 

不好意思,上面的的确有问题,正确的答案在这里:

#include "stdio.h" #define N 100 main() {int i,j,k,s,j1,j2,j3,j4,a[N][N],n; printf("please imput a number:"); scanf("%d",&n); if(n%2==1){k=(n+1)/2;a[k][k]=1; for(s=1;s<=k-1;s++) {a[k-s][k+s]=(1+2*s)*(1+2*s); for(j1=1;j1<=2*s;j1++) a[k-s+j1][k+s]=a[k-s][k+s]-j1; for(j2=1;j2<=2*s;j2++) a[k+s][k+s-j2]=a[k+s][k+s]-j2; for(j3=1;j3<=2*s;j3++) a[k+s-j3][k-s]=a[k+s][k-s]-j3; for(j4=1;j4<=2*s-1;j4++) a[k-s][k-s+j4]=a[k-s][k-s]-j4; } } else{k=n/2; for(s=1;s<=k;s++) {a[k-s+1][k+s]=4*s*s; for(j1=1;j1<=2*s-1;j1++) a[k-s+1+j1][k+s]=a[k-s+1][k+s]-j1; for(j2=1;j2<=2*s-1;j2++) a[k+s][k+s-j2]=a[k+s][k+s]-j2; for(j3=1;j3<=2*s-1;j3++) a[k+s-j3][k-s+1]=a[k+s][k-s+1]-j3; for(j4=1;j4<=2*s-2;j4++) 4a[k-s+1][k-s+1+j4]=a[k-s+1][k-s+1]-j4; } } for(i=1;i<=n;i++) {for(j=1;j<=n;j++) printf("%3d ",a[i][j]); printf("\n\n"); } getch(); } 若有更简单的方法,请不烦传上来。谢了。


2004-08-06 16:51
proton
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2004-7-30
得分:0 

哈哈哈哈...我的不用数组:(此为完整版)

#include <stdio.h> #include <conio.h> char key='y'; int n=0;

unsigned long complete(int x,int y); void come(); unsigned long min(int d,int f);

main() { while(key=='y'||key=='Y') {come(); printf("Do you want to continue?(y/n)\n"); key=getche(); } }

void come() { int i=0,j=0; printf("Input the N.\n"); scanf("%d",&n); printf("\n"); for(i=0;i<n;i++) {for(j=0;j<n;j++) {printf("%d\t",complete(j,i));} printf("\n"); } }

unsigned long min(int d,int f) { unsigned long c; c=d>f?f:d; return (c); }

unsigned long complete(int x,int y) { unsigned long mx=0,my=0,base1=n,base2=n,mm=0,mn=0,re=0; mx=min(x,n-x-1); my=min(y,n-y-1); {int nm=0,m=0,i=0; nm=min(mx,my); m=2*nm-1; while(i<=m) { base1+=2*(n-i-1); i++; } base2=base1+2*(n-i-1); }

if(x==y) { if(mx==x) re=base2; else re=base1; } else if(my<mx) { if(my==y) re=base2+x-my; else re=base1+(n-my-1)-x; } else //(mx<=my) 1 { if(mx==x) re=base2-(y-mx); else re=base1-((n-1-mx)-y); } return (re); }


Never was and never will be
2004-08-07 10:11
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
得分:0 

哎~ 这种题都见过n多遍了,不过惭愧的是到现在为止我还没有编出正确的程序

2004-08-12 15:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-3905-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.175484 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved