用“#” 输出一个菱形 代码怎么写 我一直写不对。。。

你好哇哇 2013-10-24 04:50:43
#include<iostream.h>
int main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=10-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<"#";








cout<<endl;

}
}


只会写上半部分-。-
...全文
518 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
_lee_chong 2013-11-04
  • 打赏
  • 举报
回复
引用 18 楼 jiandingzhe 的回复:
[quote=引用 17 楼 lc316546079 的回复:] [quote=引用 7 楼 jiandingzhe 的回复:] 为什么要循环呢?有要求任意尺寸的菱形吗?

cout<<"  #  \n ### \n#####\n ### \n  #  \n";
[/quote] 这个没有循环,执行效率非常高。 实际情况很可能不需要多种尺寸的菱形,那么就连函数都不用:可以把几种尺寸的菱形硬编码到一个字符串数组里。[/quote] 不过这道题,还是难倒了一部分初学者的,它主要应该是让初学者初步感受到ui展示与算法吧,硬编码对初学者就没意义了嘛,
jiandingzhe 2013-11-03
  • 打赏
  • 举报
回复
引用 17 楼 lc316546079 的回复:
[quote=引用 7 楼 jiandingzhe 的回复:] 为什么要循环呢?有要求任意尺寸的菱形吗?

cout<<"  #  \n ### \n#####\n ### \n  #  \n";
[/quote] 这个没有循环,执行效率非常高。 实际情况很可能不需要多种尺寸的菱形,那么就连函数都不用:可以把几种尺寸的菱形硬编码到一个字符串数组里。
_lee_chong 2013-11-01
  • 打赏
  • 举报
回复
引用 7 楼 jiandingzhe 的回复:
为什么要循环呢?有要求任意尺寸的菱形吗?

cout<<"  #  \n ### \n#####\n ### \n  #  \n";
lm_whales 2013-10-31
  • 打赏
  • 举报
回复
可以分两步,一步上三角,一步下三角 注意 中间的那一行,只输出一次。
lty369963 2013-10-30
  • 打赏
  • 举报
回复
铁弹火车头 2013-10-30
  • 打赏
  • 举报
回复
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
int i,j,k;
int l; //边长
char str[26],ch;
for(i=0;i<26;i++)//这儿可以修改想要输出的字符!!!
str[i]='*';
cout<<"请输入菱形上三角行数:"<<endl ;
cin>>l;

for(k=1;k<=2*l-1;k++)
{
for(i=0;i<abs((2*l-1)/2+1-k)+15;i++)
cout<<" ";
ch=str[l-abs(k-l)-1];
for(j=0;j<2*((2*l-1)/2+1-abs((2*l-1)/2+1-k))-1;j++)
cout<<ch;
cout<<endl;
}
return 0;
}
huo_yi_2000 2013-10-29
  • 打赏
  • 举报
回复
//相当于把上半部分倒过来 #include<iostream.h> void main() { int i,j; for(i=1;i<=10;i++) { for(j=1;j<=10-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<"#"; cout<<endl; } for(i=10;i>=1;i--) { for(j=1;j<=10-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<"#"; cout<<endl; } }
blackjenny242 2013-10-29
  • 打赏
  • 举报
回复
哦 你自己运行下了 贴的结果和实际的不一样
blackjenny242 2013-10-29
  • 打赏
  • 举报
回复
# ### ##### ####### ######### ########### ############# ############### ################# ################### ################# ############### ############# ########### ######### ####### ##### ### # 请按任意键继续. . .
blackjenny242 2013-10-29
  • 打赏
  • 举报
回复
int main() { int i,j; for(i=1;i<=10;i++) { for(j=1;j<=10-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<"#"; cout<<endl; } for(i=9;i>=1;i--) { for(j=1;j<=10-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<"#"; cout<<endl; } }
hangang7403 2013-10-29
  • 打赏
  • 举报
回复
顶。。。。。。。。。。。。
lm_whales 2013-10-29
  • 打赏
  • 举报
回复
# # # # # # # # 还是 # ### ##### ### #
jiandingzhe 2013-10-27
  • 打赏
  • 举报
回复
为什么要循环呢?有要求任意尺寸的菱形吗?

cout<<"  #  \n ### \n#####\n ### \n  #  \n";
自信男孩 2013-10-26
  • 打赏
  • 举报
回复
引用 楼主 u012559300 的回复:
#include<iostream.h> int main() { int i,j; for(i=1;i<=10;i++) { for(j=1;j<=10-i;j++) cout<<" "; for(j=1;j<=2*i-1;j++) cout<<"#"; cout<<endl; } } 只会写上半部分-。-
写出了上半部分,那么下半部分就不是很难写了,就是把上三角改成下三角,改一下循环的初始条件,和退出条件。其实就是调换一下条件。
yingzi5580 2013-10-26
  • 打赏
  • 举报
回复
是正菱形还是?最好发图,因为要按照图样来输出。
lm_whales 2013-10-26
  • 打赏
  • 举报
回复
先把图发出来,再配上说明。
傻傻不解释 2013-10-24
  • 打赏
  • 举报
回复
菱形每行的"#"是一样多的
傻傻不解释 2013-10-24
  • 打赏
  • 举报
回复
for(i=1;i<=10;i++)
{
for(j=1;j<=10-i;j++)
cout<<" ";
for(j=1;j<=19;j++)
cout<<"#";








cout<<endl;

}
其实菱形好简单的 直接
max_min_ 2013-10-24
  • 打赏
  • 举报
回复
把上面的输出行的输出倒过来输出一次就好了!

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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