请编一个程序,只需输入一个边长数n,就会出现两个螺旋矩形,例如,若输入6,则出现:
13 14 15 16 17 18
12 5 6 7 8 9
11 4 1 2 3 10
10 3 2 1 4 11
9 8 7 6 5 12
18 17 16 15 14 13
谢了。
[此贴子已经被作者于2004-08-06 17:04:22编辑过]
程序:
#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("\nDo you want to continue?(y/n)\n"); key=getche(); } }
void come() { int i=0,j=0; printf("\nInput the N.(the real length will be 2*N)\n"); scanf("%d",&n); n*=2; 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=0,base2=0,mm=0,mn=0,re=0; mx=min(x,n-x-1); my=min(y,n-y-1); {int nm=0,m=0,k=0; nm=min(mx,my); k=(n-2*nm); base1=k*k/2; base2=base1-(n-1-2*nm); }
if(x==y) re=base1; else if(mx==my) re=base2; else if(my<mx) { if(my==y) re=base1-(x-my); else re=base1-(n-1-my-x); } else //(mx<my) { if(mx==x) re=base2-(n-1-mx-y); else re=base2-(y-mx); } return (re); } (经测试)
只要算好行列的位置就可以了,这个不难吧