钻石形状 [英] DIAMOND SHAPE

查看:89
本文介绍了钻石形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序,它以一个数字作为输入并打印一个#和$ $
钻石。形状中的行数等于用户输入的

数。我的程序应显示

的形状,偶数和奇数值的大小。例如,如果用户输入数字7

,程序应该打印以下形状。




#$#

#$#$#

#$#$#$#

#$#$#

#$#




如果用户输入值6,程序会在以下

形状下打印菱形。




#$#

#$#$#

#$#$#

#$#




我努力了,这是我能想到的最好的


#include< iostream>

使用命名空间std;


int main()

{

int rows = 0;

cout<<"输入钻石的行数"<<< endl;

cin>> ;行;


int space = 0;

int space1 = 0;

int space2 = 0;

int space3 = 0;

int space4 = 1;

int a = 0;

int b = 0;


space1 = rows / 2;

space2 = space1;

space3 = sp ace1;

a = rows-2;

space = space1 + a;


int count = 0;

int count1 = 0;

int count2 = 0;

int count3 = 0;

int count4 = 0;

int count5 = 0;

int count6 = 0;


int char1 = 1;

int char2 = 0;


b = a-1;


char2 = space1 + b;


while(count1< space1)

{


count2 = 0;

count3 = 0;


while(count2< space2)

{


cout<<"" " ;;

count2 = count2 + 1;


}


while(count3< char1)

{


cout<<""#" ;;

count3 = count3 + 1;

if(count3< char1)


{


cout<<"" $";

count3 = count3 + 1;


}

}


cout<< endl;

count1 = count1 + 1;

char1 = char1 + 2;

space2 = space2-1;


}


while(count< = space)

{

cout<<"#" ;

count = count + 1;

if(count< space)

{

cout<< $;

count = count + 1;


}

}


cout<< endl;


while(count4< = space3)

{


count5 = 0;

count6 = 0;


while(count5< space4)

{

cout<<" " ;;

count5 = count5 + 1;


}


while(count6< char2)

{


cout<<""#" ;;

count6 = count6 + 1;

if(count6< char2)

{


cout<<" $";

count6 = count6 + 1;


}

}


space4 = space4 + 1;

char2 = char2-2;

count4 = count4 + 1;

cout<< endl;


}

}


以上代码适用于3但不适用于其他奇数值,如5

和7.任何人都可以帮助?

如何让它适用于偶数?

I need to write a program which takes a number as an input and prints a
diamond of # and $. The number of rows in the shape is equal to the
number entered by the user. My program should display the shape for
both even and odd value of size. For example if user enters number 7
the program should print following shape.

#
#$#
#$#$#
#$#$#$#
#$#$#
#$#
#

If user enters a value 6, the program prints diamond in following
shape.

#
#$#
#$#$#
#$#$#
#$#
#

I have tried very hard and this is the best i could come up with

#include<iostream>
using namespace std;

int main()
{

int rows=0;
cout<<"Enter the number of rows of the diamond"<<endl;
cin>>rows;

int space=0;
int space1=0;
int space2=0;
int space3=0;
int space4=1;
int a=0;
int b=0;

space1=rows/2;
space2=space1;
space3=space1;
a=rows-2;
space=space1+a;

int count=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;

int char1=1;
int char2=0;

b=a-1;

char2=space1+b;

while(count1<space1)
{

count2=0;
count3=0;

while(count2<space2)
{

cout<<" ";
count2=count2+1;

}

while(count3<char1)
{

cout<<"#";
count3=count3+1;

if(count3<char1)

{

cout<<"$";
count3=count3+1;

}
}

cout<<endl;
count1=count1+1;
char1=char1+2;
space2=space2-1;

}

while(count<=space)
{
cout<<"#";
count=count+1;
if(count<space)
{
cout<<"$";
count=count+1;

}
}

cout<<endl;

while(count4<=space3)
{

count5=0;
count6=0;

while(count5<space4)
{

cout<<" ";
count5=count5+1;

}

while(count6<char2)
{

cout<<"#";
count6=count6+1;

if(count6<char2)
{

cout<<"$";
count6=count6+1;

}
}

space4=space4+1;
char2=char2-2;
count4=count4+1;
cout<<endl;

}
}

This above code works fine for 3 but not for other odd values like 5
and 7. Can anyone help?
Also how to make it work for even numbers?

推荐答案

。形状中的行数等于用户输入的

数。我的程序应显示

的形状,偶数和奇数值的大小。例如,如果用户输入数字7

,程序应打印以下形状。




. The number of rows in the shape is equal to the
number entered by the user. My program should display the shape for
both even and odd value of size. For example if user enters number 7
the program should print following shape.

#
#




#
#



这篇关于钻石形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆