如何在c程序中使用循环制作圣诞树

时间:2014-10-05 15:44:05

标签: c

我是一名新生,我们在介绍中有一项活动。我们的任务是使用循环创建一棵圣诞树......

我的代码在这里:

#include<stdio.h>
int main ()
{
    int rows,a,b,space;
    clrscr();
    printf("Enter a number of rows:");
    scanf("%d",&rows);
    space=rows-1
    for(b=space;b>=1;b--)
    {
        for(a=rows;a>=1;a--)
            space--;
        printf("");
        for(a=2*(rows-b)-1;a>=1;a--)
            printf("*",a);
        printf("\n");
        space = space-1;
    }
    getche();
    return 0;
}

这段代码是由我们的教授给我们的...程序运行,但输出错误。你能救我吗?

当我运行这个程序时,输出是这样的:

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

9 个答案:

答案 0 :(得分:1)

你必须找到一个模式。假设您想要一个n行的树。最后一行将有2n-1颗星。它之前的行将有2n-3等等。要打印一行,首先打印一些空格,然后打印一些星号。对于最后一行,您可以打印0个空格和2n-1个星号。对于之前的行,您可以打印1空格和2n-3星等等。

for(int i = 0; i < n; i++)
{   for(int j = i + 1; j < n; j++)
       printf(" ");
    for(int j = 0; j <= 2*i; j++)
       printf("*");
    if(i < n - 1) puts("");
}

答案 1 :(得分:0)

“守则”对我来说有点混乱,但这应该有效:

#include<stdio.h>

int main() {

    /*Variables*/
    int rows, starNumber, spaceNumber;
    int rowCount, spaceCount, starCount, treeTrunkCount, treeTrunkSpaceCount;

    printf("Enter Rows:\n>");
    scanf("%d",&rows);

    for(rowCount = 1; rowCount <= rows; rowCount++) {
        starNumber = rowCount * 2 - 1;
        spaceNumber = rowCount + rows - starNumber;

        for(spaceCount = 0; spaceCount < spaceNumber; spaceCount++)
            printf(" ");

        for(starCount = 0; starCount < starNumber; starCount++)
            printf("%c",'*');

        printf("\n");
    }

    for(treeTrunkCount = 0; treeTrunkCount < 3; treeTrunkCount++) {
        for(treeTrunkSpaceCount = 0; treeTrunkSpaceCount < (rows * 2 + 1)/2; treeTrunkSpaceCount++)
            printf(" ");

        printf("%c\n",'*');
    }
}

答案 2 :(得分:0)

这是您的计划最简单的解决方案..

#include <stdio.h>

int main()
{
  int i=-1,j=0,rows;

  printf("Enter Rows:\n");
  scanf("%d",&rows);

  while(j++<rows) // Moving pointer for the first '*'
  {
    printf(" ");
  }
  printf("*"); // This prints the first '*'
  while(++i<rows)
  {
      for(j=-2;++j<rows-i;) // This loop will print Spaces before '*' on each row
          printf(" ");
      for(j=0;++j<2*i;) // This loop will print * on each row
      {
          printf("*");
      }
      printf("\n"); // This printf will take you to the next Line
    }
}

答案 3 :(得分:0)

这是您问题的最简短,最简单的解决方案:

#include<stdio.h>
#include<conio.h>

void main(){
    int count;
    int i,j;
    printf("enter the numbers of line");
    scanf("%d",&count);
    for(i=1;i<=count;i++){
        for(j=1;j<=i;j++){
            printf("*");
        }
        printf("\n");
    }
    getch();
}

答案 4 :(得分:0)

你忘了“”之间的空格。

    for(a=rows;a>=1;a--)
        space--;
    printf("");

应该是

    for(a=rows;a>=1;a--)
        space--;
    printf(" ");

答案 5 :(得分:0)

#include <stdio.h>


int main() {
    int n = 50;

    for (int i = 0; i <= n; ++i) {

        for (int k = i; k < n; ++k)
            printf(" ");

        for (int j = 0; j < i; ++j)
            printf("*");

        for (int j = 1; j < i; ++j)
            printf("*");

        printf("\n");
    }

    for (int l = 1; l < n/2; ++l) {
        for (int i = 1; i < n; ++i)
            printf(" ");

        printf("[|]\n");

    }

    return 0;
}

答案 6 :(得分:0)

可以用 for循环组成一棵简单的树,圣诞节可能需要更多符号...

//Linux C program to print a tree
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <unistd.h> 
#include <sys/ioctl.h>
#include <string.h> 
  
int pcenter(char *s) {
        struct winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        int ct = w.ws_col;
        int sl = strlen(s) / 2;
        printf("%*s%*s\n", ct / 2 + sl, s, ct / 2 - sl, "");
        return 0;
}

int ptree(char s, char t, int l, int r) {
        int i;
        for (i = 1; i <= l; i++) {
                int j = 2 * i - 1;
                char *p = malloc(j);
                memset(p, s, j);
                pcenter(p);
                free(p);
        }
        for (i = 1; i <= r; i++) {
                int j = 1;
                char *p = malloc(j);
                memset(p, t, j);
                pcenter(p);
                free(p);
        }
        return 0;
}

int main() { 
//      system("clear"); 
        ptree('*', '|', 10, 5);
        return 0; 
} 

答案 7 :(得分:-1)

#include<stdio.h>
main()
{
    int n,i, j, space=1;
    printf("Enter the number of rows: ");
    scanf("%d",&n);
    space=n-1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=space;j++)
       {
            printf(" ");
        }
        space--;
        for(j=1;j<=2*i-1;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    for(i=1;i<=n-3;i++)
    {
        for(j=1;j<=10;j++)
        {
            printf(" ");
        }
         for(j=1;j<=1;j++)
        {
           printf("*");
        }
         for(j=1;j<=1;j++)
        {
            printf("*");
        }
         for(j=1;j<=1;j++)
        {
            printf("*");
        }
        printf("\n");
    }
}

答案 8 :(得分:-1)

#include<stdio.h>
int main()
{
    int i,j,k,l=1,a,b;
    for(i=8;i>=0;i--)
        {
            for(j=0;j<=i;j++)
                {
                printf(" ");
                }
                k=0 ;
            while(k<l)
                {
                printf("*");
                k=k+1;
                }
            l=l+2;
            printf("\n");
        }
    i=8;
    for(b=0;b<=3;b++)
        {
        for(a=0;a<=i-1;a++)
            {
            printf(" ");
            }
        printf("***"); 
        printf("\n");
        }
}