信息学奥赛一本通(2045:【例5.13】蛇形填数)
生活随笔
收集整理的這篇文章主要介紹了
信息学奥赛一本通(2045:【例5.13】蛇形填数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2045:【例5.13】蛇形填數
時間限制: 1000 ms ??? ??? 內存限制: 65536 KB
提交數: 374 ??? 通過數: 191
【題目描述】
在n×nn×n方陣里填入1,2,3,…,n×n1,2,3,…,n×n,要求填成蛇形。例如n=4n=4時方陣為:
10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4其中,n≤20n≤20。
【輸入】
輸入nn。
【輸出】
輸出題述方陣。nn行,每行各數之間用一個空格隔開。
【輸入樣例】
4【輸出樣例】
10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4【參考代碼】
#include<stdio.h> #define N 30 int a[N][N]; int main() {int n,count=1,x,y;scanf("%d",&n);a[0][n-1]=1;x=0;y=n-1;while(count<n*n){while(x+1<n && !a[x+1][y])a[++x][y]=++count;while(y-1>=0 &&!a[x][y-1])a[x][--y]=++count;while(x-1>=0 &&!a[x-1][y])a[--x][y]=++count;while(y+1<n &&!a[x][y+1])a[x][++y] =++count;}for(x=0;x<n;x++){for(y=0;y<n;y++)printf("%d ",a[x][y]);printf("\n");}return 0; }http://ybt.ssoier.cn:8088/problem_show.php?pid=2045
總結
以上是生活随笔為你收集整理的信息学奥赛一本通(2045:【例5.13】蛇形填数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息学奥赛一本通 1136:密码翻译 |
- 下一篇: 信息学奥赛一本通 1071:菲波那契数