c++用循环打印空心菱形

来源:百度知道 编辑:UC知道 时间:2024/06/14 16:28:57
*
* *
* *
* *
* *
* *
* *
* *
*

#include <iostream>
using namespace std;
int main(int argc, char ** argv){
int width;
cin>>width;
if(width % 2 == 0){
width += 1;
}
for(int i = 0; i != width; i ++){
for(int j = 0 ; j != width; j++){
if( j == (width/2 -i) || j == (width/2 + i) || j == (i - width/2) || j == (width/2 *3 -i))
{
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
}
getchar();
return 0;
}
width就是宽度,输入整数,如果是偶数,就加1,这样才能输出完整的菱形。

#include <iomanip>
#include<iostream>
using namespace std;

void main()
{
int ifalg = 0;
for (int i = 4; i <= 4;)
{
if (i == 4)
{
cout<<setw(