1. 程式人生 > >CCF201412-2 Z字形掃描(模擬)

CCF201412-2 Z字形掃描(模擬)

得到 while ostream () turn rect pac clu 分隔

 對於下面的4×4的矩陣,
  1 5 3 9
  3 7 5 6
  9 4 6 4
  7 3 1 3
  對其進行Z字形掃描後得到長度為16的序列:
  1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
  請實現一個Z字形掃描的程序,給定一個n×n的矩陣,輸出對這個矩陣進行Z字形掃描的結果。 輸入格式   輸入的第一行包含一個整數n,表示矩陣的大小。
  輸入的第二行到第n+1行每行包含n個正整數,由空格分隔,表示給定的矩陣。 輸出格式   輸出一行,包含n×n個整數,由空格分隔,表示輸入的矩陣經過Z字形掃描後的結果。 樣例輸入 4
1 5 3 9
3 7 5 6
9 4 6 4
7 3 1 3 樣例輸出 1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3 評測用例規模與約定   1≤n≤500,矩陣元素為不超過1000的正整數。
 1
#include<iostream> 2 using namespace std; 3 //找規律 4 int direction[4][2]={0,1,1,-1,1,0,-1,1}; 5 int table[501][501]; 6 int main(){ 7 int i,j,n; 8 cin>>n; 9 for(i=0;i<n;i++){ 10 for(j=0;j<n;j++){ 11 cin>>table[i][j]; 12 } 13 } 14 i=0
; 15 j=0; 16 cout<<table[0][0]<< ; 17 while(i!=n-1||j!=n-1){ 18 if(j!=n-1){ 19 i+=direction[0][0]; 20 j+=direction[0][1]; 21 }else{ 22 i+=direction[2][0]; 23 j+=direction[2][1]; 24 } 25 cout<<table[i][j]<<
; 26 if(i==n-1&&j==n-1){ 27 break; 28 } 29 while(j!=0&&i!=n-1){ 30 i+=direction[1][0]; 31 j+=direction[1][1]; 32 cout<<table[i][j]<< ; 33 } 34 if(i!=n-1){ 35 i+=direction[2][0]; 36 j+=direction[2][1]; 37 }else{ 38 i+=direction[0][0]; 39 j+=direction[0][1]; 40 } 41 cout<<table[i][j]<< ; 42 if(i==n-1&&j==n-1){ 43 break; 44 } 45 while(i!=0&&j!=n-1){ 46 i+=direction[3][0]; 47 j+=direction[3][1]; 48 cout<<table[i][j]<< ; 49 } 50 } 51 cout<<"\b\n"; 52 return 0; 53 }

CCF201412-2 Z字形掃描(模擬)