# inverted triangle

• ### 問題

•       ABCDEFGHIJK

ABCDEFGHI

ABCDEFG

ABCDE

ABC

A

Please input number of rows for the triangle (range 3-20)

how to write a C++ program like the above

I had wrote this:(but cannot make)

#include <iostream> //for cin and cout

using namespace std; //avoid std::cout

char Tree(int r, int c)

{

if (r>c) return ' ';

char t = char(c + 65 - r);

if (t>'Z') return t - 'Z' + 'A' - 1;

else return t;;

}

int main()

{

int rows, cols;

while(true)

{

cout << "\nPlease input number of rows for the triangle (range 3-20): ";

cin >> rows;

if (rows>2 && rows<21) break;

cout << "Incorrect range of number, it should between 3 to 20.";

}

cols = rows;

for (int i=0; i<rows; i++)

{

for (int j=0; j<cols; j++)

{

cout << Tree(i, j);

}

cout << '\n';

}

system("pause");

}

2015年10月4日 上午 09:23

### 所有回覆

• ```    for (int i = 0; i < rows; i++)
{
cols = rows * 2 - i - 1;
for (int j = 0; j < cols; j++)
{
cout << Tree(i, j);
}
cout << '\n';
}```

2015年10月6日 上午 12:59