[求助]如何打印空心菱形
如: *
* *
* *
* *
*
大家帮帮忙,谢谢
[此贴子已经被作者于2006-6-29 14:45:54编辑过]
人都到哪去了,我写了一个,大家有更好的也贴一贴,红色部分是主要的代码,其它是输入输出等
[CODE]#include <iostream>
using namespace std;
void main()
{
int nLength = 0, i, j, n;
cout<<"please input the length:";
cin>>nLength;
if (nLength % 2 != 1 || nLength < 2)
{
cout<<"error length"<<endl;
}
n = (nLength - 1) / 2;
char *buffer = new char[nLength + 1];
//这里最好再判断下内存是否申请成功,我懒得写了,嘿嘿
memset(buffer, ' ', nLength);
buffer[nLength] = '\0';
for (i = 0; i < nLength; i ++)
{
j = i > n ? nLength - i - 1 : i;
buffer[n - j] = buffer[n + j] = '*';
cout<<buffer<<endl;
buffer[n - j] = buffer[n + j] = ' ';
}
}[/CODE]