【问题标题】:Write a basic half pyramid pattern program编写一个基本的半金字塔图案程序
【发布时间】:2019-11-14 08:10:49
【问题描述】:

尝试了一些基本模式

尝试获取模式

1
2 4
3 6 12
4 8 16 32

到目前为止试图找到正确的序列,我的想法是需要另一个变量让我们说 num,并且需要为 num 创建一个序列以最终打印 num

#include <stdio.h>
int main()
{
    int rows = 0  , i, j , num,num2;


   do{
        printf("please enter the number of rows: ");
       scanf("%d",&rows);
    }while(rows <=2 );
    printf("printing a half pyramid of %d rows", rows);
    printf("\n");
     for( i = 1; i <=rows; ++i) {
        for (j = 1; j <= i; ++j ) {

            printf("%d ",  );


        }
       printf("\n");

    }
    return 0;
}

无法确定顺序

【问题讨论】:

  • 最后一行真的是4 8 16 32吗?因为如果是4 8 12 16 一个简单的printf("%d ", i*j); 就可以了;^)
  • @WhozCraig 仅当 prog-fh 的评论正确时。否则,这将与所需的输出不匹配。
  • 是的,这是我的实际问题,我想通了。只是试图找到一个变化,看看我是否可以通过它:)
  • 哦,对不起,我写错了模式,刚刚检查了我的书,准备尝试 3,6,12> 4,8,16,32。
  • 那么如果是3 6 12 printf("%d ", i&lt;&lt;(j-1) ); 应该可以工作

标签: c loops for-loop while-loop do-while


【解决方案1】:

您收到的代码实际上包含所有必要的部分。剩下的就是在嵌套循环中填写这一行:

printf("%d ", <i>‹what goes here?›</i>);

要找到答案,您需要找出值与当前行和列的关系(分别由ij 给出)。

您不需要额外的变量num(明确地说,您可以创建一个,但没有必要解决这个问题。

【讨论】:

    【解决方案2】:

    我们,初学者,应该互相帮助。:)

    你来了。

    #include <stdio.h>
    
    int main(void) 
    {
        while ( 1 )
        {
            const unsigned int Base = 10;
    
            printf( "Enter the height of a pyramid (0 - exit): " );
    
            unsigned int n;
    
            if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
    
            int width = 0;
            unsigned int tmp = n * n;
    
            do { ++width; } while ( tmp /= Base );
    
            putchar( '\n' );
    
            for ( unsigned int i = 0; i < n; i++ )
            {
                unsigned int value = i + 1;
                for ( unsigned int j = 0; j++ <= i; )
                {
                    printf( "%*u ", width, value * j );
                }
                putchar( '\n' );
            }
    
            putchar( '\n' );
        }
    
        return 0;
    }
    

    程序输出可能看起来像

    Enter the height of a pyramid (0 - exit): 1
    
    1 
    
    Enter the height of a pyramid (0 - exit): 2
    
    1 
    2 4 
    
    Enter the height of a pyramid (0 - exit): 3
    
    1 
    2 4 
    3 6 9 
    
    Enter the height of a pyramid (0 - exit): 4
    
     1 
     2  4 
     3  6  9 
     4  8 12 16 
    
    Enter the height of a pyramid (0 - exit): 5
    
     1 
     2  4 
     3  6  9 
     4  8 12 16 
     5 10 15 20 25 
    
    Enter the height of a pyramid (0 - exit): 6
    
     1 
     2  4 
     3  6  9 
     4  8 12 16 
     5 10 15 20 25 
     6 12 18 24 30 36 
    
    Enter the height of a pyramid (0 - exit): 7
    
     1 
     2  4 
     3  6  9 
     4  8 12 16 
     5 10 15 20 25 
     6 12 18 24 30 36 
     7 14 21 28 35 42 49 
    
    Enter the height of a pyramid (0 - exit): 8
    
     1 
     2  4 
     3  6  9 
     4  8 12 16 
     5 10 15 20 25 
     6 12 18 24 30 36 
     7 14 21 28 35 42 49 
     8 16 24 32 40 48 56 64 
    
    Enter the height of a pyramid (0 - exit): 9
    
     1 
     2  4 
     3  6  9 
     4  8 12 16 
     5 10 15 20 25 
     6 12 18 24 30 36 
     7 14 21 28 35 42 49 
     8 16 24 32 40 48 56 64 
     9 18 27 36 45 54 63 72 81 
    
    Enter the height of a pyramid (0 - exit): 0
    

    程序中的循环也可以是这样的

    for ( unsigned int i = 0; i++ < n;  )
    {
        unsigned int value = i;
        for ( unsigned int j = 0; j < i; j++ )
        {
            printf( "%*u ", width, value );
            value += i;
        }
        putchar( '\n' );
    }
    

    或者不引入中间变量valuelike

    for ( unsigned int i = 0; i < n; i++ )
    {
        for ( unsigned int j = 0; j++ <= i; )
        {
            printf( "%*u ", width, j * ( i + 1 ) );
        }
        putchar( '\n' );
    }
    

    您自己可以在程序中添加检查n * n 不大于UINT_MAX

    编辑:当您更改模式中的显示值时,程序可以看起来像以下方式

    #include <stdio.h>
    #include <math.h>
    
    int main(void) 
    {
        while ( 1 )
        {
            const unsigned int Base = 10;
    
            printf( "Enter the height of a pyramid (0 - exit): " );
    
            unsigned int n;
    
            if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
    
            int width = 0;
            unsigned long long int tmp = n * ( long long unsigned )pow( 2, ( n - 1 ) );
    
            do { ++width; } while ( tmp /= Base );
    
            putchar( '\n' );
    
            for ( unsigned int i = 0; i++ < n;  )
            {
                unsigned int value = i;
                for ( unsigned int j = 0; j < i; j++ )
                {
                    printf( "%*u ", width, value );
                    value *= 2;
                }
                putchar( '\n' );
            }
    
            putchar( '\n' );
        }
    
        return 0;
    }
    

    程序输出可能看起来像

    Enter the height of a pyramid (0 - exit): 10
    
       1 
       2    4 
       3    6   12 
       4    8   16   32 
       5   10   20   40   80 
       6   12   24   48   96  192 
       7   14   28   56  112  224  448 
       8   16   32   64  128  256  512 1024 
       9   18   36   72  144  288  576 1152 2304 
      10   20   40   80  160  320  640 1280 2560 5120 
    
    Enter the height of a pyramid (0 - exit): 0
    

    【讨论】:

      【解决方案3】:

      棘手的模式。这是该模式的实现逻辑。 'n' 是行数。

      #include <stdio.h>
      int main(void) {
      int n = 4;
          for(int i=1; i<=n; i++) {
              int k=i;
              printf("%d%s",k," ");
              for(int j=1; j<i; j++) {
                  k = k*2;
                  printf("%d%s",k," ");
              }
              printf("\n");
          }
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多