[C/C++ 演算法]-用for迴圈 排成特殊"菱形"
剛才逛論壇發現這一題目,並且也有人回答,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
#include <stdio.h>
int main (void) {
int i, j, N;
scanf ("%d", &N);
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++)
if (i <= N / 2)
if (j <= (N+1)/2-i) putchar (' ');
else if (j<=(N+1)/2) putchar ('*');
else if (j<=(N+1)/2+i-2) putchar (' ');
else if (j==(N+1)/2+i-1) putchar ('*');
else break;
else if (i == (N+1)/2)
putchar ('*');
else
if (j <= i-(N+1)/2) putchar (' ');
else if (j == i-(N+1)/2+1) putchar ('*');
else if (j < (N+1)/2) putchar (' ');
else if (j <= N-i+(N+1)/2) putchar ('*');
else break;
putchar ('\n');
}
return 0;
}
來源出處:http://www.eyny.com/thread-8967525-1-3D41XTMV.html
沒有留言:
張貼留言