close
標題:

請問C語言的菱形怎麼寫(星星)

發問:

例如 Input size n=3, 出現以下圖形(size為其邊長) ............* ..........*** ........***** ..........*** ............* (雅虎這個輸出來怪怪的...不過大概能了解我意思) 最好能解釋一下, 拜託各高手囉......

最佳解答:

aa.jpg

 

此文章來自奇摩知識+如有不便請留言告知

#include#includeint main(){? int i,j,n,w;?printf("Input size n=");? scanf("%d",&n);? w=2*n+1;? for(i=1;i<=w;i++)? {?? for(j=1;j<=w;j++)??? if( abs(i-n)+abs(j-n) < n )//以(n,n)為中點.算i,j分別和n其差之絕對值和 ???? printf("*");??? else???? printf(" ");??printf(" ");? }? return 0;} //可以對菱形中央做x,y軸就知道這程式怎麼跑n=3的情況,是否有看到小於3的部份形成菱形.圖中數字就是和中央的絕對值距離(x差+y差)4 3 2 3 43 2 1 2 32 1 0 1 23 2 1 2 34 3 2 3 4

其他解答:
arrow
arrow

    ute40zf07t 發表在 痞客邦 留言(0) 人氣()