怎样能提高程序的效率?

efuliang 2002-10-26 11:26:28
*
***
*****
*******
*********
*******
*****
***
*

要再c++中输出该程序,我是这么编的:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
main()
{
int i,j,b,c,d,e;
char a;
a='*';
b=5,c=2,d=1,e=7;
for(i=1;i<=5;i++)
{
cout<<setw(b);
for(j=1;j<c;j++)
cout<<a;
b--;
c=c+2;
cout<<endl;
}
b=2;
c=7;
for(i=1;i<=5;i++)
{
cout<<setw(b);
for(j=1;j<=c;j++)
cout<<a;
b++;
c=c-2;
cout<<endl;
}
getch();
}

请问各位,还有没有使它精简的办法来提高效率?
...全文
16 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lonelybug 2002-10-26
  • 打赏
  • 举报
回复
用两个想歉套的循环就可以了!
八for改称while(i<10)
然后里面加一个if语句判断,
if i>5 then
{
cout<<setw(b);
for(j=1;j<=c;j++)
cout<<a;
b++;
c=c-2;
cout<<endl;
}
blh 2002-10-26
  • 打赏
  • 举报
回复
//高效算法,呵呵
#include <iostream>

static char *stars[] =
{
" *",
" ***",
" *****",
" *******",
"*********",
" *******",
" *****",
" ***",
" *",
NULL
};

int main()
{
int i = 0;
while(stars[i])
{
out << stars[i++] << endl;
}
return 0;
}
efuliang 2002-10-26
  • 打赏
  • 举报
回复
图象打错了,应该是:
*
***
*****
*******
*********
*******
*****
***
*

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧