7,763
社区成员
发帖
与我相关
我的任务
分享
void main()
{
int i,k,j;
for(i=1;i<=5;i++)
{
for(k=1;k<=5-i;k++)
printf(" ");
for(j=1;j<=i*2-1;j++)
printf("*");
printf("\n");
}
}
c写过
public class Node
{
public Dictionary<char, Node> Dict { get; set; }
public Node() { Dict = new Dictionary<char, Node>(); }
public IEnumerable<string> Get()
{
foreach (var item in Dict.OrderBy(x => x.Key))
{
if (item.Key == '\0') yield return "";
foreach (var item1 in item.Value.Get())
{
yield return item.Key.ToString().ToLower() + item1;
}
}
}
}
List<string> list;
public void digui(Dictionary<char, Node> dict)
{
foreach (var v in dict.OrderBy(x => x.Key))
{
if (v.Key == '\0')
{
list.Add(temp);
temp = "";
}
else
{
temp = temp + v.Key.ToString();
digui(v.Value.Dict);
}
}
}
上面那个是别人写的,下面那个是自己重新写的。。感觉实现的东西都一样。。但不知道迭代器到底是按怎样一个顺序(或原理)执行的。。MSDN解释看不太懂(我比较笨)。。设断点也跑不进去using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
foreach (var line in 我的杨辉三角(10, 15))
Console.WriteLine(line);
Console.ReadKey();
}
private static IEnumerable<string> 我的杨辉三角(int n, int 前边有几个空格)
{
if (n > 1)
{
foreach (var line in 我的杨辉三角(n - 1, 前边有几个空格 + 1))
yield return line;
}
yield return new string(' ', 前边有几个空格) + new string('*', n * 2 - 1);
}
}
}
我的杨辉三角(10, 5);
看看结果,就能明白“我的杨辉三角”的目的就是:打印n层级的三角,并且要向右移动一定位置。
然后所谓“分析的思路”就是:打印n行三角,需要先打印n-1行,然后再打印最后一行即可。
学软件要学会思路,真正的算法都是精当的科学归纳算法,不要学一堆低级的拼凑结果的所谓“算法”。using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
我的杨辉三角(5, 0);
Console.ReadKey();
}
private static void 我的杨辉三角(int n, int 前边有几个空格)
{
if (n > 1)
我的杨辉三角(n - 1, 前边有几个空格 + 1);
Console.Write(new string(' ', 前边有几个空格));
Console.WriteLine(new string('*', n * 2 - 1));
}
}
}
不过学生恐怕只会拼凑而不会分析,因此恐怕看这种东西需要人指点。for (int i = 0; i < 5; i++)
{
string temp = "";
for (int j = 0; j <= i; j++)
{
temp += "*";
}
Console.WriteLine(temp);
}
Console.Read();
这算不算3角型- -0