简体   繁体   English

在C中以一定条件打印直角三角形

[英]Print a right-angled triangle with certain condition in c

Enter a certain number, and that number is a condition which determines the number of chars in a single row.Let's say the number is 3 In the first row there is only 1 char. 输入一个特定的数字,该数字是确定单行中的字符数的条件。假设数字是3在第一行中只有1个字符。 In second row there is a condition. 在第二行中有一个条件。 a+1 Where a is the number that we entered In third row is 2a+1 Fourth 3a+1 And so on... Example: Number that we entered is 3. a+1其中,a是我们在第三行中输入的数字是2a+1第四是3a+1 ,以此类推...例如:我们输入的数字是3。

a (1)
aaaa (3+1)
aaaaaaa (2*3+1)

Here is what i've came up. 这就是我的想法。 I have trouble with implementing that condition. 我在执行该条件时遇到了麻烦。

#include<stdio.h>
main()
{
int i,j,n;

printf("Enter the numbers of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("a");
printf("\n");
}
getch();
} 

Just start iterating inner for loop starting from n till i * n as below, 只需从ni * n开始迭代内部for循环,如下所示,

for(i=1;i<=n;i++)
{
  for(j=n;j<=i*n;j++)
  printf("a");
  printf("\n");
}

Here is the demo 这是演示

Suggestion: get used to start counting at 0 建议:习惯从0开始计数

for(i=1;i<=n;i++) // could be for (i = 0; i < n; i++)
{
for(j=1;j<=i;j++) // could be for (j = 0; j < i; j++)

You have to have a multiplication by 3 "somewhere". 您必须乘以3“某处”。 Try and find the right place and what to multiply by 3. 尝试找到正确的位置以及乘以3的乘积。

for(i=0;i<n;i++){
    for(j=0;j<n*i+1;j++)
        printf("a");
    printf("\n");
}

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

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