读书人

第一回做c语言作业想了很久但不会

发布时间: 2013-03-04 17:22:12 作者: rapoo

第一次做c语言作业,想了很久但不会。
.编写程序,在屏幕上显示下列图形:
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
用for或while,do-while循环语句实现,求指导,求解。
[解决办法]

#include <iostream>

using namespace std;

int main()
{
for (int i = 9; i > 0; i--) {
for (int j = 0; j < i; j++)
cout << '*';
cout << endl;
}
return 0;
}

[解决办法]

#include <stdio.h>

int main(void)
{
int line;
int i;

for(line = 0; line < 9; line++)
{
for(i = 9; i > line; i--)
{
printf("*");
}
printf("\n");
}

return 0;
}

[解决办法]
#include <stdio.h>

//使用宏定义来替换行数和列数
#define ROW 9
#define COL 9

//定义成一个函数
void show_shape(void);

int main(void)
{
show_shape();
return 0;
}

void show_shape(void)
{
int row;
int col;
for(row = 0; row < ROW; row++) //行数
{
for(col = COL; col > row; col--) //列数,根据行数和列数的对应关系
{
printf("*"); //putchar('*');
}
printf("\n");
}
}

读书人网 >C++

热点推荐