我有这个分配:
编写一个程序梯形。cpp,打印一个颠倒的梯形 给定宽度和高度。
但是,如果输入高度对于给定宽度不可能大,则 然后该程序应报告,不可能形状!
示例1:
输入宽度:12输入高度:5形状:************ ********** ******** ****** ****
示例2:
输入宽度:12输入高度:7不可能的形状!
我需要帮助,试图弄清楚什么是不可能的梯形。到目前为止,这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int rows, width, height, spaces, stars; // declare values
cout << "enter width" << endl;
cin >> width;
cout << "enter height" << endl;
cin >> height;
for (int row = 0; row < height; ++row) {
for (int col = height + row; col > 0; --col) {
if (height % 6 == 1) {
cout << "Impossible shape!" << endl;
return 0;
}
cout << " ";
}
for (int col = 0; col < (width - 2 * row); ++col) {
cout << "*";
spaces += 1;
stars -= 2;
}
cout << endl;
形状中的星号的数量以最高级别的宽度开始,然后在每个后续级别降低2。一旦底层为2个星号宽,您就无法在其下方具有另一个级别。因此,宽度k的最大高度是k/k/2的k/2。