(이춘 유 편집장) 방과 후 연습 문제 [연습 문제 6]

15741 단어 데이터 구조
【6.5】
 1 #include <iostream>

 2 

 3 using namespace std;  4 #define MAXN 100

 5 #define N 4

 6 #define M 4

 7 int x,y,num;  8 int a[MAXN][MAXN] = {  9     {0,2,3,4}, 10     {1,5,6,7}, 11     {8,9,10,11}, 12     {12,13,14,15}}; 13 bool FindX(int X) 14 { 15     while(a[x][y]!=X){ 16         if(a[x][y]<X) 17             x++; 18         else

19             y--; 20         num++; 21         if(x<0 || x>=M || y<0 || y>=N)  //

22             return false; 23  } 24     return true; 25 } 26 int main() 27 { 28     int X; 29     while(cin>>X){ 30         num=0; 31         x=0,y=N-1; 32         if(FindX(X)){ 33             cout<<"  x,   "<<'('<<x<<','<<y<<')'<<endl; 34             cout<<"   "<<num<<" "<<endl; 35  } 36         else{ 37             cout<<"    "<<endl; 38  } 39  } 40     return 0; 41 }

【6.6】
 1 #include <iostream>

 2 

 3 using namespace std;  4 #define M 4

 5 #define N 4

 6 #define MAXSIZE M*N

 7 typedef struct{  8     int r;  //  

 9     int c;  //  

10     int d;  //   

11 }TupNode;   //     

12 typedef struct{ 13     int rows;   //  

14     int cols;   //  

15     int nums;   // 0   

16  TupNode data[MAXSIZE]; 17 }TSMatrix;  //        

18 int GetSum(TSMatrix ts) //             

19 { 20     int i,sum=0; 21     for(i=0;i<ts.nums;i++){ 22         if(ts.data[i].c==ts.data[i].r) 23             sum+=ts.data[i].d; 24  } 25     return sum; 26 } 27 int main() 28 { 29     int Case=1,nums,i,r,c,d; 30     cout<<"*        "<<M<<'*'<<N<<",        "<<endl<<endl; 31     cout<<"#Case "<<Case++<<':'<<endl; 32     cout<<"           0     :"<<endl; 33     while(cin>>nums){ 34         if(nums==0) break;   //   0  

35  TSMatrix ts; 36         ts.rows = M;    //

37         ts.cols = N; 38         ts.nums = nums; 39         for(i=0;i<ts.nums;i++){ //     nums  0  

40             cout<<"    "<<i+1<<"  0    ,      "<<endl; 41             cin>>r>>c>>d;  //      ,  ,   

42             if(r<0 || r>=M || c<0 || c>=N){ //  

43                 cout<<""<<endl; 44                 i--; 45  } 46             else{   //    

47                 ts.data[i].r = r; 48                 ts.data[i].c = c; 49                 ts.data[i].d = d; 50  } 51  } 52         cout<<"         "<<GetSum(ts)<<endl<<endl; 53         cout<<"#Case "<<Case++<<':'<<endl; 54         cout<<"           0     :"<<endl; 55  } 56     return 0; 57 }

Freecode :  www.cnblogs.com/yym2013

좋은 웹페이지 즐겨찾기