【问题标题】:Can you expand my half solution for a specific problem in c?您能否针对 c 中的特定问题扩展我的半解决方案?
【发布时间】: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(也可以写成++valuevalue++)到value=value-1。您需要决定递增或递减的逻辑是什么?你能实现吗?
  • 我想决定增量。我在尝试。 @Rup
  • 我不明白这个问题。如果“行数”输入多于一位,那么底行应该是什么样子?如果我输入11,输出是否应该以123456789101110987654321结尾?还是最多允许 9 个?
  • 九不是允许的最大值。如果输入 11,输出将以 123456789101110987654321.@Howlium 结尾
  • “我只想自己找出解决方案”——那你为什么要我们为你做呢?小心@Rup,这听起来像是一项大学作业,我们被要求代表提问者完成。

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


【解决方案1】:

这是我对问题的解决方案。 我将我们正在打印的行作为基准来记录正在打印的行的中间值。最大值将与行相同,即第一行的最高值为 1,第二行的值为 2,依此类推。因此,对于您的“无法完成中间位置值的模式”是这样的。从 1 开始,将行值设为最大值,然后从那里递减。

下面是相同的实现。

#include <stdio.h>


int main() {
   int i, space, rows,j;


   printf("Enter the number of rows: ");
   scanf("%d", &rows);


   for (i = 1; i <= rows; ++i) {
      for (space = 1; space <= rows - i; ++space) {
         printf(" ");
   
      }
//Using the row we on as the benchmark for the values we need to go to
      for(j=1;j<i;j++)
         printf("%d",j);
//Handling the case of first row
      if(i==1){
         printf("1\n");
         continue;
      }
//Printing the Decrement
      for(;j>=1;j--)
         printf("%d",j);
      printf("\n");
   }
   return 0;
}

【讨论】:

  • 感谢您的协助。@Mohit Sharma
  • 不用担心。如果注意到你有空格,所以我没有解释。 (如果您对解决方案感到满意,您可以接受答案)
  • 好的,我已经使用了解决方案。但我会再试一次你的解决方案。谢谢你。 @Mohit Sharma
【解决方案2】:

您使用 if-else 语句的解决方案太复杂了。

例如这个循环

for(int j=1; j<=p; j++){

当条件

j<=p

由于这个语句而在循环内改变

p=p+2;

使程序不可读。

我可以提出一个更简单的解决方案。

你来了。

#include <stdio.h>

int main(void) 
{
    const unsigned int UPPER_BOUND = 10;
    
    while ( 1 )
    {
        printf( "Please enter the number of rows less than %u (0 - exit): ", UPPER_BOUND );
        
        unsigned int n;
        
        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
        
        if ( !( n < UPPER_BOUND ) ) n = UPPER_BOUND - 1;
        
        putchar( '\n' );

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

        putchar( '\n' );
    }

    return 0;
}

程序输出可能看起来像 '

Please enter the number of rows less than 10 (0 - exit): 5

    1
   121
  12321
 1234321
123454321

Please enter the number of rows less than 10 (0 - exit): 10

        1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321

Please enter the number of rows less than 10 (0 - exit): 0

【讨论】:

  • 先生,我明白了。感谢您的协助。来自莫斯科的@Vlad
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多