zoukankan      html  css  js  c++  java
  • HDU--2032

    杨辉三角

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 43360    Accepted Submission(s): 18223


    Problem 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 1 1 1 1 1 2 1
     

    Author
    lcy

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


  • 相关阅读:
    Rust-数据类型
    Rust-智能指针
    Rust-使用包、Crate和模块管理不断增长的项目
    Rust-Slice类型
    Rust-引用与借用
    Rust 的核心功能-所有权(ownership)
    How to fix “sudo: command not found error”
    ABC195 F
    CF1501D Two chandeliers【拓展欧几里得+二分】
    CCA的小球【容斥定理】
  • 原文地址:https://www.cnblogs.com/Tovi/p/6194900.html
Copyright © 2011-2022 走看看