- using System;
- namespace test23
- {
- /// <summary>
- /// Program类
- /// </summary>
- internal class Program
- {
- /// <summary>
- /// Main
- /// </summary>
- /// <param name="args"></param>
- private static void Main(string[] args)
- {
- //题目: 杨辉三角
- var p = new Program();
- Console.WriteLine("请输入数组值:");
- var strName = Console.ReadLine();
- int value = Convert.ToInt16(strName);
- p.Yanghui(value);
- Console.ReadKey();
- }
- public void Yanghui(int value)
- {
- if (value < 3)
- {
- Console.WriteLine("请重新输入数组大于3的值!");
- }
- else
- {
- var arry = new int[value, value];
- Console.WriteLine("杨辉三角形:");
- for (var i = 0; i < value; i++)
- {
- var str = "";
- str = str.PadLeft(value - i, '\t');
- Console.Write(str);
- for (var j = 0; j <= i; j++)
- {
- if (i == j || j == 0)
- {
- arry[i, j] = 1;
- }
- else
- {
- arry[i, j] = arry[i - 1, j - 1] + arry[i - 1, j];
- }
- Console.Write(arry[i, j] + "\t\t");
- }
- Console.WriteLine();
- }
- }
- }
- }
- }
复制代码
|
|