Re: [問題] C語言聖誕樹並排

看板C_and_CPP作者 (朱子)時間16年前 (2009/04/28 17:35), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
剛剛寫的,應該還有很多改進的空間 #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)
文章代碼(AID): #19zpv5kq (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19zpv5kq (C_and_CPP)