Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
你能用c语言扩展我对一个特定问题的半解吗?_C_Loops_For Loop_While Loop_Printf - Fatal编程技术网

你能用c语言扩展我对一个特定问题的半解吗?

你能用c语言扩展我对一个特定问题的半解吗?,c,loops,for-loop,while-loop,printf,C,Loops,For Loop,While Loop,Printf,输入: 输入行数: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

输入:

输入行数: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显示为每一行的第一个和最后一个位置。但我无法在每行的第一个和最后一个位置内完成中间位置值的模式。对于这个特殊的问题,网上有大量的解决方案。但我只想自己找到解决办法。这就是为什么我在这里听取您的一些建议。提前谢谢您

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

下面是相同的实现

#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;
}
#包括
int main(){
int i,空间,行,j;
printf(“输入行数:”);
scanf(“%d”,行和行);

对于(i=1;i,使用if-else语句的解决方案太复杂了

例如,这个循环

for(int j=1; j<=p; j++){
使程序不可读

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

给你

#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;
}

好的,所以您需要从正在打印的位置计算值,或者如果您想在打印过程中增加和减少
value
,那么您需要从
value=value+1
(也可以写成
++value
value++
)to
value=value-1
。决定递增或递减需要什么逻辑?你能实现吗?我想决定递增。我正在努力。@RupI不理解这个问题。如果“行数”输入不止一位,最下面一行应该是什么样子?如果我输入11,输出应该以12345678910110987654321结尾?还是9是允许的最大值?9不是允许的最大值。如果输入11,输出将以12345678910110987654321结尾。@Howlium“我想自己找出解决方案”-那你为什么要求我们为你做这件事呢?小心@Rup,这闻起来像是一个大学作业,我们被要求代表询问者完成。谢谢你的帮助。@Mohit SharmaNo担心。如果注意到你已经计算出了空格,所以我没有解释。(如果你对解决方案满意,你可能会接受答案)好的,我已经坚持这个解决方案。但我会再试试你的解决方案。谢谢。@Mohit SharmaSir,我明白了。谢谢你的帮助。@Vlad,莫斯科人
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