Description
请编写程序,打印一个 n 阶数字三角形,比如 3 阶的时候是这样的:
1 3 4
2 5
6
Input
一个整数 n (1≤n≤20)。
Output
n 阶三角形,注意每个数字之后需要一个空格。
Sample Input
4
Sample Output
1 3 4 10
2 5 9
6 8
7
HINT
#include<iostream>
#include <iomanip>
using namespace std;
#define max 20
int main() {
int n, temp = 1,i, t[max][max] = { 0 };
cout << "Sample Input:" << endl;
cin >> n;
cout << "Sample Output:" <<endl;
for (i=1; i <= n; i++) {
if (i % 2) {
for (int j = 0, k = i-1; k>=0;k=k-1,j=i-1-k) {
//cout << k << "\t" << j << "\t" << "①" << temp << endl;
t[j][k] = temp++;
}
}
else{
for (int j = i-1, k = 0; j>=0;j=j-1,k=i-1-j) {
//cout << k << "\t" << j << "\t" << "②" << temp << endl;
t[j][k] = temp++;
}
}
}
for (i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cout << setw(3)<<t[i][j] << " ";
}
cout << endl;
}
}
我设置了对齐你不想要就把setw(3)删了