【发布时间】:2020-12-28 20:00:37
【问题描述】:
输入:
输入行数:5
预期输出:
1
121
12321
1234321
123454321
我正在尝试按照以下方法解决该问题->
方法:
#include <stdio.h>
int main(){
//Declaring variables for storing information
int num_rows,value=1,p=1,t=1,f=1;
//prompting and taking input from the user
printf("Please enter the number of row: ");
scanf("%d",&num_rows);
for(int i=1; i<=num_rows; i++){
for(int m=1; m<=num_rows-i; m++){
printf(" ");
}
for(int j=1; j<=p; j++){
//Condition to display the value as 1 for the first iteration as well as each of the row's first position
if(i==1 || j==1){
value=1;
}
//Condition to display the value as 1 for each of the row's last position
else if(i==t && j==f){
value=1;
}
else{
//This is for displaying value within first and last value of each row
value=value+1;
}
printf("%d",value);
}
t++;
f=f+2;
printf("\n");
p=p+2;
}
}
现在我的问题是:输出显示如下
1
121
12341
1234561
123456781
通过上面的代码,我可以将值 one 显示为每一行的第一个和最后一个位置。但是我无法在每行的第一个和最后一个位置完成中间位置的值的模式。针对这个特定问题,网上有大量的解决方案。但我只想自己找出解决方案。这就是为什么我在这里接受你的一些建议。提前谢谢你
【问题讨论】:
-
好的,所以你要么需要从你正在打印的位置计算值,要么如果你想增加和减少
value,那么你需要切换从value=value+1(也可以写成++value或value++)到value=value-1。您需要决定递增或递减的逻辑是什么?你能实现吗? -
我想决定增量。我在尝试。 @Rup
-
我不明白这个问题。如果“行数”输入多于一位,那么底行应该是什么样子?如果我输入11,输出是否应该以123456789101110987654321结尾?还是最多允许 9 个?
-
九不是允许的最大值。如果输入 11,输出将以 123456789101110987654321.@Howlium 结尾
-
“我只想自己找出解决方案”——那你为什么要我们为你做呢?小心@Rup,这听起来像是一项大学作业,我们被要求代表提问者完成。
标签: c loops for-loop while-loop printf