带有“*”和“.”的金字塔在 C++

问题描述 投票:0回答:0

我需要用星星和点做一个金字塔。代码的前半部分是正确的,但另一半是错误的。我试过了。

输入为:size=4 and n = 7 输出是:

*...
**..
***.
****
.***
..**
...*

我想要它

*...
**..
***.
****
***.
**..
*...
#include < iostream >

using namespace std;

int main() {
  int size, n;

  cin >> size;
  cin >> n;

  for (int R = 1; R <= size; R++) {
    if (R > n) {
      break;
    }
    for (int i = 1; i <= size; i++) {
      if (i > R) {
        cout << ".";
      } else {
        cout << "*";
      }
    }
    cout << endl;
  }

  for (int R = 1; R <= size - 1; R++) {
    if (R > n) {
      break;
    }
    for (int i = 1; i <= size; i++) {
      if (i > R) {
        cout << "*";
      } else {
        cout << ".";
      }
    }
    cout << endl;
  }

  return 0;

}

logic pyramid
© www.soinside.com 2019 - 2024. All rights reserved.