我的输出应该是四个三角形和一个金字塔。我设法得到了四个三角形,但无法弄清金字塔。任何帮助都会很棒。 (我也必须使用setw和setfill)。使用setw建立一个金字塔
输出是左对齐的三角形,然后左对齐倒置。 右对齐三角形,然后右对齐三角形倒置。
这是我的电流输出:
#include <iostream>
#include <iomanip>
using namespace std;
//setw(length)
//setfill(char)
int height; //Number of height.
int i;
int main()
{
cout << "Enter height: ";
cin >> height;
//upside down triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n"; //line break between
//rightside up triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//right aligned triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(i-height) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//upside down/ right aligned triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill (' ') << setw(height-i+1) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//PYRAMID
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(height-i*3) << " "; //last " " is space between
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
}//end of main
它很难理解你想要什么显示图像图 –
感谢您的建议。我已添加目前的输出。最后一个三角形应该是金字塔。 – Ace