当前位置: 移动技术网 > IT编程>开发语言>c# > C# 中杨辉三角的实现

C# 中杨辉三角的实现

2019年07月18日  | 移动技术网IT编程  | 我要评论
c# 中杨辉三角的实现 问题描述:创建一个程序来求三角形。该程序提示用户输入数据,然后显示出杨辉三角的规律。     &

c# 中杨辉三角的实现

问题描述:创建一个程序来求三角形。该程序提示用户输入数据,然后显示出杨辉三角的规律。           

// 输入描述:杨辉三角长,代表数值            

// 程序输出:杨辉三角 

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();



    }
  }
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网