因为不知道你是要输出三角形的星号(根据图形),还是要输出菱形的星号(根据文字),所以两个C++程序我都编写出来给你.
按照给出的图形编写的C++程序如下
#include <iostream>
using namespace std;
int main()
{
int i,j,N=4;
for(i=1;i<=N;i++){
for(j=1;j<=N-i;j++){
cout << " ";
}
for(j=1;j<=i*2-1;j++){
cout << "*";
}
cout << endl;
}
return 0;
}
按照要求编写的输出菱形星号的C++程序如下
#include <iostream>
using namespace std;
int main()
{
int i,j,N=4;
for(i=1;i<=N;i++){
for(j=1;j<=N-i;j++){
cout << " ";
}
for(j=1;j<=i*2-1;j++){
cout << "*";
}
cout << endl;
}
for(i=N+1;i<=2*N-1;i++){
for(j=1;j<=i-N;j++){
cout << " ";
}
for(j=1;j<=4*N-i*2-1;j++){
cout << "*";
}
cout << endl;
}
return 0;
}