首页 > 程序开发 > 软件开发 > C语言 >

一步一步写算法(之“数星星”)

2011-11-01

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 学过编程的朋友都知道,当初为了学习编程语言中的各种语法结构,我们要试着解决各种各样奇怪的题目。其中数星星就似乎其...

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱: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 ", &#39;*&#39;);

}

printf("\n");

}

}

void star_1()

{

int outer;

int inner;

for(outer = 1; outer <10; outer ++){

for(inner = 1; inner <= outer; inner ++){

printf("%c ", &#39;*&#39;);

}

printf("\n");

}

} b)倒三角

void star_2()

{

int outer;

int inner;

for(outer = 9; outer >= 1; outer --){

for(inner = 1; inner <= outer; inner ++){

printf("%c ", &#39;*&#39;);

}

printf("\n");

}

}

void star_2()

{

int outer;

int inner;

for(outer = 9; outer >= 1; outer --){

for(inner = 1; inner <= outer; inner ++){

printf("%c ", &#39;*&#39;);

}

printf("\n");

}

} c)左三角&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;&#65279;

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");

}

}

相关文章
最新文章
热点推荐