#6
by かずま » 7年前
box さんが書きました:実行後、偶数の整数を入力してみてください。
なぜ、偶数しかダメなんですか?
段数と考えたら、奇数でもよいと思うんですが。
コード:
#include <stdio.h>
void triangle(int n)
{
for (int y = n; y > 0; y--)
for (int x = 1 - n; x <= n; x++)
putchar(x >= y - n && x <= n - y ? '*' : " \n"[x==n]);
}
int main(void)
{
for (int n = 1; n <= 5; n++)
printf("[%d]\n", n), triangle(n);
return 0;
}
実行結果
コード:
[1]
*
[2]
*
***
[3]
*
***
*****
[4]
*
***
*****
*******
[5]
*
***
*****
*******
*********
でも、これは三角形を表示するプログラムですよね。
問題は、ピラミッドを表示するプログラムとなっています。
コード:
#include <stdio.h> // printf
#include <string.h> // memset
#include <math.h> // sin, cos
#define T1 25
#define T2 75
#define PI 3.141592653589793238
#define Cos(t) cos(PI/180 * (t))
#define Sin(t) sin(PI/180 * (t))
char a[24][80];
double w;
void init(void) { memset(a, ' ', sizeof a); w = 0; }
void display(void)
{
for (int i = 24; --i >= 0; )
printf("%.79s\n", a[i]);
}
void plot(double x, double y)
{
unsigned i = y / w * 24, j = x / w * 60;
if (i < 24 && j < 80) a[i][j] = '*';
}
void line(double x1, double y1, double x2, double y2)
{
for (int i = 0; i <= 64; i++)
plot((x1*i + x2*(64-i))/64, (y1*i + y2*(64-i))/64);
}
void piramid(double base, double height)
{
double x[5], y[5];
init();
x[1] = 0, y[1] = base * Sin(T1);
x[2] = base * Cos(T1), y[2] = 0;
x[3] = x[2] + base * Cos(T2), y[3] = base * Sin(T2);
x[4] = base * Cos(T2), y[4] = y[1] + base * Sin(T2);
x[0] = x[3] / 2, y[0] = y[4] / 2 + height;
w = y[0] > y[4] ? y[0] : y[4];
line(x[0], y[0], x[1], y[1]);
line(x[0], y[0], x[2], y[2]);
line(x[0], y[0], x[3], y[3]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[3], y[3]);
if (y[0] < y[4]) {
line(x[0], y[0], x[4], y[4]);
line(x[3], y[3], x[4], y[4]);
line(x[4], y[4], x[1], y[1]);
}
display();
}
int main(void)
{
piramid(10, 3); // 底辺 10、高さ 3 のピラミッド
piramid(10, 7); // 底辺 10、高さ 7 のピラミッド
return 0;
}
実行結果
コード:
*******
** *********
* *** ******
** *** ******
** *** *****
* *** ******
* ************ ******
** ***** ****************
* **** * *
** *** * **
** *** ** **
* *** ** *
* *** ** **
** *** * **
* *** * *
**** ** **
** ** **
****** ** *
****** ** **
****** * **
****** * *
****** ** **
****** ****
******
******
** ** ****
** * ****
** * ****
** * ****
** * ****
*** * ****
** * ***
** ** *
** * *
** ** **
** ** *
*** * **
** ** **
** * *
** ** *
** * **
****** * *
****** * **
****** * **
****** ** *
****** * *
****** ****
******
冗談です。
[quote="box" id=3,19933,149587]実行後、偶数の整数を入力してみてください。[/quote]
なぜ、偶数しかダメなんですか?
段数と考えたら、奇数でもよいと思うんですが。
[code=c]
#include <stdio.h>
void triangle(int n)
{
for (int y = n; y > 0; y--)
for (int x = 1 - n; x <= n; x++)
putchar(x >= y - n && x <= n - y ? '*' : " \n"[x==n]);
}
int main(void)
{
for (int n = 1; n <= 5; n++)
printf("[%d]\n", n), triangle(n);
return 0;
}
[/code]
実行結果
[code=text]
[1]
*
[2]
*
***
[3]
*
***
*****
[4]
*
***
*****
*******
[5]
*
***
*****
*******
*********
[/code]
でも、これは三角形を表示するプログラムですよね。
問題は、ピラミッドを表示するプログラムとなっています。
[code=c]
#include <stdio.h> // printf
#include <string.h> // memset
#include <math.h> // sin, cos
#define T1 25
#define T2 75
#define PI 3.141592653589793238
#define Cos(t) cos(PI/180 * (t))
#define Sin(t) sin(PI/180 * (t))
char a[24][80];
double w;
void init(void) { memset(a, ' ', sizeof a); w = 0; }
void display(void)
{
for (int i = 24; --i >= 0; )
printf("%.79s\n", a[i]);
}
void plot(double x, double y)
{
unsigned i = y / w * 24, j = x / w * 60;
if (i < 24 && j < 80) a[i][j] = '*';
}
void line(double x1, double y1, double x2, double y2)
{
for (int i = 0; i <= 64; i++)
plot((x1*i + x2*(64-i))/64, (y1*i + y2*(64-i))/64);
}
void piramid(double base, double height)
{
double x[5], y[5];
init();
x[1] = 0, y[1] = base * Sin(T1);
x[2] = base * Cos(T1), y[2] = 0;
x[3] = x[2] + base * Cos(T2), y[3] = base * Sin(T2);
x[4] = base * Cos(T2), y[4] = y[1] + base * Sin(T2);
x[0] = x[3] / 2, y[0] = y[4] / 2 + height;
w = y[0] > y[4] ? y[0] : y[4];
line(x[0], y[0], x[1], y[1]);
line(x[0], y[0], x[2], y[2]);
line(x[0], y[0], x[3], y[3]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[3], y[3]);
if (y[0] < y[4]) {
line(x[0], y[0], x[4], y[4]);
line(x[3], y[3], x[4], y[4]);
line(x[4], y[4], x[1], y[1]);
}
display();
}
int main(void)
{
piramid(10, 3); // 底辺 10、高さ 3 のピラミッド
piramid(10, 7); // 底辺 10、高さ 7 のピラミッド
return 0;
}
[/code]
実行結果
[code=text]
*******
** *********
* *** ******
** *** ******
** *** *****
* *** ******
* ************ ******
** ***** ****************
* **** * *
** *** * **
** *** ** **
* *** ** *
* *** ** **
** *** * **
* *** * *
**** ** **
** ** **
****** ** *
****** ** **
****** * **
****** * *
****** ** **
****** ****
******
******
** ** ****
** * ****
** * ****
** * ****
** * ****
*** * ****
** * ***
** ** *
** * *
** ** **
** ** *
*** * **
** ** **
** * *
** ** *
** * **
****** * *
****** * **
****** * **
****** ** *
****** * *
****** ****
******
[/code]
冗談です。