码迷,mamicode.com
首页 > 编程语言 > 详细

[C++]蛇形填数

时间:2019-12-07 01:31:19      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:cin   int   ++   while   turn   pre   开始   code   include   

[从左下角开始,逆时针蛇形填数]

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main() {
 5     int n;
 6     cin>>n;
 7     int **p=new int*[n];
 8     for(int i=0;i<n;i++)
 9     {
10         p[i]=new int[n];
11         for(int j=0;j<n;j++)
12             p[i][j]=0;
13     }
14     
15     int x,y;
16     int total=p[x=n-1][y=0]=1;
17     
18     while(total<n*n)
19     {
20         while(y+1<n&&!p[x][y+1])p[x][++y]=++total;
21         while(x-1>=0&&!p[x-1][y])p[--x][y]=++total;
22         while(y-1>=0&&!p[x][y-1])p[x][--y]=++total;
23         while(x+1<n&&!p[x+1][y])p[++x][y]=++total;
24     }
25     
26     for(int i=0;i<n;i++)
27     {
28         for(int j=0;j<n;j++)
29             cout<<p[i][j]<<"\t";    
30         cout<<endl;
31     }
32         
33     return 0;
34 }

 

[C++]蛇形填数

标签:cin   int   ++   while   turn   pre   开始   code   include   

原文地址:https://www.cnblogs.com/dreamcenter/p/12000302.html

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