一个正整数可以表示为多个正整数相加的格式,写一个函数求有多少种分拆? 一个正整数可以表示为多个正整数相加的格式 如 5=1+1+1+1+1,=1+1+1+2,=1+2+2 , =1+1+3 =2+3 =1+4,写一个函数求有多少种分拆?求解!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 补充说明,最简单的计算就是 移位运算 >> private void calculate(int n, List<int> list1, int start) { if (n == 1) { for (int i = 0; i < list1.Count; i++) { Console.Write(list1[i] + "+"); } Console.WriteLine(1); } else { for (int i = start; i <= n / 2; i++) { list1.Add(i); calculate(n - i, list1, i); list1.RemoveAt(list1.Count - 1); } for (int i = 0; i < list1.Count; i++) { Console.Write(list1[i] + "+"); } Console.WriteLine(n); } }修改别人C++的代码你要的是下面效果吧 调用 List<int> list1 = new List<int>(); calculate(9, list, 1); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { foreach (var item in foo(5)) Console.WriteLine(string.Join(" + ", item)); } static IEnumerable<IEnumerable<int>> foo(int n) { if (n == 1) return new int[][] { new int[] { 1 } }; return Enumerable.Range(1, n - 1).SelectMany(x => foo(n - x).Select(y => y.Concat(new int[] { x }))); } }}1 + 1 + 1 + 1 + 11 + 2 + 1 + 11 + 1 + 2 + 11 + 3 + 11 + 1 + 1 + 21 + 2 + 21 + 1 + 31 + 4Press any key to continue . . . 不考虑顺序:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { foreach (var item in foo(5)) Console.WriteLine(string.Join(" + ", item)); } static IEnumerable<IEnumerable<int>> foo(int n) { if (n == 1) return new int[][] { new int[] { 1 } }; return Enumerable.Range(1, n - 1).SelectMany(x => foo(n - x).Where(y => y.Max() <= x).Select(y => y.Concat(new int[] { x }))); } }}1 + 1 + 1 + 1 + 11 + 1 + 1 + 21 + 2 + 21 + 1 + 31 + 4Press any key to continue . . .这是9的:1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 11 + 1 + 1 + 1 + 1 + 1 + 1 + 21 + 1 + 1 + 1 + 1 + 2 + 21 + 1 + 1 + 2 + 2 + 21 + 2 + 2 + 2 + 21 + 1 + 1 + 1 + 1 + 1 + 31 + 1 + 1 + 1 + 2 + 31 + 1 + 2 + 2 + 31 + 1 + 1 + 3 + 31 + 2 + 3 + 31 + 1 + 1 + 1 + 1 + 41 + 1 + 1 + 2 + 41 + 2 + 2 + 41 + 1 + 3 + 41 + 4 + 41 + 1 + 1 + 1 + 51 + 1 + 2 + 51 + 3 + 51 + 1 + 1 + 61 + 2 + 61 + 1 + 71 + 8Press any key to continue . . . c#按钮如何实现按一次,数值减少一点 我用的ewebeditor的文本编辑器,在我本机上传图片没问题,可是把站点挪到服务器就不能上传了? 请问属性里只能有get set这两句吗? web.config里面连接字符的解密方法? 如何listbox每一行颜色不同 水晶报表的入门问题,抢分哈 字符格式问题 Windows服务的问题,急啊,麻烦高手进!急!~~~~~~~~ 服务器控件如button,textbox等能否实现鼠标放上,移开后的样式变化? 如何把string转换成能utf-8,或者gb2312 C# 代码更改Wince注册码 超时时间已到处理;
{
if (n == 1)
{
for (int i = 0; i < list1.Count; i++)
{
Console.Write(list1[i] + "+");
}
Console.WriteLine(1);
}
else
{
for (int i = start; i <= n / 2; i++)
{
list1.Add(i);
calculate(n - i, list1, i);
list1.RemoveAt(list1.Count - 1);
}
for (int i = 0; i < list1.Count; i++)
{
Console.Write(list1[i] + "+");
}
Console.WriteLine(n);
}
}修改别人C++的代码
你要的是下面效果吧
List<int> list1 = new List<int>();
calculate(9, list, 1);
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
foreach (var item in foo(5))
Console.WriteLine(string.Join(" + ", item));
} static IEnumerable<IEnumerable<int>> foo(int n)
{
if (n == 1) return new int[][] { new int[] { 1 } };
return Enumerable.Range(1, n - 1).SelectMany(x => foo(n - x).Select(y => y.Concat(new int[] { x })));
}
}
}1 + 1 + 1 + 1 + 1
1 + 2 + 1 + 1
1 + 1 + 2 + 1
1 + 3 + 1
1 + 1 + 1 + 2
1 + 2 + 2
1 + 1 + 3
1 + 4
Press any key to continue . . .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
foreach (var item in foo(5))
Console.WriteLine(string.Join(" + ", item));
} static IEnumerable<IEnumerable<int>> foo(int n)
{
if (n == 1) return new int[][] { new int[] { 1 } };
return Enumerable.Range(1, n - 1).SelectMany(x => foo(n - x).Where(y => y.Max() <= x).Select(y => y.Concat(new int[] { x })));
}
}
}1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 2
1 + 2 + 2
1 + 1 + 3
1 + 4
Press any key to continue . . .
这是9的:
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1 + 2
1 + 1 + 1 + 1 + 1 + 2 + 2
1 + 1 + 1 + 2 + 2 + 2
1 + 2 + 2 + 2 + 2
1 + 1 + 1 + 1 + 1 + 1 + 3
1 + 1 + 1 + 1 + 2 + 3
1 + 1 + 2 + 2 + 3
1 + 1 + 1 + 3 + 3
1 + 2 + 3 + 3
1 + 1 + 1 + 1 + 1 + 4
1 + 1 + 1 + 2 + 4
1 + 2 + 2 + 4
1 + 1 + 3 + 4
1 + 4 + 4
1 + 1 + 1 + 1 + 5
1 + 1 + 2 + 5
1 + 3 + 5
1 + 1 + 1 + 6
1 + 2 + 6
1 + 1 + 7
1 + 8
Press any key to continue . . .