HDU-2032 杨辉三角

来源:互联网 发布:mac icloud备份 编辑:程序博客网 时间:2024/06/14 18:07

2016-07-10

HDU-2032 杨辉三角

题目大意:输入将要输出的杨辉三角的层数,输出对应的杨辉三角。

解题思路:先用二位数组画一个 100 层的杨辉三角,再根据要求输出杨辉三角。

#include <iostream>using namespace std;int main () {    int a[100][100];    int n;    a[0][0] = 1;    a[1][0] = 1;    a[1][1] = 1;    for (int i = 2; i < 100; i++) {        for ( int j = 0; j < 100; j++) {            if ( j == 0 )    a[i][j] = 1;            else    a[i][j] = a[i-1][j-1] + a[i-1][j];            if ( i == j )    a[i][j] = 1;            if ( i < j )     break;        }        }    while ( cin >> n ) {        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                if ( i < j )    break;                if ( j == 0 )    cout << a[i][j];                else    cout << " " << a[i][j];            }            cout << endl;            }        cout << endl;    }    return 0;}


1 0
原创粉丝点击