杨辉三角形

来源:互联网 发布:php 文件提交系统 编辑:程序博客网 时间:2024/05/31 13:14
//Copyright (c) 2014软件技术1班
// All rights reserved. 
// 作    者:A23罗燕芬
// 完成日期:2014年 11 月 23日  
// 版 本 号:v1.0
// 问题描述:创建一个程序,根据杨辉三角形的规律,输入并输出杨辉三角形。
// 输入描述:输入杨辉三角形的规律。

// 程序输出:杨辉三角形的输出

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            int[,] num = new int[11, 11];//定义数组            //数组赋值            for (int i = 0; i < num.GetLength(0); i++)            {                for (int j = 0; j < num.GetLength(1); j++)                {                    if (i == j || j == 0) num[i, j] = 1;                    else if (i > 1&&j>0) num[i, j] = num[i - 1, j - 1] + num[i - 1, j];                    else num[i, j] = 0;                }            }            //输出数组            for (int i = 0; i < num.GetLength(0); i++)            {                for (int j = 0; j < num.GetLength(1); j++)                {                    if (num[i, j] != 0) Console.Write("{0 } ", num[i, j]);                    else Console.Write(" ");                                   }                Console.WriteLine();            }            Console.ReadKey();        }    }}



总结:加深了对数组的了解,对for循环的使用更加深刻。


0 0
原创粉丝点击