码迷,mamicode.com
首页 > 其他好文 > 详细

南阳oj 蛇形填数

时间:2018-07-22 20:46:55      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ios   iostream   for   stream   std   count   turn   clu   end   

#include<iostream>
using namespace std;
int main ()
{
int n,m,max;
cin>>n;
m=n;
max=n*n;
int count=1;
int s[105][105];
int x=1,y=n;
while(count<=max)
{
while(x<=m&&count<=max)
{
s[x][y]=count++;
x++;
}
y--;
x--;
while(y>=n-m+1&&count<=max)
{
s[x][y]=count++;
y--;

}
x--;
y++;
while(x>=n-m+1&&count<=max)
{
s[x][y]=count++;
x--;
}
y++;
x++;
m--;
while(y<=m&&count<=max)
{
s[x][y]=count++;
y++;
}
x++;
y--;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j<n)
cout<<s[i][j]<<" ";
else if(j==n)
cout<<s[i][j]<<endl;
}
}
return 0;
}

 

蛇形填数,这次是自己做出来的,有一些技巧和规律

但是在处理循环次数的时候还是不熟练,不清晰。

等有时间看看大佬的代码学习一下。

南阳oj 蛇形填数

标签:ios   iostream   for   stream   std   count   turn   clu   end   

原文地址:https://www.cnblogs.com/huanya/p/9351151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!