本文主要是介绍nefu 23 杨辉三角,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
杨辉三角
Problem:23
Time Limit:1000ms
Memory Limit:65536K
Description
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Input
输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。
Output
对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。
Sample Input
2 3
Sample Output
1 1 11 1 1 1 2 1
Hint
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;int main()
{int a[30][30]= {0};int i,j,n;while(cin>>n){if(n==0)break;for(i=0; i<n; i++){for(j=0; j<i+1; j++){if(j==0||j==i){a[i][j]=1;}else{a[i][j]=a[i-1][j-1]+a[i-1][j];}printf("%d ",a[i][j]);}printf("\n");}}return 0;
}
Discuss
这篇关于nefu 23 杨辉三角的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!