日期:2014-05-18  浏览次数:20974 次

输出如下图形
1、 输出如下图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
。。。。。。。。。。。。。。
要求:
a) 开始运行时显示:“请输入打印行数,在1~9之间“。
b) 根据输入的数字进行输出,输入的数字不在1~9之间提示“请输入1~9间的数字“,然后让用户继续输入。
c) 如输入0则终止运行程序
d) 当打印完成后显示“是否要继续输入?Y/N“,如是Y重复步骤a,如是N退出。


------解决方案--------------------
class Program
{
static void Main(string[] args)
{
Console.Write("请输入打印行数,在1~9之间:");

int row = Convert.ToInt32(Console.ReadLine());

while (row > 0)
{
int[] source = new int[1] { 1 };
if (row < 10)
{
for (int i = 0; i < row; i++)
{
foreach (int n in source)
{
Console.Write(" {0}", n);
}
Console.WriteLine();

source = NextRow(source);
}
}

Console.Write("是否要继续输入?Y/N:");

if (Console.ReadLine().ToLower() == "y")
{
Console.Write("请输入打印行数,在1~9之间:");
row = Convert.ToInt32(Console.ReadLine());
}
else
{
row = 0;
}
}
}

static int[] NextRow(int[] source)
{
int[] outArray = new int[source.Length + 1];
outArray[0] = 1;

for (int i = 1; i < outArray.Length - 1; i++)
{
outArray[i] = source[i-1] + source[i];
}

outArray[outArray.Length - 1] = 1;

return outArray;
}
}