怎么用C语言打钻石图???

3. Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.

* 1
*** 3
***** 5
******* 7
********* 9
******* 7
***** 5
*** 3
* 1
这是题目,用for的嵌套打出来,怎么弄啊???一点思路也没有............麻烦说的详细一点,多谢了!!!

第1个回答  2010-04-12
#include <iostream>
using namespace std;
int main()
{
int i,j,k,num; //定义for循环中的变量 i,j,k 。num是菱形的个数。
int N; //定义菱形的宽度。

cout<<"Please enter the width of the diamend:"<<endl<<"N=";
cin>>N;
while(N%2==0) //判断输入的菱形宽度是否为奇数,若是奇数则正确,若是偶数则错误。
{
cout<<"The number you entered is wrong!"<<endl;
cout<<"Please try again:"<<endl;
cin>>N;
}

char c;
cout<<"The number is right!"<<endl<<"请输入构成菱形的字符(字符必须是单个的)."<<endl;
cout<<"The sign is:";
cin>>c;

cout<<"请输入你想输出的菱形的个数!"<<endl;
cout<<"The number is:";
cin>>num;

for(int n=0;n<num;n++) //最外层for循环开始,控制菱形个数
{
for(i=0;i<(N+1)/2;i++) //显示菱形的上半部分
{
for(j=0;j<(N+1)/2-i-1;j++) //显示空格
{
cout<<" ";
}
for(j=0;j<2*i+1;j++) //显示构成菱形的字符
{
cout<<c;
}
cout<<endl;
}
/* 注释:display
*
***
*****
******* */

for(k=i;k<N;k++) //显示菱形的下半部分
{
for(j=0;j<k-i+1;j++) //显示空格
{
cout<<" ";
}
for(j=0;j<2*N-2*k-1;j++) //显示构成菱形的字符

{
cout<<c;
}
cout<<endl;
}
/* 注释:display
*****
***
*
*/
cout<<endl;
} //最外层for循环结束
return 0;
}

以前写的注释也很详细,只是跟你的还是有点区别,自己研究下把本回答被提问者采纳
第2个回答  2010-04-12
#include <iostream.h>
void output(int n, int max);
int main()
{
for(int i = 0; i != 10; i += 2)
{
output(i, 9);
}
for(int j = 6; j != -2; j -= 2)
{
cout<<j<<endl;
output(j, 9);
}
}

void output(int n, int max)
{
for(int i = 0; i !=max; ++i)
{
int middle = (max - n) / 2;
if(i < middle || i > middle + n)
{
cout<<" ";
}
else{
cout<<"*";
}

}
cout<<endl;
}
第3个回答  2010-04-12
#include <stdio.h>
#include <math.h>
void main()
{
int x,y;
printf("%d\n",f(x));
for(y=0;y<=8;y++)
{
for(x=0;x<=8;x++)
if(abs(x+y-8)<=4
&&abs(x-y)<=4)
putchar('*');
else
putchar(' ');
putchar('\n');
}
}
相似回答