怎么用打印出杨辉三角形啊

tangulao6575 2012-10-29 03:21:21
怎么打印出如下杨辉三角形?

1
1 2 1
1 3 3 1
1 4 6 4 1
...全文
233 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-10-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
static class Helper
{
//m!/(n!*(m-n)!)
public static int C(int m, int n)
{
return facEx(n + 1, m) / facEx(2, m - n);
}

private static int facEx(int from, int to)
{
int result = 1;
for (int i = from; i <= to; i++)
{
result *= i;
}
return result;
}
}

class Program
{
static void Main(string[] args)
{
int n = 8;
int[][] result = new int[n + 1][];
for (int i = 0; i < n + 1; i++)
{
result[i] = new int[i + 1];
for (int j = 0; j <= i; j++)
{
result[i][j] = Helper.C(i, j);
}
}
foreach (int[] item in result)
{
Console.WriteLine(string.Join(" ", item.Select(x => x.ToString().PadLeft(3, ' ')).ToArray()));
}
}
}
}


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
Press any key to continue . . .
落尘521 2012-10-29
  • 打赏
  • 举报
回复
我的面试题也差不多是用两个for循环做出来,
---*---
-*-*-*-
*-*-*-*
-*-*-*-
---*---
求大侠们指点下
桑子 2012-10-29
  • 打赏
  • 举报
回复
我知道的可以用二维数组来做
冰镇宝贝321 2012-10-29
  • 打赏
  • 举报
回复
http://wzcsying.blog.51cto.com/284684/53767/http://wzcsying.blog.51cto.com/284684/53767/
烈火蜓蜻 2012-10-29
  • 打赏
  • 举报
回复
bdmh 2012-10-29
  • 打赏
  • 举报
回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧