一个正整数可以表示为多个正整数相加的格式 如 5=1+1+1+1+1,=1+1+1+2,=1+2+2 ,  =1+1+3    =2+3    =1+4,写一个函数求有多少种分拆?求解!!

解决方案 »

  1.   

    补充说明,最简单的计算就是  移位运算 >>  
      

  2.   

      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++的代码
    你要的是下面效果吧
      

  3.   

    调用  
    List<int> list1 = new List<int>();
     calculate(9, list, 1);
      

  4.   

    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 + 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 . . .
      

  5.   

    不考虑顺序:
    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 . . .