观察杨辉三角的分布,得到a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
i,j分别是行和列,
#include<iostream>
const int M = 20000;
using namespace std;
int a[M][M];
int main() {
a[1][1] = 1;
int N;
scanf("%d", &N);
int num = 1;
const int y = 1;
if(N==1) printf("%d",y);
else{
for (int i = 2; i < M; i++) {
for (int j = 1; j <= i; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
num++;
if (a[i][j] == N) {
cout << num << endl;
return 0;
}
}
}
}
}
值得注意的是,数组的初始化符合我们的认知,同时注意到N>=1,
所以当输入为一的时候,应该输出1