Advertisement
lIlIlIlIIlI

ProblemSolving_Tornado.c

Sep 15th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3.  
  4. /*
  5. struct PrintTornado{
  6.    
  7. };
  8. */
  9.  
  10. int** ConstructTor(int n){
  11.     int** ms = malloc(n * sizeof(int*) );
  12.     int i = 0;
  13.     while(i < n){
  14.         ms[i] = malloc(n * sizeof(int) );
  15.         i++;
  16.     }
  17.     return ms;
  18. }
  19.  
  20. void go(int** Tornado, int num, int X, int Y, int direction){
  21.     int count = 0;
  22.     int steps = 0;
  23.     int move = 0;
  24.     int i;
  25.     while(count < num * num){
  26.         if(direction == 0){
  27.             steps = 1 + (move / 2);
  28.             for(i = 0; i < steps; i++){
  29.                 printf("%d", Tornado[Y][X--] );
  30.                 count++;
  31.             }  
  32.             move++;
  33.             direction = 1; 
  34.         }else if(direction == 1){
  35.             steps = 1 + (move / 2);
  36.             for(i = 0; i < steps; i++){
  37.                 printf("%d", Tornado[Y--][X] );
  38.                 count++;
  39.             }  
  40.             move++;    
  41.             direction = 2;         
  42.         }else if(direction == 2){
  43.             steps = 1 + (move / 2);
  44.             for(i = 0; i < steps; i++){
  45.                 printf("%d", Tornado[Y][X++] );
  46.                 count++;
  47.             }          
  48.             move++;
  49.             direction = 3; 
  50.         }else{
  51.             steps = 1 + (move / 2);
  52.             for(i = 0; i < steps; i++){
  53.                 printf("%d", Tornado[Y++][X] );
  54.                 count++;
  55.             }
  56.             move++;
  57.             direction = 0; 
  58.         }          
  59.     }
  60. }
  61.  
  62. int main(){
  63.     int num;
  64.     int direction;
  65.     while(scanf("%d", &num) != EOF){
  66.         int** Tornado = ConstructTor(num);     
  67.         scanf("%d", &direction);
  68.         // 0代表左 、1代表上 、2代表右 、3代表下
  69.         int i = 0;
  70.         while(i < num){
  71.             int j = 0;
  72.             while(j < num){
  73.                 scanf("%d", &Tornado[i][j] );
  74.                 j++;
  75.             }
  76.             i++;
  77.         }
  78.         int Xin = (num / 2);
  79.         int Yin = (num / 2);
  80.         go(Tornado, num, Xin, Yin, direction);
  81.         printf("\n");
  82.     }
  83.     return 0;
  84. }
  85. //(2ms, 116KB)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement