C#杨辉三角形两种实现方法

//第一种:
using System;
using System.Collections.Generic;
using System.Text;

namespace yhsj
{
    class Program
    {
        static void Main(string[] args)
        {
            //@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/【Dream InterFace】/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/@/
            Console.WriteLine(“请输入您想得到的最大次方(也就是行数+1):”);

            int n = Convert.ToInt32(Console.ReadLine());//用于定义三角的行数,也就是n次方;
            Console.WriteLine(“/n杨辉三角如下:”);
            //int n = 5;
            int[] Deposited = new int[n + 1];//用于临时存储上一次得到的数字
            int[] Triangle;//用来表示每一行的数组;
            for (int i = 0; i <= n; i++)
            {
                if (i == 0)
                {
                    Format(0, n);
                    Console.WriteLine(“1/n”);
                }
                else if (i == 1)
                {
                    Triangle = new int[i + 1];
                    Triangle[0] = 1;
                    Triangle[i] = 1;
                    Format(1, n);
                    foreach (int r in Triangle)
                    {
                        Console.Write(r + “/t”);
                    }
                    //把得到的一行数据转存到Deposited中
                    for (int D = 0; D <= i; D++)
                    {
                        Deposited[D] = Triangle[D];
                    }
                    Console.WriteLine(“/n”);
                }
                else if (i > 1)
                {
                    //把上一行的数据传到方法里面,计算后返回下一行的数据存储在Triangle
                    Triangle = Genius(Deposited, i, n);
                }
            }
            //在此处无实际意义,主要是在程序结束后保证窗体不会一下消失,当然按Shift+F5也是一样的
            Console.ReadLine();
        }
        /// <summary>
        /// 计算下一行的数据
        /// </summary>
        /// <param name=”Deposited”>上一行的数据</param>
        /// <param name=”i”>当前行数</param>
        /// <param name=”n”>总行数</param>
        /// <returns>得到的行的数据</returns>
        private static int[] Genius(int[] Deposited, int i, int n)
        {
            int[] Num;
            Num = new int[i + 1];
            Num[0] = 1;
            Num[i] = 1;
            for (int j = 0; j < i – 1; j++)
            {
                Num[j + 1] = Deposited[j] + Deposited[j + 1];
            }
            Format(i, n);
            for (int o = 0; o < Num.Length; o++)
            {
                if (i % 2 != 0)
                {
                    Console.Write(Num[o] + “/t”);
                }
                else
                    Console.Write(Num[o] + “/t”);
            }

            //把得到的一行数据转存到Deposited中
            for (int D = 0; D <= i; D++)
            {
                Deposited[D] = Num[D];
            }

            Console.WriteLine(“/n”);
            return Num;
        }

        public static void Format(int i, int n)   //控制输出的空格
        {
            for (int Tab = 0; Tab < n – i; Tab++)
            {
                Console.Write(“”);
            }
        }
    }
}

 

 

 

//第二种

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintValue(8);
        }

        private static void PrintValue(int maxLineNumber)
        {
            int[] lastNum = { 1 };
            for (int i = 1; i <= maxLineNumber; i++)
            {
                int[] num = new int[i];
                num[0] = num[i – 1] = 1;

                if (i % 2 == 0)
                {
                    for (int j = 1; j < i / 2; j++)
                    {
                        num[i – j – 1] = num[j] = lastNum[j – 1] + lastNum[j];
                    }
                }
                else
                {
                    for (int j = 1; j < (i / 2) + 1; j++)
                    {
                        num[i – j – 1] = num[j] = lastNum[j – 1] + lastNum[j];
                    }
                }

                foreach (int a in num)
                {
                    Console.Write(a);
                    Console.Write(“/t”);
                }

                lastNum = num;
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/zhangguofang1129/article/details/4569350
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞