#include <stdio.h>
int main()
{
int a[100] = {0,1}; //初始化数组
int n, i, j;
int l, r; //存放上一层左边的数和右边的数
scanf("%d",&n); //层数
for(i = 1; i <= n; i++)
{
l = 0;
for(j = 1; j <= i ; j++)
{
r = a[j]; //面试题空白处
a[j] = l + r; //面试题空白处
printf("%-4d", a[j]);
l = r;
}
printf("\n");
}
}
灭有什么需要解释的;
最多,同一行中,左边数字的前一行的右边数字,正好是该数字的左边数字 L=r
Int a[100]={0,1}前两个元素为 0,1;其他为0;
认为第一层的前一行元素为 0,1
在此基础上实现的HDUOJ2032题
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
int a[100] = {0,1}; //初始化数组
int n, i, j;
int l, r; //存放上一层左边的数和右边的数
while(~scanf("%d",&n)){ //层数
for(i = 1; i <= n; i++)
{
l = 0;
for(j = 1; j <= i ; j++)
{
r = a[j]; //面试题空白处
a[j] = l + r; //面试题空白处
printf("%d", a[j]);
l = r;
if(j!=i){
cout<<" ";
}
}
printf("\n");
}
a[0]=0;
a[1]=1;
for(i=2;i<100;i++){
a[i]=0;
}
cout<<endl;
}
}