简体   繁体   English

C程序打印直角三角形

[英]C program to print right angle triangle

I am trying to write a program in C using for loops which will output a pattern similar to this:我正在尝试使用 for 循环在 C 中编写一个程序,该循环将 output 类似于此的模式:

 1
 2 3
 4 5 6
 7 8 9 10

As you can see the pattern is a right angled triangle with each row with the same amount of numbers as the first number in each row.如您所见,该模式是一个直角三角形,每一行的数字数量与每行中的第一个数字相同。 I am a beginner in C and chose to go about solving to practice my use of for loops and done this step by step so i chose to first find out how i can print the first column.我是 C 的初学者,并选择 go 关于解决问题以练习我对 for 循环的使用并逐步完成此操作,因此我选择首先了解如何打印第一列。 Here is my code so far:到目前为止,这是我的代码:

#include <stdio.h>

int main()
{
   int n;
   int p = 1;

   printf("enter number of rows: ");
   scanf("%d", &n);

   for (int i= 1; i <= n; i += p++){
      printf("%d\n", i);
   }

   return 0;
}

What i found was that the first number of each column was being increased by 1 when broken down for example ( 1 + 1 = 2 , 2 + 2 = 4 , 4 + 3 = 7 ) As you can see each number i have made bold makes up the first column.我发现每列的第一个数字在分解时增加了 1 例如( 1 + 1 = 2 、 2 + 2 = 4 、 4 + 3 = 7 )正如你所看到的每个数字我都加粗了构成第一列。 My problem is that when i ask the use to enter a value to make up a specific number of rows, such as for example 5, the for statement prints out only 4 rows as you can see do below:我的问题是,当我要求用户输入一个值来组成特定数量的行时,例如 5,for 语句只打印出 4 行,如下所示:

enter number of rows: 5                                      
1                                                            
2                                                            
4

I am guessing this is due to the statement i <= n but how do i specify the amount of rows without disrupting the calculation of the first column?我猜这是由于语句 i <= n 但我如何指定行数而不中断第一列的计算?

Here you have a triangle printing program.这里有一个三角形打印程序。

int ndigits(unsigned long long x)
{
    int ndg = 0;
    while(x)
    {
        ndg++;
        x /= 10;
    }
    return ndg;
}

void triangle(int rows)
{
    int count = 1;
    int ndg = ndigits(rows * (rows + 1) /2);

    for(int row = 1; row <= rows; row++)
    {
        for(int column = 1; column <= row; column++)
        {
            printf("%0*d ", ndg, count++);
        }
        printf("\n");
    }
}

int main(void)
{
    triangle(30);
}

the function ndigits calculates number of decimal digits of the number. function ndigits计算数字的小数位数。 It is needed to print real triangle as all numbers have to have the same size (some will be padded by zeroes).需要打印实三角形,因为所有数字必须具有相同的大小(有些将用零填充)。 Otherwise the trangle will not look like a triangle.否则三角形看起来不会像三角形。

the rows * (rows + 1) /2 formula calculates the result of the 1+2+3+4+5... showing the largest number in the triangle rows * (rows + 1) /2公式计算 1+2+3+4+5... 的结果...显示三角形中的最大数字

three row triangle will look:三排三角形将看起来:

1 
2 3 
4 5 6 

ten rows triangle:十行三角形:

01 
02 03 
04 05 06 
07 08 09 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 

and 20 rows:和 20 行:

001 
002 003 
004 005 006 
007 008 009 010 
011 012 013 014 015 
016 017 018 019 020 021 
022 023 024 025 026 027 028 
029 030 031 032 033 034 035 036 
037 038 039 040 041 042 043 044 045 
046 047 048 049 050 051 052 053 054 055 
056 057 058 059 060 061 062 063 064 065 066 
067 068 069 070 071 072 073 074 075 076 077 078 
079 080 081 082 083 084 085 086 087 088 089 090 091 
092 093 094 095 096 097 098 099 100 101 102 103 104 105 
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 

you can try it yourself here: https://godbolt.org/z/7EWWso您可以在这里自己尝试: https://godbolt.org/z/7EWWso

You can use This Simple Code:您可以使用这个简单的代码:

#include <stdio.h>

int main(){
    int count=0;
    for (int i = 01; i < 11; ++i) {

        for (int j = 01; j <=i; ++j) {
            count+=01;
            printf("%d ",count);
        }
        printf("\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM