返回题目问答
讨论 / 题目问答/ 帖子详情

想问一下,我的代码为什么会超时,感觉和老师写的一样啊

#include<bits/stdc++.h>
using namespace std;
int a[105][105]={0};
int main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);
//	cout.tie(0);
int n,m;
scanf("%d %d",&n,&m);
memset(a,0,sizeof(a));
int tot=1,x=0,y=m-1;
a[x][y]=1;
while(tot<n*m){
while(x+1<n&&!a[x+1][y]) a[++x][y]=++tot;
while(y-1>=0&&!a[x][y-1]) a[x][--y]=++tot;
while(x-1>=0&&!a[x-1][y]) a[--x][y]=++tot;
while(y+1<n&&!a[x][y+1]) a[x][++y]=++tot;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%5d",a[i][j]);
}
printf("\n");
}
return 0;
}
0 回复 0 转发 0 喜欢 88 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!