CF118B 题解
Forgotten_freopen · · 题解
题解区有不少打表做的,这里我讲一下我的方法——找规律,然后用函数一行一行地输出。
比如当
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 2 1 0
0 1 2 1 0
0 1 0
0
输出字符个数の规律
第一行
第二行
第三行
……
以此类推,第
for(int i = 1; i <= abs(a - n - 1) * 2; i++) cout<< " " ;
输出数字大小の规律
可以发现:
第一行
第二行
第三行
……(实际上当下一次输出的数为
第六行
但是!
第七行
不过这时
所以可以得出以下的代码(其中
for(int i = 1; i < (n - abs(a - n - 1)) * 2 + 1; i++){
cout<< b << " " ;
if(i <= min(a - 1,s - a)) b++;
else b--;
}
cout<< b << endl;
完整代码
#include<bits/stdc++.h>
using namespace std;
int n,s,i;
void anyu(int a){
int b = 0;
for(int i = 1; i <= abs(a - n - 1) * 2; i++) cout<< " " ;
for(int i = 1; i < (n - abs(a - n - 1)) * 2 + 1; i++){
cout<< b << " " ;
if(i <= min(a - 1,s - a)) b++;
else b--;
}
cout<< b << endl;
}
int main(){
cin >> n;
s = 2 * n + 1;
for(i = 1; i <= s; i++) anyu(i);
return 0;
}
简单吧。