本文摘自PHP中文网,作者黄舟,侵删。
这篇文章主要介绍了C# 中杨辉三角的实现的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下C# 中杨辉三角的实现
问题描述:创建一个程序来求三角形。该程序提示用户输入数据,然后显示出杨辉三角的规律。
// 输入描述:杨辉三角长,代表数值
// 程序输出:杨辉三角
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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;
a[j][j] = 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();
}
}
}
|
以上就是C#实现杨辉三角的示例的详细内容!
相关阅读 >>
.net core2.0小技巧之memorycache问题修复解决的方法(图)
.net core如何在新的项目系统中(.csproj)发布可执行文件
c#中datetime与时间戳转换的实例代码
.net中core如何利用redis发布订阅的实例分析
c#和.net是一个东西吗?c#与.net的区别与联系
c#中noto sans字体支持韩文的实例教程
详细了解在.net core 上运行的wordpress
c#中关于infinity与nan的简单介绍
httpclient向https发送数据建立ssl连接时的异常
c#中enum与string的相互转换的示例
更多相关阅读请进入《csharp》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#实现杨辉三角的示例