作者mantour (朱子)
看板C_and_CPP
标题Re: [问题] C语言圣诞树并排
时间Wed Apr 29 01:34:59 2009
刚刚写的,应该还有很多改进的空间
#include<stdio.h>
void printspaces(int n)
{
int i;
for(i=0;i<n;i++)
{
putchar(' ');
}
}
void printstars(int n)
{
int i;
for(i=0;i<n;i++)
{
putchar('*');
}
}
void tree(int n,int h) //印出高h的tree的第n列
{
if(n<h)
{
printspaces(h-n-1);
printstars(2*n+1);
printspaces(h-n-1);
}
else if(n<h+(h-1)/2)
{
printspaces(h-1);
printstars(1);
printspaces(h-1);
}
else
{
printspaces(2*h-1);
}
}
int main()
{
int i, h[4];
printf("请输入你想要圣诞叶的高度(0~39且为奇数):");
scanf("%d,%d,%d",h+1,h+2,h+3);
h[0]=h[1];
for(i=2;i<4;i++) // 使h[0] = max(h[1],h[2],h[3])
{
if(h[0]<h[i])
{
h[0]=h[i];
}
}
for(i=0;i<h[0]+(h[0]-1)/2;i++)
{
tree(i,h[1]); //第一颗树的第i列
putchar(' '); //接一个空白
tree(i,h[2]); //接第二颗树的第i列
putchar(' '); //再接一个空白
tree(i,h[3]); //接第三颗树的第i列
putchar('\n'); //换行
}
return 0;
}
※ 引述《programming (复制自己)》之铭言:
: 实在想不通...不知道有没有大大可以提供一下意见...。
: #include<stdio.h>
: #include<stdlib.h>
: void tree(int x);
: int main()
: {
: int Height, Length, Width, Leaf, Stem, Index, Heightt, Heighttt;
: printf("请输入你想要圣诞叶的高度(0~39且为奇数):");
: scanf("%d,%d,%d",&Height,&Heightt,&Heighttt);
: tree(Height);
: tree(Heightt);
: tree(Heighttt);
: Width = Height*2-1;
: Length = Height/2;
: system("pause");
: return 0;
: }
: void tree(int x)
: {int Height, Length, Width, Leaf, Stem, Index, Heightt, Heighttt;
: Width = x*2-1;
: Length = x/2;
: if ((x > 0)&&(x < 40)&&(x%2 != 0))
: {
: for (Leaf = 0; Leaf < x; Leaf++)
: {
: for (Index = 1; Index <= Width; Index++)
: {
: if ((Index <= x+Leaf)&&(Index >= x-Leaf))
: printf("*");
: else
: printf("-");
: }
: printf("\n");
: }
: for (Stem = 0; Stem < Length; Stem++)
: {
: for (Index = 1; Index <= Width; Index++)
: {
: if (Index == x)
: printf("*");
: else
: printf("-");
: }
: printf("\n");
: }
: }
: return;
: }
: 可显示出三颗串联的圣诞树...
: 如图:
: --*--
: -***-
: *****
: --*--
: ----*----
: ---***---
: --*****--
: -*******-
: *********
: ----*----
: ----*----
: ------*------
: -----***-----
: ----*****----
: ---*******---
: --*********--
: -***********-
: *************
: ------*------
: ------*------
: ------*------
: 可是我想改成"并排"的三棵圣诞树,如图
: --*-- ----*---- ------*------
: -***- ---***--- -----***-----
: ***** --*****-- ----*****----
: --*-- -*******- ---*******---
: ********* --*********--
: ----*---- -***********-
: ----*---- *************
: ------*------
: ------*------
: ------*------
: 试了一天...还是改不出来QQ
: 怎麽会这样ˊˋ
: 有大大会的吗QQ?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.213.158
※ 编辑: mantour 来自: 140.112.213.158 (04/29 01:35)