ITPub博客

首页 > 应用开发 > IT综合 > c#杨辉三角代码

c#杨辉三角代码

原创 IT综合 作者:qcyhzwq001 时间:2015-01-26 17:31:27 0 删除 编辑
此处不常用,用于出题考试倒是不错.
1.了解原理.
2.最终本质特征.
杨辉三角概念中提到了由数字排列成三角形数表.
没有具体说是那种形式的三角形.
代码如下:


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


namespace YHSJ
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] Array_int = new int[10][];//定义一个10行的二维数组
            //向数组中记录杨辉三角形的值
            for (int i = 0; i < Array_int.Length; i++)//遍历行数
            {
                Array_int[i] = new int[i + 1];//定义二维数组的列数
                for (int j = 0; j < Array_int[i].Length; j++)//遍历二维数组的列数
                {
                    if (i <= 1)//codego.net如果是数组的前两行
                    {
                        Array_int[i][j] = 1;//将其设置为1
                        continue;
                    }
                    else
                    {
                        if (j == 0 || j == Array_int[i].Length - 1)//如果是行首或行尾
                            Array_int[i][j] = 1;//将其设置为1
                        else
                            Array_int[i][j] = Array_int[i - 1][j - 1] + Array_int[i - 1][j];//根据杨辉算法进行计算
                    }
                }
            }
            for (int i = 0; i < Array_int.Length; i++)//输出杨辉三角形
            {
                for (int j = 0; j < Array_int[i].Length; j++)
                    Console.Write("{0}\t", Array_int[i][j]);
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30011186/viewspace-1415892/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-11-06

  • 博文量
    7
  • 访问量
    10644