zoukankan      html  css  js  c++  java
  • 2018-12-15 日常acm HDU

    C - Problem C HDU - 2032

    HDU - 2032
    还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
    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

    思路:进行模拟,在第一列以及最右边设为1;中间的依次相加;
    输出的时候要注意空格输出的情况。

    #include<iostream>
    using namespace std;
    int main()
    {
    	int a[35][35];
    	int i, j, n;
    	while (cin >> n)
    	{
    		for (i = 0; i < n; i++)
    		{
    			for (j = 0; j <= i; j++)
    			{
    				if (j == 0 || j == i)
    				{
    					a[i][j] = 1;
    				}
    				else
    				{
    					a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
    				}
    			}
    		}
    
    		for (i = 0; i < n; i++)
    		{
    			for (j = 0; j < i + 1; j++)
    			{
    				if (i!= j)
    				{
    					cout << a[i][j] << " ";
    				}
    				else
    					cout << a[i][j];
    			}
    			cout << endl;
    		}
    		cout << endl;
    	}
    	
        return 0;
    }
    
  • 相关阅读:
    python中__dict__和dir()
    python学习之copy模块
    python学习之optparse
    python join和split和strip用法
    浅谈 Python 的 with 语句
    Python:itertools模块
    OpenStack Swift client开发
    OpenStack Swift集群部署流程与简单使用
    python bisect模块
    Python中的导入
  • 原文地址:https://www.cnblogs.com/gidear/p/10433289.html
Copyright © 2011-2022 走看看