题解 写错了,是EOJ 2979
题解
using namespace std; const int dx[4]={0,1,0,-1}; const int dy[4]={1,0,-1,0}; int T,n,tot,d; int tu[20][20];
int isout(int xx) { if(xx<1 || xx>n) return 1; else return 0; } int can_write(int xx,int yy) { if(!isout(xx) && !isout(yy) && tu[xx][yy]==0) return 1; else return 0; } void write(int& xx,int& yy) { tu[xx][yy]=++tot; if(tot==n*n) return;
int fx=xx+dx[d],fy=yy+dy[d]; while(!can_write(fx,fy)) { d=(d+1)%4; fx=xx+dx[d],fy=yy+dy[d]; } xx=fx;yy=fy;return; } int main() { cin>>T; for(int step=0;step>n; int xx=1,yy=1; d=0; tot=0; memset(tu,0,sizeof(tu));
for(int i=1;i<=n*n;i++) write(xx,yy); printf("case #%d:\n",step); for(int i=1;i<=n;i++) { cout<<tu[i][1]; for(int j=2;j<=n;j++) cout<<' '<<tu[i][j]; cout<<endl; }
} return 0; }
直接模拟,姊妹题:EOJ 2983
重写基本题目
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[101][101]; memset(a, 0, sizeof(a)); int c = 1, x = 0, y = 0; while(c < n * n) { while(x < n - 1 && a[y][x + 1] == 0) a[y][x++] = c++; while(y < n - 1 && a[y + 1][x] == 0) a[y++][x] = c++; while(x > 0 && a[y][x - 1] == 0) a[y][x--] = c++; while(y > 0 && a[y - 1][x] == 0) a[y--][x] = c++; } a[y][x] = c; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) cout << a[i][j] << ' '; cout << endl; } return; } int main() { int T; cin >> T; for(int i = 0; i < T; i++) { printf("case #%d:\n", i); solve(); } return 0; }
c期中考试题
int main() { int x; /二维数组的第一个下标/ int y; /二维数组的第二个下标/ int n; /创建的矩阵为n*n/ int count = 1; /蛇形矩阵由1开始计数/ int round; /矩阵转的圈数/
int t,i; scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d",&n); count=1; int (*a)[n] = calloc(n * n,sizeof(int)); //如果n = 1,则直接输出 if(n == 1) { a[0][0] = count; } else { for(round = 0;round <= n / 2;round++) { x = round; for(y = round;y < n - round;y++) { a[x][y] = count; count++; } y = n - round - 1; for(x = round + 1;x < n - round - 1;x++) { a[x][y] = count; count++; } x = n - round - 1; for(y = n - round - 1;y >= round;y--) { a[x][y] = count; count++; } y = round; for(x = n - round - 2;x > round;x--) { a[x][y] = count; count++; } } if(n % 2 == 1) { a[n / 2][n / 2] = n*n; //n为奇数时,“最中间的”值 } } printf("case #%d:\n",i); for(x = 0;x< n;x++) { for(y = 0;y < n;y++) { printf("%d",a[x][y]); if(y<n-1) printf(" "); } printf("\n"); } } return 0;
}
题解
写错了,是EOJ 2979
题解
include
using namespace std;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
int T,n,tot,d;
int tu[20][20];
int isout(int xx)
{
if(xx<1 || xx>n) return 1;
else return 0;
}
int can_write(int xx,int yy)
{
if(!isout(xx) && !isout(yy) && tu[xx][yy]==0) return 1;
else return 0;
}
void write(int& xx,int& yy)
{
tu[xx][yy]=++tot;
if(tot==n*n) return;
int fx=xx+dx[d],fy=yy+dy[d];
while(!can_write(fx,fy))
{
d=(d+1)%4;
fx=xx+dx[d],fy=yy+dy[d];
}
xx=fx;yy=fy;return;
}
int main()
{
cin>>T;
for(int step=0;step>n;
int xx=1,yy=1;
d=0;
tot=0;
memset(tu,0,sizeof(tu));
}
return 0;
}
直接模拟,姊妹题:EOJ 2983
重写基本题目
c期中考试题
include
include
int main()
{
int x; /二维数组的第一个下标/
int y; /二维数组的第二个下标/
int n; /创建的矩阵为n*n/
int count = 1; /蛇形矩阵由1开始计数/
int round; /矩阵转的圈数/
}