【问题标题】:C program to print the power of 2 Triangle pattern using nested loopsC 程序使用嵌套循环打印 2 个三角形图案的幂
【发布时间】:2023-02-15 22:46:42
【问题描述】:

我正在尝试这段代码;

#include <stdio.h>

void main()
{
    int no_row=5,c=1,blk,i,j;
    //printf("Input number of rows: ");
    //scanf("%d",&no_row);
    for(i=0;i<no_row;i+=2)
    {
        for(blk=1;blk<=no_row-i;blk++)
            printf("  ");
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                c=1;
            else
                c=c*(i-j+1)/j;
            printf("% 4d",c);
        }
        printf("\n");
    }
}

我需要帮助来编写代码以获得如图所示的输出。 power 2 模式在图片中

【问题讨论】:

  • 使用制表符而不是数字之间的空格?

标签: c loops for-loop nested


【解决方案1】:

有困难的方法(使用 5 个不同的变量),也有简单的方法(使用缓冲区和三个变量)。

这具有“嵌套循环”并向上和向下计数,并适当缩进。

#include <stdio.h>

int main() {
    char buf[ 128 ], *at;
    int r, i;

    int lftBlnk = 32;
    for( r = 1; r <= 256; r *= 2 ) {
        at = buf;
        for( i = 1; i <= r; i *= 2 ) // ascending
            at += sprintf( at, "%-4d", i );
        for( i /= 4; i; i /= 2 ) // descending
            at += sprintf( at, "%-4d", i );

        printf( "%*s%s
", lftBlnk, "", buf ); // indent & output
        lftBlnk -= 4;
    }

    return 0;
}

输出

                                1
                            1   2   1
                        1   2   4   2   1
                    1   2   4   8   4   2   1
                1   2   4   8   16  8   4   2   1
            1   2   4   8   16  32  16  8   4   2   1
        1   2   4   8   16  32  64  32  16  8   4   2   1
    1   2   4   8   16  32  64  128 64  32  16  8   4   2   1
1   2   4   8   16  32  64  128 256 128 64  32  16  8   4   2   1

编辑:

找点乐子,去掉不必要的东西,代码会变得非常紧凑。

int main() {
    for( int lftBlnk = 32, r = 1; r <= 256; r *= 2, lftBlnk -= 4 ) {
        printf( "%*s", lftBlnk, "");
        int i;
        for( i = 1; i <= r; i *= 2 ) printf( "%-4d", i );
        for( i /= 4; i; i /= 2 ) printf( "%-4d", i );
        putchar( '
' );
    }

    return 0;
}

事实上,让我们摆脱一个变量,添加另一个变量,并提高代码的灵活性(没有像“4”这样的重复常量和像 256 这样的幻数。)现在,指定编号。行数,并为每列留出足够的宽度...

int main() {
    int rows = 11, wid = 5;
    for( int r = 0; r < rows; r++ ) {
        printf( "%*s", (rows-1-r)*wid, "");
        int i;
        for( i = 1; i <= (1 << r); i *= 2 ) printf( "%-*d", wid, i );
        for( i /= 4; i; i /= 2 ) printf( "%-*d", wid, i );
        putchar( '
' );
    }

    return 0;
}

                                                  1
                                             1    2    1
                                        1    2    4    2    1
                                   1    2    4    8    4    2    1
                              1    2    4    8    16   8    4    2    1
                         1    2    4    8    16   32   16   8    4    2    1
                    1    2    4    8    16   32   64   32   16   8    4    2    1
               1    2    4    8    16   32   64   128  64   32   16   8    4    2    1
          1    2    4    8    16   32   64   128  256  128  64   32   16   8    4    2    1
     1    2    4    8    16   32   64   128  256  512  256  128  64   32   16   8    4    2    1
1    2    4    8    16   32   64   128  256  512  1024 512  256  128  64   32   16   8    4    2    1

【讨论】:

  • @Epsilon C 是一门美丽的语言,而且功能无限...享受每天学习新事物的乐趣...感谢有机会玩得开心:-)
  • @Epsilon 更进一步!对其进行(最低限度)调整以打印帕斯卡三角形!无限可能:)
【解决方案2】:

首先,固定计算以获得 2 的幂 然后使用制表符作为分隔符以获得与图片相同的对齐方式

                                    1   
                                1   2   1   
                            1   2   4   2   1   
                        1   2   4   8   4   2   1   
                    1   2   4   8   16  8   4   2   1   
                1   2   4   8   16  32  16  8   4   2   1   
            1   2   4   8   16  32  64  32  16  8   4   2   1   
        1   2   4   8   16  32  64  128 64  32  16  8   4   2   1   
    1   2   4   8   16  32  64  128 256 128 64  32  16  8   4   2   1   
1   2   4   8   16  32  64  128 256 512 256 128 64  32  16  8   4   2   1   
#include <stdio.h>

void main()
{
    int no_row=10,c=1,blk,i,j;
    //printf("Input number of rows: ");
    //scanf("%d",&no_row);
    for(i=0;i<no_row;i++)
    {
        for(blk = 0; blk < no_row - i - 1; blk++)
            printf("	");
        c = 1;
        printf("%d	",c);
        for(j = 0;j < i;j++)
        {
            c=c*2;
            printf("%d	",c);
        }
        for(j = 0;j < i;j++)
        {
            c=c/2;
            printf("%d	",c);
        }
        printf("
");
    }
}

【讨论】:

    【解决方案3】:

    我用另一种方法来实现你说的

    这个方法需要两个参数,一个是需要推进的倍数,一个是需要推进的行数。

    #include <stdio.h>
    
    //row main how many lines do you want to print
    //multiple means you want it to be several times expanded
    void  Triangle(int multiple, int row){
        int value = 1, mid;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < 2*row-1; j++)
            {
                mid = (2*(row-1)+1)/2;  //It means that the middlemost pointer points to
                //mid - i adn mid + i means that there is a majority of the number that needs to be displayed on this line
                if (j >= mid -i && j<= mid +i)
                {
                    if (j>mid)
                    {
                        value = value / multiple;
                        printf("%d", value);
                    }else{
                        printf("%d", value);
                        if (j!= mid)
                        {
                            value = value * multiple;
                        }
                    }
                }else{
                    printf(" ");
                }
                printf("    ");
            }
            value = 1;
            printf("
    ");
        }
    } 
    
    //Example
    int main(){
        Triangle(2,7);
        return 0;
    }
    

    结果:

    当然你甚至可以使用 3 或更多的倍数

    int main(){
        Triangle(3,7);
        return 0;
    }
    

    结果:

    【讨论】:

      【解决方案4】:

      以下是我基于您的代码的解决方案:

      #include <stdio.h>
      #include <stdlib.h>
      #include <stdint.h>
      
      #define MAX 64
      
      int main(int argc, char **argv)
      {
          while (--argc, *++argv) {
      
              int N = atoi(argv[0]);
              if (N <= 0 || N >= 64)
                  continue;
      
              int wide = snprintf(NULL, 0,
                  "%llu", 1ULL << (N-1)) + 1;
      
              for(int i = 0; i < N; i++) {
      
                  /* print spaces */
                  for(int j = 1; j < N - i; j++)
                      printf("%*s", wide, "");
      
                  uint64_t bit, end = 1ULL << i;
      
                  /* print the growing seq */
                  for(bit = 1; bit && bit < end; bit <<= 1)
                      printf("%*lu", wide, bit);
      
                  /* print the decreasing seq */
                  for(; bit; bit >>= 1)
                      printf("%*lu", wide, bit);
                  printf("
      ");
              }
          }
          return 0;
      }
      

      它首先计算要打印的最大数字的宽度,并将其用作字段长度以打印左边的空格和数字之间的间隙。 该程序将要打印的三角形的高度作为命令行参数,并使用 uint64_t 来达到最大 63 的大小。

      该代码使用位运算符来计算 2 的幂,并使用位移位来生成连续的 2 的幂。

      我产生了一个右对齐的输出,在我看来它更自然,但为了产生左移输出,你只需要在 '%''*' 字符之间添加一个 -,在输出到左边证明它。

      下面是一个示例运行

      $ a.out 3 11
           1
         1 2 1
       1 2 4 2 1
                                                            1
                                                       1    2    1
                                                  1    2    4    2    1
                                             1    2    4    8    4    2    1
                                        1    2    4    8   16    8    4    2    1
                                   1    2    4    8   16   32   16    8    4    2    1
                              1    2    4    8   16   32   64   32   16    8    4    2    1
                         1    2    4    8   16   32   64  128   64   32   16    8    4    2    1
                    1    2    4    8   16   32   64  128  256  128   64   32   16    8    4    2    1
               1    2    4    8   16   32   64  128  256  512  256  128   64   32   16    8    4    2    1
          1    2    4    8   16   32   64  128  256  512 1024  512  256  128   64   32   16    8    4    2    1
      $ _
      

      【讨论】:

        【解决方案5】:

        编写一个程序,计算并打印二维数组对角线上正成员的总和

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          • 1970-01-01
          • 2020-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多