1157: 零起點學算法64——回型矩陣


1157: 零起點學算法64——回型矩陣

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 2440  Accepted: 925
[Submit][Status][Web Board]

Description

輸出n*m的回型矩陣

 

Input

多組測試數據
每組輸入2個整數 n和m(不大於20)  

 

Output

輸出n*m的回型矩陣,要求左上角元素是1,(每個元素占2個位置,靠右) 

 

Sample Input

 
4 3

 

Sample Output

 1  2  3
10 11  4
 9 12  5
 8  7  6

 

Source

 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     int n,m,a[20][20];
 5     while(scanf("%d%d",&n,&m)!=EOF){
 6         
 7         memset(a,0,sizeof(a));
 8         int tot,i,j;
 9         tot=a[i=0][j=0]=1;
10         while(tot<n*m){
11             while(j+1<m&&!a[i][j+1])
12               a[i][++j]=++tot;  
13             while(i+1<n&&!a[i+1][j])
14               a[++i][j]=++tot;
15             while(j-1>=0&&!a[i][j-1])
16               a[i][--j]=++tot;
17             while(i-1>=0&&!a[i-1][j])
18               a[--i][j]=++tot;
19         }
20         
21         for(int i=0;i<n;i++){
22             for(int j=0;j<m-1;j++){
23                 printf("%2d ",a[i][j]);
24             }
25             printf("%2d\n",a[i][m-1]);
26         }
27     }
28     
29     return 0;
30 } 

//依舊是思想很重要!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM