代码写的比较粗糙 各位看官凑合着看看
两层循环搞定 代码写的不算面向对象 用了class但用在数据结构上了
话说我真是老了 一个非递归搜索写了一个小时
Code
class Program
{
class Node
{
public Node _last;
public int _num;
public int _sum;
public int _count;
public Node(Node last, int num)
{
_last = last;
_num = num;
_sum = last._sum + num;
_count = last._count + 1;
}
public Node(int min)
{
_last = null;
_num = min;
_sum = 0;
_count = 0;
}
}
static void printsplit(int max, int min, int n, int sum)
{
List<Node> collection = new List<Node>();
collection.Add(new Node(min));
int i = 0;
while (i < collection.Count)
{
Node current=collection[i];
if (current._count == n - 1)
{
Console.Write(sum - current._sum + " ");
while (current._last!=null)
{
Console.Write(current._num.ToString()+" ");
current = current._last;
}
Console.Write("\n");
i++;
continue;
}
for (int j = current._num; j < max && current._sum + j * (n - current._count) <= sum; j++)
{
collection.Add(new Node(current, j));
}
i++;
}
}
static void Main(string[] args)
{
printsplit(5, 1, 3, 5);
}
}