使用循环打印图案

时间:2019-01-16 10:59:43

标签: c loops

我正在解决有关hackerrank的挑战。它以螺旋图案打印数字,每完成一个圆便减小其值。

4 4 4 4 4 4 4
   4 3 3 3 3 3 4
   4 3 2 2 2 3 4
   4 3 2 1 2 3 4
   4 3 2 2 2 3 4
   4 3 3 3 3 3 4
   4 4 4 4 4 4 4

//Printing Pattern using Loops
/* for eg. for n = 4 
  4 4 4 4 4 4 4
  4 3 3 3 3 3 4
  4 3 2 2 2 3 4
  4 3 2 1 2 3 4
  4 3 2 2 2 3 4
  4 3 3 3 3 3 4
  4 4 4 4 4 4 4
 */
 //Author: Arvind Bakshi
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
 #include <stdlib.h>

int main() 
{

int n,row,col,size;
scanf("%d", &n);
// Complete the code to print the pattern.
size=2*n-1;
  int arr[size][size];
  //n=n+1;
    while(n>0){
        row=0;
        col=0;
        while(col<size){
          arr[row][col] = n;
          col++;
        }
        col=size-1;
        row=0;
        while(row<size){
            arr[row][col]=n;
            row++;
        }
        row = size-1;
        col = size-1;
        while (col >=0) {
          arr[row][col] = n;
          col--;
        }
        col = 0;
        row = size-1;
        while (row >=0) {
          arr[row][col] = n;
          row--;
        }
        n--;
    }
    for(row=0;row<size;row++){
        for(col=0;col<size;col++){
            printf("%d",arr[row][col]);
        }
        printf("\n");
    }
return 0;
}

预期输出为

 2 2 2 
 2 1 2 
 2 2 2

我得到了

111
101
111

网上有很多可用的代码,我只是想知道我的错误,我做错的地方。请不要将其标记为重复。

3 个答案:

答案 0 :(得分:0)

检查此代码。 Online compiler 我将相同的代码与额外的缓冲区“ x”一起使用。这样一来,框架就可以以逐渐减小的螺旋度在循环中移动。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
  int n,row,col,size;
  scanf("%d", &n);
  // Complete the code to print the pattern.
  size=2*n;
  int x = n;
  int arr[size-1][size-1];
  //n=n+1;
  while(n>0){
    row=x-n;
    col=x-n;
    while(col<size-(x-n)-2){
      arr[row][col] = n;
      col++;
    }
    col=size-(x-n)-2;
    row=x-n;
    while(row<size-(x-n)-1){
      arr[row][col]=n;
      row++;
    }
    row = size-(x-n)-2;
    col = size-(x-n)-2;
    while (col >x-n) {
      arr[row][col] = n;
      col--;
    }
    col = x-n;
    row = size-(x-n)-2;
    while (row > x-n) {
      arr[row][col] = n;
      row--;
    }
    n--;
  }
  for(row=0;row<size-1;row++){
    for(col=0;col<size-1;col++){
      printf("%d ",arr[row][col]);
    }
    printf("\n");
  }
  return 0;
}


这些是您的预期输出。 for input 2 for input 4

答案 1 :(得分:0)

如果性能和“覆盖”无关紧要,则可以尝试使用此直观的简短版本。

#include <stdio.h>
#include <stdlib.h>

int main() 
{
  int n, size;
  scanf("%d", &n);
  size=2*n-1;
  int arr[size][size];

  for(int i=0; i<n;i++)
      for(int row=i; row<size-i; row++)
          for(int col=i; col<size-i; col++)
            arr[row][col] = n-i;

  for(int row=0; row<size; row++) {
    for(int col=0; col<size; col++)
      printf("%d ",arr[row][col]);
    printf("\n");
  }
  return 0;
}

答案 2 :(得分:0)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d", &n);

    for(int i = n; i >= 1; i--){
        for(int j = n; j > i; j--)
            printf("%d ", j);
        for(int j = 1; j <= 2 * i - 1; j++)
            printf("%d ", i);
        for(int j = i + 1; j <= n; j++)
            printf("%d ", j);
        printf("\n");
    }

    for(int i = 1; i < n; i++){
        for(int j = n; j > i; j--)
            printf("%d ", j);
        for(int j = 1; j <= 2 * i - 1; j++)
            printf("%d ", i + 1);
        for(int j = i + 1; j <= n; j++)
            printf("%d ", j);
        printf("\n");
    }

    return 0;
}
相关问题