杨辉三角

本文介绍了一个使用C#实现的简单程序,该程序能够根据用户输入的长度生成对应的杨辉三角形。通过二维数组存储各层数值,并利用杨辉三角的数学特性进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

02.// All rights reserved.                    
04.// 完成日期:2014年 11 月 21日           
05.// 版 本 号:v1.0           
06.//           
07.// 问题描述:创建一个程序来求三角形。该程序提示用户输入数据,然后显示出杨辉三角的规律。          
08.// 输入描述:杨辉三角长,代表数值           
09.// 程序输出:杨辉三角
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int length = 0;//杨辉三角形的长度 
            Console.Write("输入杨辉三角长度:");


            length = Convert.ToInt32(Console.ReadLine());//指定杨辉三角形的长度
            int[][] a = new int[length][];//二维数组


            for (int i = 0; i < a.Length; i++)
                a[i] = new int[i + 1];//遍历,赋值增量
            for (int j = 0; j < a.Length; j++)
            {
                a[j][0] = 1; //把第1列的元素都赋1
                a[j][j] = 1; //把每1列最右边的元素都赋1
                for (int m = 1; m < a[j].Length - 1; m++)


                    a[j][m] = a[j - 1][m - 1] + a[j - 1][m];//其余元素的值由杨辉公式计算
            }
            for (int i = 0; i < a.Length; i++) //遍历数组输出杨辉三角形
            {


                for (int j = 0; j < a[i].Length; j++)
                    Console.Write("{0}\t", a[i][j]);
                Console.Write("\n");
            }
            Console.Read();




 


        }
    }
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值