【 聲明:版權所有,歡迎轉載,請勿用於商業用途。 聯系信箱:feixiaoxing @163.com】
學過編程的朋友都知道,當初為了學習編程語言中的各種語法結構,我們要試著解決各種各樣奇怪的題目。其中“數星星”就似乎其中的一種。什麼是“數星星”呢?就是打印各種形狀的“*”,正三角、倒三角、菱形等等。本篇博客純粹為了紀念我們逝去的歲月。
a)正三角
void star_1()
{
int outer;
int inner;
for(outer = 1; outer <10; outer ++){
for(inner = 1; inner <= outer; inner ++){
printf("%c ", '*');
}
printf("\n");
}
}
void star_1()
{
int outer;
int inner;
for(outer = 1; outer <10; outer ++){
for(inner = 1; inner <= outer; inner ++){
printf("%c ", '*');
}
printf("\n");
}
} b)倒三角
void star_2()
{
int outer;
int inner;
for(outer = 9; outer >= 1; outer --){
for(inner = 1; inner <= outer; inner ++){
printf("%c ", '*');
}
printf("\n");
}
}
void star_2()
{
int outer;
int inner;
for(outer = 9; outer >= 1; outer --){
for(inner = 1; inner <= outer; inner ++){
printf("%c ", '*');
}
printf("\n");
}
} c)左三角
void star_3()
{
int outer;
int inner;
for(outer = -4; outer <= 4; outer ++){
for(inner = 1; inner <= (5 - abs(outer)); inner ++){
printf("*");
}
for(; inner <= 5; inner ++){
printf(" ");
}
printf("\n");
}
}
void star_3()
{
int outer;
int inner;
for(outer = -4; outer <= 4; outer ++){
for(inner = 1; inner <= (5 - abs(outer)); inner ++){
printf("*");
}
for(; inner <= 5; inner ++){
printf(" ");
}
printf("\n");
}
} d) 右三角
void star_4()
{
int outer;
int inner;
for(outer = -4; outer <= 4; outer ++){
for(inner = 1; inner <= abs(outer); inner ++){
printf(" ");
}
for(; inner <= 5; inner ++){
printf("*");
}
printf("\n");
}
}
void star_4()
{
int outer;
int inner;
for(outer = -4; outer <= 4; outer ++){
for(inner = 1; inner <= abs(outer); inner ++){
printf(" ");
}
for(; inner <= 5; inner ++){
printf("*");
}
printf("\n");
}
} e)垂直菱形
void star_5()
{
int outer;
int inner;
for(outer = -4; outer <= 4; outer ++){
for(inner = -4; inner <= abs(outer) -5; inner ++){
printf(" ");
}
for(; inner <= 5 - abs(outer); inner ++){
printf("*");
}
printf("\n");
}
}
void star_5()
{
int outer;
int inner;
for(outer = -4; outer <= 4; outer ++){
for(inner = -4; inner <= abs(outer) -5; inner ++){
printf(" ");
}
for(; inner <= 5 - abs(outer); inner ++){
printf("*");
}
printf("\n");
}
} f)水平菱形
void star_6()
{
int outer;
int inner;
for(outer = -9; outer <= 0; outer ++){
for(inner = outer; inner < 0; inner ++){
printf(" ");
}
for(; inner <= 9; inner ++){
printf("*");
}
printf("\n");
}
}
void star_6()
{
int outer;
int inner;
for(outer = -9; outer <= 0; outer ++){
for(inner = outer; inner < 0; inner ++){
printf(" ");
}
for(; inner <= 9; inner ++){
printf("*");
}
printf("\n");
}
}
g)楊輝三角形
void star_7()
{
int outer;
int inner;
for(outer = 1; outer <10; outer ++){
for(inner = 1; inner <= outer; inner ++){
printf("%3d ", outer * inner);
}
printf("\n");
}
}
void star_7()
{
int outer;
int inner;
for(outer = 1; outer <10; outer ++){
for(inner = 1; inner <= outer; inner ++){
printf("%3d ", outer * inner);
}
printf("\n");
}
}