#8
by かずま » 5年前
ループの中に if文があるのが気に食わないので書き直してみました。
コード:
#include <stdio.h>
void plot(int c, int n) { while (--n >= 0) putchar(c); }
void draw(int x)
{
int s = x*x, t = x*2, i, j, k;
for (i = 0; i < x; i++)
for (j = 1; j < t; j += 2) {
plot('.', --s), plot('*', j);
for (k = i; --k >= 0; ) plot('.', t-j), plot('*', j);
plot('.', s), putchar('\n');
}
}
int main(void)
{
for (int x = 2; x <= 4; x++) printf("\n x = %d\n", x), draw(x);
}
解は一つではありません。いろいろな解答を考えてみましょう。
ループの中に if文があるのが気に食わないので書き直してみました。
[code]
#include <stdio.h>
void plot(int c, int n) { while (--n >= 0) putchar(c); }
void draw(int x)
{
int s = x*x, t = x*2, i, j, k;
for (i = 0; i < x; i++)
for (j = 1; j < t; j += 2) {
plot('.', --s), plot('*', j);
for (k = i; --k >= 0; ) plot('.', t-j), plot('*', j);
plot('.', s), putchar('\n');
}
}
int main(void)
{
for (int x = 2; x <= 4; x++) printf("\n x = %d\n", x), draw(x);
}
[/code]
解は一つではありません。いろいろな解答を考えてみましょう。