题解 | #杨辉三角#
杨辉三角
http://www.nowcoder.com/questionTerminal/e671c6a913d448318a49be87850adbcc
递归,导致大量重复计算,但代码很短
#include <cstdio>
#include <iostream>
using namespace std;
int Yanghuinum(int row, int col){
if(row == 0 || col == 0 || row == col){
return 1;
}else{
return Yanghuinum(row-1, col-1) + Yanghuinum(row-1,col);
}
}//显而易见,代码简单,但是会做很多重复计算,无所谓了
int main(){
int n;
while(scanf("%d",&n) != EOF){
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){//<=是个细节
printf("%5d",Yanghuinum(i,j));
}
printf("\n");
}
}
return 0;
}