zoukankan      html  css  js  c++  java
  • 第6关:输出杨辉三角

    任务描述

    题目描述:还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:

    1

    1 1

    1 2 1

    1 3 3 1

    1 4 6 4 1

    1 5 10 10 5 1

    编写程序,输出杨辉三角的前n行,要求从键盘输入n的值。

    输入

    从键盘输入n的值。

    输出

    打印出杨辉三角图形的前n行。格式见题目描述部分。每个整数后面接一个空格来分隔开整数

    编程要求

    根据提示,在右侧编辑器补充代码。

    编程提示

    假设数组名为a,则数组元素的输出格式建议采用如下格式:

    Console.Write("{0} ",a[i,j]);

    测试说明

    平台会对你编写的代码进行测试:

    测试输入:

    4

    预期输出:

    1

    1 1

    1 2 1

    1 3 3 1

    测试输入:

    6

    预期输出:

    1

    1 1

    1 2 1

    1 3 3 1

    1 4 6 4 1

    1 5 10 10 5 1


    开始你的任务吧,祝你成功!

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ch706
    {
        class Program
        {
            static void Main(string[] args)
            {
    			/******begin*******/
    			int n = Convert.ToInt32(Console.ReadLine());
                int[,] a = new int[n, n];
                
                for (int i = 0; i < a.GetLength(0); ++i)
                {
                    for (int j = 0; j <= i; ++j)
                    {
                        if(j == 0 || i == j)
                        {
                            a[i, j] = 1;
                        }
                        else
                        {
                            a[i,j] = a[i - 1,j] + a[i-1,j-1];
                        }
                        Console.Write(a[i,j].ToString() + " ");
                    }
                    Console.WriteLine();
                }
    
    			
    			
    			/*******end********/
    
            }
        }
    }
    

      

  • 相关阅读:
    go语言切片
    sharding-jdbc分库分表配置,多数据源
    spring boot的配置文件
    go-micro生成项目
    自定义注解+aop实现jetcache功能扩展
    linux下mysql忘记密码解决方案
    MySQL 1130错误,无法远程连接
    Linux/UNIX 上安装 MySQL
    BarTender遇到的问题
    SourceTree安装使用
  • 原文地址:https://www.cnblogs.com/mjn1/p/12452193.html
Copyright © 2011-2022 走看看