在C

时间:2017-03-25 05:55:35

标签: c triangle-count

我是C的新手,我有这个程序,我试图根据它的高度打印一个三角形:

 /\
/__\

因此,如果高度为2,则有2个' /',' \'和' _'。

所以我写了这些代码:

#include <stdio.h>
int main(void)
{
    int noOfRows; 
    printf("Enter height: ");
    scanf("%d", &noOfRows);

    int counter, rowNumber;
    for (rowNumber = 0 ; rowNumber < noOfRows; rowNumber++)
    {
        // For each row, insert numberOfRows - N spaces before printing
        for (counter = 0 ; counter < noOfRows- rowNumber ; counter++)
            printf(" ");

        // For each row, print N times the character
            printf("/");

        for (counter = 0; counter < noOfRows; counter++)
            printf("\\");

        printf(" ");

        printf("\n");
    }

    return 0;
}

然而,当我输入3作为高度时,它会给我一个输出。

  /\\\
 /\\\
/\\\

我想得到&#39; \&#39;每个新行出现时有更多空间,但我不确定如何修改for循环,以便正确形成三角形。请告诉我是否应该再添加问题以使其更清晰。

我对代码进行了更改,现在有了这个:

#include <stdio.h>
int main(void)
{
    int noOfRows; 
    printf("Enter height: ");
    scanf("%d", &noOfRows);

    int counter, rowNumber;
    for (rowNumber = 0 ; rowNumber < noOfRows; rowNumber++)
    {
        // For each row, insert numberOfRows - N spaces before printing
        for (counter = 0 ; counter < noOfRows- rowNumber ; counter++)
            printf(" ");

        // For each row, print N times the character
            printf("/");

        for (counter = 0 ; counter < rowNumber ; counter++)
            if(rowNumber >= 1)  
                for(int j=0; j<counter +1; j++)
                    printf(" ");

        if(rowNumber != 0)
            printf(" ");

            printf("\\");

        printf("\n");
    }

    return 0;
}

现在我的当前输出是这样的:

Height: 3
  /\
 /  \
/    \

Height: 5

    /\
   /  \
  /    \
 /       \
/           \

我在制作一些&#39; \&#39;的代码中做错了什么?走得更远?

4 个答案:

答案 0 :(得分:1)

@ Barmar的想法是对的,你不再需要 if(rowNumber != 0) printf(" ");

Fullcode:

#include <stdio.h>
int main(void)
{
    int noOfRows; 
    printf("Enter height: ");
    scanf("%d", &noOfRows);
    int counter, rowNumber;
    for (rowNumber = 0 ; rowNumber < noOfRows; rowNumber++)    {

        for (counter = 0 ; counter < noOfRows- rowNumber ; counter++)
            printf(" ");        
            printf("/");
        for (counter = 0 ; counter < rowNumber ; counter++)
            if(rowNumber >= 1)  
                for (counter = 0; counter < rowNumber*2; counter++) {
        if (rowNumber == noOfRows-1) {
        printf("_");
        } else {
        printf(" ");
        }
        }
        printf("\\");
        printf("\n");
    }

}

答案 1 :(得分:1)

您只能使用两个循环来使用if else打印此模式。在if else中,您可以对空格,反斜杠,正斜杠和下划线使用多个条件。

1 #include<stdio.h>
  2 
  3 int main()
  4 {
  5         int n;
  6         int i,j,k;
  7         printf("Enter hight of trangle\n");
  8         scanf("%d",&n);
  9         for(i=0;i<n;i++,printf("\n"))
 10         {
 11                 for(j=-n;j<=n;j++)
 12                 {
 13                         if(j<0) k=-j;
 14                         else    k=j;
 15 
 16                         if(j==0);
 17                         else if((j<0)&&(k==(i+1)))
 18                                 printf("/");
 19                         else if((j>0)&&(k==(i+1)))
 20                                 printf("\\");
 21                         else if((i+1)==n)
 22                                 printf("_");
 23                         else
 24                                 printf(" ");
 25                 }
 26         }
 27 }

答案 2 :(得分:0)

您不需要两个嵌套循环来打印/\\之间的空格。它应该只打印rowNumber*2个空格。

for (counter = 0; counter < rowNumber*2; counter++) {
    printf(" ");
}

您无需测试if (rowNumber >= 1),因为当0循环立即结束时。

但是,您需要检查最后一行,因为您应该打印_而不是空格。所以它应该是:

for (counter = 0; counter < rowNumber*2; counter++) {
    if (rowNumber == noOfRows-1) {
        printf("_");
    } else {
        printf(" ");
    }
}

答案 3 :(得分:0)

或者,您可以将字符的重复打印抽象为:

void print_triangle(int height) {
  for (int i = 1; i <= height; ++i) {
    print_char(height-i, ' ');               // print leading empty spaces
    printf("/");                             // print forward slash
    if (i == height) {                       // print middle dashes/spaces
      print_char((height-1)*2, '_');         //             <--|      |
    } else {                                 //                       |
      print_char((i-1)*2, ' ');              //             <---------|
    }
    printf("\\\n");                          // print backslash
  }
}

这样你只需要在构造三角形时跟踪一个for循环:

int main(void) {
  int height;
  printf("Enter height: ");
  if (scanf("%d", &height) != 1) {
    // handle error if scanf fails
  }
  print_triangle(height);
  return 0;
}

例如,

$ Enter height: 1
/\
$ Enter height: 5
    /\
   /  \
  /    \
 /      \
/________\

然后,

DetachedMockFactory