34 条题解
-
-1yujingbin LV 6 @ 2015-11-14 15:37:08
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define mid k/2+1
int ans[40][40],p_l[1600],p_r[1600],have[40][40];
void solve(int k){
int i;
for (i=2; i<=k*k; i++) {
if (p_l[i-1]==1&&p_r[i-1]!=k) {
p_l[i]=k;
p_r[i]=p_r[i-1]+1;
have[p_l[i]][p_r[i]]=1;
}else if(p_r[i-1]==k&&p_l[i-1]!=1){
p_r[i]=1;
p_l[i]=p_l[i-1]-1;
have[p_l[i]][p_r[i]]=1;
}else if(p_l[i-1]==1&&p_r[i-1]==k){
p_l[i]=p_l[i-1]+1;
p_r[i]=p_r[i-1];
have[p_l[i]][p_r[i]]=1;
}else{
if (have[p_l[i-1]-1][p_r[i-1]+1]==0) {
p_l[i]=p_l[i-1]-1;
p_r[i]=p_r[i-1]+1;
have[p_l[i]][p_r[i]]=1;
}else{
p_l[i]=p_l[i-1]+1;
p_r[i]=p_r[i-1];
have[p_l[i]][p_r[i]]=1;
}
}
}
}
int main(){
int k,i,j;
scanf("%d",&k);
memset(ans,0,sizeof(ans));
memset(have,0,sizeof(have));
if(k%2!=0){
p_l[1]=1;
p_r[1]=mid;
have[p_l[1]][p_r[1]]=1;
solve(k);
for (i=1; i<=k*k; i++) {
ans[p_l[i]][p_r[i]]=i;
}
for (i=1; i<=k; i++) {
for (j=1; j<=k; j++) {
printf("%d ",ans[i][j]);
}
printf("\n");
}
}
return 0;
} -
-22017-02-10 18:07:24@
评测结果
编译成功测试数据 #0: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 548 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #7: Accepted, time = 15 ms, mem = 544 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 544 KiB, score = 10
测试数据 #9: Accepted, time = 15 ms, mem = 544 KiB, score = 10
Accepted, time = 30 ms, mem = 548 KiB, score = 100
代码include <cstdio>
include <cstring>
using namespace std;
int main()
{
int n=0,h=0,l=0;
int hf[41][41]={0};
scanf("%d",&n);h=1;
l=n/2+1;
hf[h][l]=1;
h=n;
l++;
for(long i=2;i<=n*n;i++)
{
if(h==1&&l!=n)
{
hf[h][l]=i;
h=n;
l++;
continue;
}
if(l==n&&h!=1)
{
hf[h][l]=i;
l=1;
h--;
continue;
}
if(h==1&&l==n)
{
hf[h][l]=i;
h++;
continue;
}
if(h!=1&&l!=n)
{
if(hf[h-1][l+1]!=0)
{
hf[h][l]=i;
h++;
continue;
}
if(hf[h-1][l+1]==0)
{
hf[h][l]=i;
h--;
l++;
continue;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
printf("%d ",hf[i][j]);
}
printf("\n");
}
return 0;
} -
-22016-11-07 14:21:16@
范围要大,几千才行
不能用cout输出,要用printf -
-22016-08-08 10:46:34@
var
n,i,j:integer;
a:array[0..39,0..39] of integer;procedure sub(k,x,y:integer); //搜索
var u,v:integer;
begin
if k<=sqr(n) then begin
if x=1 then begin
if y=n then begin u:=x+1;v:=y;end
else begin u:=n;v:=y+1;end;
end
else begin
if y=n then begin v:=1;u:=x-1; end
else begin
if (a[x-1,y+1]=0) then begin u:=x-1;v:=y+1;end
else begin u:=x+1;v:=y;end;
end;
end;a[u,v]:=k;
sub(k+1,u,v);
end;
end;begin
fillchar(a,sizeof(a),0);
readln(n);a[1,(n+1) div 2]:=1;
sub(2,1,(n+1) div 2);for i:=1 to n do
begin
for j:=1 to n do write(a[i,j],' ');
writeln;
end;end.
-
-22016-08-02 20:54:36@
注意空格啊= =醉了
program magic;
var k,x,y,i,j,n:integer;
a:array [0..50,0..50] of integer;
begin
//assign
//hang,lie
read(n);
fillchar(a,sizeof(a),0);
a[1,n div 2 +1]:=1;
x:=1; y:=n div 2 +1;
for k:=2 to n*n do
begin
if (x=1) and (y<>n) then
begin
a[n,y+1]:=k;
x:=n;
y:=y+1;
continue;
end;
if (x<>1) and (y=n) then
begin
a[x-1,1]:=k;
x:=x-1;
y:=1;
continue;
end;
if (x=1) and (y=n) then
begin
a[x+1,y]:=k;
x:=x+1;
continue;
end;
if (x<>1) and (y<>n) then
begin
if (a[x-1,y+1]=0) then
begin
a[x-1,y+1]:=k;
x:=x-1;
y:=y+1;
continue;
end
else begin a[x+1,y]:=k;
x:=x+1;
continue;
end;
end;
end;
for i:=1 to n do
begin
for j:=1 to n-1 do begin write(a[i,j],' '); end;
writeln(a[i,n]);
end;
end. -
-22016-07-20 21:33:47@
//c++模拟
#include<iostream>#include<stdio.h>
#include<string.h>
using namespace std;
int maze[50][50];
int n;
int main()
{
cin>>n;
if(n==1)
{
cout<<1;
return 0;
}
maze[1][(1+n)/2]=1;
int hang=1;
int lie=(1+n)/2;
for(int k=2;k<=n*n;k++)
{if((hang==1)&&(lie!=n))
{
hang=n;
lie+=1;
maze[hang][lie]=k;
continue;
}
if((lie==n)&&(hang!=1))
{
lie=1;
hang-=1;
maze[hang][lie]=k;
continue;
}
if(hang==1&&lie==n)
{
hang+=1;
maze[hang][lie]=k;
continue;
}
if(hang!=1)
{
if(lie!=n)
{
if(maze[hang-1][lie+1]==0)
{
hang-=1;
lie+=1;
maze[hang][lie]=k;
continue;
}
else
{
hang+=1;
maze[hang][lie]=k;
continue;
}
}
}
;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<maze[i][j]<<" ";
}
cout<<endl;
}
return 0;}
-
-22016-07-12 12:24:32@
#include <bits/stdc++.h> #define MAX_N 50 int arr[MAX_N + 10][MAX_N + 10]; using namespace std; int n; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin>>n; arr[1][(n + 1)>>1] = 1; int last_r = 1,last_c = ((n + 1)>>1),now_r = 0,now_c = 0; for (int i = 2,range=n*n; i <= range; i++) { if (last_r == 1 && last_c != n) now_r = n,now_c = last_c + 1; else if (last_r != 1 && last_c == n) now_r = last_r - 1,now_c = 1; else if (last_r == 1 && last_c == n) now_r = last_r + 1,now_c = last_c; else { if (!arr[last_r - 1][last_c + 1]) now_r = last_r - 1,now_c = last_c + 1; else now_r = last_r + 1,now_c = last_c; } arr[now_r][now_c] = i,last_r = now_r,last_c = now_c; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (j == 1)cout<<arr[i][j]<<" "; else cout<<arr[i][j]<<" "; } cout<<"\n"; } return 0; }
-
-22015-11-14 18:52:20@
诶,退役了......浙江太NB,已炸
这是道纯模拟题。模拟吧。没技巧,注意二维数组行和列。最好一开始就做个输出的子函数,每填一个数输出表格一次,可以加快排bug效率。
pascal代码等我程序发回来再发...
-
-32017-05-18 13:42:43@
#include<stdio.h> int f[50][50],n,i,j,k=2; int main(){ for(scanf("%d",&n),f[i=1][j=n+1>>1]=1;k<=n*n;f[i][j]=k++) if(i==1 && j<n) i=n,++j; else if(j==n&&i>1) j=1,i--; else if(i==1&&j==n) ++i; else !f[i-1][j+1]?i--,j++:i++; for(i=1;i<=n;++i,puts("")) for(j=0;j<n;printf("%d ",f[i][++j])); }
-
-32016-11-05 19:45:09@
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#include <queue>using namespace std;
int f[1000][1000],n,x,y;
int main(){
scanf("%d",&n);
f[x=1][y=(n+1)/2]=1;
for(int k=2;k<=n*n;f[x][y]=k++){
if(x==1&&y!=n)x=n,y=y+1;
else if(y==n&&x!=1)y=1,x--;
else if(x==1&&y==n)x++;
else !f[x-1][y+1]&&(x--,y++)||x++;
}
for(int i=1;i<=n;i++,puts(""))
for(int j=1;j<=n;printf("%d ",f[i][j++]));
} -
-32016-10-04 20:47:05@
可以用数学规律,第一行的正中间一定为1,然后向左移一格,向上移一格为下一个数,若到边界就回到另一边开始,若该格已有数则下移一格。
-
-32016-07-17 09:49:46@
这不就是硬解么...Pascal代码
Pascal
var n,x,y,k:integer;
a:array[1..39,1..39] of integer;
begin
readln(n);fillchar(a,sizeof(a),0);
y:=n div 2+1;x:=1;a[x,y]:=1;
if n=1 then begin writeln('1');halt; end
else begin
for k:=2 to n*n do
begin
if (x=1) and (y<>n) then begin x:=n;inc(y); end
else if (y=n) and (x<>1) then begin y:=1;dec(x); end
else if (x=1) and (y=n) then inc(x)
else if a[x-1,y+1]=0 then begin dec(X);inc(y); end
else inc(x);
a[x,y]:=k;
end;
end;
for x:=1 to n do
begin
for y:=1 to n do write(a[x,y],' ');
writeln;
end;
end.
-
-32016-06-19 10:41:11@
~c++
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=40;
int n,a[maxn][maxn],x,y;
int main()
{
scanf("%d",&n);
a[1][n/2+1]=1;
x=1,y=(n/2)+1;
for(int i=2;i<=n*n;i++)
{
if((x==1)&&(y!=n))x=n,y+=1;
else
if((x!=1)&&(y==n))y=1,x-=1;
else
if((x==1)&&(y==n))x=2,y=n;
else
if(a[x-1][y+1]==0)x-=1,y+=1;
else
x+=1,y=y;
a[x][y]=i;}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)printf("%d ",a[i][j]);
printf("\n");}
return 0;
} -
-42016-11-02 17:40:07@
#include <iostream> #include <cstring> using namespace std; struct _hash{ int x,y; }; int k; int map[40][40]={0}; _hash myhash[40*40]={0}; int n; void set(int k,int x,int y){ myhash[k].x = x; myhash[k].y = y; map[x][y]=k; } int main(){ cin>> n; memset(map,0,sizeof(map)); memset(myhash,0,sizeof(myhash)); set(1,1,n/2+1); for(k =2;k<=n*n;k++){ if(myhash[k-1].x==1&&myhash[k-1].y!=n){ set(k,n,myhash[k-1].y+1); } else if(myhash[k-1].y==n&&myhash[k-1].x!=1){ set(k,myhash[k-1].x-1,1); } else if(myhash[k-1].x==1&&myhash[k-1].y==n){ set(k,2,n); } else if(myhash[k-1].x!=1&&myhash[k-1].y!=n){ if(map[myhash[k-1].x-1][myhash[k-1].y+1]==0){ set(k,myhash[k-1].x-1,myhash[k-1].y+1); } else{ set(k,myhash[k-1].x+1,myhash[k-1].y); } } else{} } for(int x=1;x<=n;x++){ for(int y = 1;y<=n;y++){ cout<<map[x][y]<<" "; } cout<<endl; } return 0; }
信息
- ID
- 1978
- 难度
- 2
- 分类
- (无)
- 标签
- 递交数
- 2443
- 已通过
- 1340
- 通过率
- 55%
- 被复制
- 8
- 上传者