我应该在哪个循环中放置打印功能?

时间:2015-03-01 15:01:59

标签: c

我是编程的初学者。我和c一起工作。我的程序应该产生这样的东西:

> Give me a number: 4
> Pattern is:
**** *
*** **
** ***
* ****

我的解决方案是:

#include <stdio.h>

int main()
{
   int row, c, n, t, temp;

   printf("Give me a number: ");
   scanf("%d",&n);

   temp = n;


   for ( row = 0 ; row <= n ; row++ )
   {
      for ( c = 0 ; c < temp ; c++ ){
         printf("*");

      }

      for ( t = n ; t >= temp ; t-- ){
         printf(" ");
         printf("*");
      }

      temp--;

      printf("\n");
   }

   return 0;
}

当我输入4作为输入时,我的代码产生了这个:

**** *
*** * *
** * * *
* * * * *
 * * * * *

我应该在哪里放置printf(" ");以解决此问题?

谢谢。

6 个答案:

答案 0 :(得分:1)

printf(" ");放在第二个for循环之外(之前)。

for ( row = 0 ; row < n ; row++ ) // Change row <= n to row < n 
{
     for ( c = 0 ; c < temp ; c++ ){
         printf("*");
     }
     printf(" "); // Out side the loop
     for ( t = n ; t >= temp ; t-- ){
         printf("*");
     }

     temp--;
     printf("\n");
}

答案 1 :(得分:1)

两件事。首先移动printf("")外面的第二个for循环,然后将n-1添加到first for循环。

  ...
  for ( row = 0 ; row <= n-1 ; row++ ) //<---- fix
  {
      for ( c = 0 ; c < temp ; c++ ){
      printf("*");
  }

  printf(" "); //<----- fix
  for ( t = n ; t >= temp ; t-- ){
     printf("*");
  }
  ...

答案 2 :(得分:1)

  

以下代码是建议的修复程序。此代码适用于   如果用户输入

,则允许所有值      

注意用户输入内容的限制请注意错误检查   用户输入和scanf()

     

注意评论的用法,以清楚地表明正在执行的内容   在该计划的每一步。

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int row, c, temp;
   int n = 0; // user input number
   int t = 0; // number of trailing * to print

   while( (n<1) || (n >50) )
   {

       printf("Give me a number (1...50): ");

       if( 1 != scanf("%d",&n) )
       { // then scanf failed
           perror( "scanf for number failed" );
           exit( EXIT_FAILURE );
       } 

       // implied else, scanf successful

       if( (n<1) || (n >50) ) printf( "\n number not in range 1...50\n");
   } // end while

   temp = n;


   for ( row = 0 ; row < n ; row++ )
   { // fore each row

        // calculate number of leading * to pring
        temp = n - row;

        // print leading *
        for ( c = 0 ; c < temp ; c++ )
        { // for each * before space
            printf("*");
        }

        // print space
        printf(" ");

        // calculate number of trailing * to print
        t++;

        for ( c = 0; c < t; c++ )
        {
            printf("*");
        }

        printf("\n");
    } // end for

    return 0;
} // end function main

答案 3 :(得分:0)

循环之前

#include <stdio.h>                                                             

int main()                                                                     
{                                                                              
   int row, c, n, t, temp;                                                     

   printf("Give me a number: ");                                               
   scanf("%d",&n);                                                             

   temp = n;                                                                   


   for ( row = 0 ; row <= n ; row++ )                                          
   {                                                                           
      for ( c = 0 ; c < temp ; c++ ){                                          
         printf("*");                                                          

      }                                                                        

      printf(" ");                                                             
      for ( t = n ; t >= temp ; t-- ){                                         
         printf("*");                                                          
      }                                                                        

      temp--;                                                                  

      printf("\n");                                                            
   }                                                                           

   return 0;                                                                   
} 

答案 4 :(得分:0)

printf(" ")应该在for ( t = n ; t >= temp ; t-- )和之前  for ( row = 0 ; row <= n ; row++ )应更改为for ( row = 0 ; row < n ; row++ )

您的代码应该是这样的:

#include <stdio.h>

int main()
{
   int row, c, n, t, temp;

   printf("Give me a number: ");
   scanf("%d",&n);

   temp = n;

   for ( row = 0 ; row < n ; row++ )
   {
      for ( c = 0 ; c < temp ; c++ ){
         printf("*");

      }

      printf(" ");

      for ( t = n ; t >= temp ; t-- ){
         printf("*");
      }

      temp--;

      printf("\n");
   }

   return 0;
}

答案 5 :(得分:0)

使用此:

int main()
{        
    int tmp;
    printf("\n");
    scanf("%d",&tmp);
    for ( int i = 0 ; i < tmp ; i++ )
    {           
        for (int j = 0; j <= tmp-i; j++)
            printf("*");
        printf(" ");

        for (int j = 0; j <= i; j++)
            printf("*");
        printf("/n");           
    }
    return 0;
}