C#数学里面 求拆一个数一个方法

言多必失 2013-01-29 05:29:40

现在想实现一个功能, OutPut(16, new int[]{1,2,4});
由1,2,4数字 组成和为16的所有情况,

比如:
16个1,
8个2,
4个4,
14个1和1个2
10个1和1个2和1个4
8个1 和2个2和1个4
........

主要想看看



/// <summary>
/// 求一个数 有些数的之和组成的排列
/// </summary>
/// <param name="sum">和数</param>
/// <param name="array">构成的数字</param>
/// <returns>返回能够组成的一个集合</returns>
public static List<List<int>> OutPut(int sum, int[] array)
{

List<List<int>> result = new List<List<int>>();
//实现过程
return result;

}
...全文
568 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
异常异长 2013-08-27
  • 打赏
  • 举报
回复
引用 6 楼 caozhy 的回复:
包装下,和你的接口一致
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<int>> result = OutPut(16, new int[] { 1, 2, 4 });
            foreach (var item in result)
                Console.WriteLine(string.Join(",", item.Select(x => x.ToString())));
        }

        /// <summary>
        /// 求一个数 有些数的之和组成的排列
        /// </summary>
        /// <param name="sum">和数</param>
        /// <param name="array">构成的数字</param>
        /// <returns>返回能够组成的一个集合</returns>
        public static List<List<int>> OutPut(int sum, int[] array)
        {
            return OutPut1(sum, array, new List<int>()).Select(x => x.ToList()).ToList();
        }

        private static IEnumerable<IEnumerable<int>> OutPut1(int sum, int[] array, IEnumerable<int> seed)
        {
            if (seed.Sum() == sum)
                yield return seed;
            else
                foreach (var item in array.Where(x => x >= seed.LastOrDefault()).Select(x => seed.Concat(new int[] { x })).Where(x => x.Sum() <= sum).SelectMany(x => OutPut1(sum, array, x)))
                    yield return item;

        }
    }
}
确实很牛逼 好好看看linq
threenewbee 2013-08-26
  • 打赏
  • 举报
回复
这个可以结贴啦。
言多必失 2013-01-30
  • 打赏
  • 举报
回复
引用 12 楼 hjywyj 的回复:
感觉这题应该再加个限制条件吧,比如:每个数不能重复多少次或数组Count不超过几个 sum=9,array= new int[] { 3, 2, 4, -1 }//这样就无限循环了
嗯,那都是外层处理过的,最里面核心实现代码就是上面那个方法, 你考虑的全 还是不错的。
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
引用 12 楼 hjywyj 的回复:
感觉这题应该再加个限制条件吧,比如:每个数不能重复多少次或数组Count不超过几个 sum=9,array= new int[] { 3, 2, 4, -1 }//这样就无限循环了
确实,要考虑参数范围。
  • 打赏
  • 举报
回复
感觉这题应该再加个限制条件吧,比如:每个数不能重复多少次或数组Count不超过几个 sum=9,array= new int[] { 3, 2, 4, -1 }//这样就无限循环了
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
Sum Concat LastOrDefault还是属于Linq,不过你可以进一步展开,比如 if (seed.Sum() == sum) yield return seed; 写成 int _sum = 0; foreach (int x in seed) _sum = _sum + x; if (_sum == sum) yield return seed; 等等。
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
把Linq代码展开。 Linq是C#语法的一部分,它本身没有意义,只是简化代码的书写而已,只要你懂Linq,展开很简单。
private static IEnumerable<IEnumerable<int>> OutPut1(int sum, int[] array, IEnumerable<int> seed)
{
    if (seed.Sum() == sum)
        yield return seed;
    else
        foreach (var item in array)
        {
            if (item >= seed.LastOrDefault())
            {
                var newseed = seed.Concat(new int[] { item });
                if (newseed.Sum() <= sum)
                {
                    var col = OutPut1(sum, array, newseed);
                    foreach (var item1 in col)
                        yield return item1;
                }
            }
        }
}
言多必失 2013-01-29
  • 打赏
  • 举报
回复
引用 7 楼 caozhy 的回复:
引用 5 楼 llkaximoduo 的回复: 引用 3 楼 caozhy 的回复:C# code?123456789101112131415161718192021222324252627282930313233using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace……
上面说写了100行,还只是一个方法的,都加起来还不止。 如果不用Linq的话,想看看你是如何实现。
言多必失 2013-01-29
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
public static IEnumerable<List<int>> OutPut(int sum, int[] array, IEnumerable<int> seed)
这个方法确实好,对楼主的能力从来没有怀疑过,LINQ这样熟悉, 我写了快100行,你就3,5行搞的,我情何以堪。
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
引用 5 楼 llkaximoduo 的回复:
引用 3 楼 caozhy 的回复:C# code?123456789101112131415161718192021222324252627282930313233using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleAppl……
简单来说2个字,递归。 求sum,array排列组合的问题,可以转化为 对于每个array的元素,求(sum为sum-这个元素)的组合
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
包装下,和你的接口一致
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<int>> result = OutPut(16, new int[] { 1, 2, 4 });
            foreach (var item in result)
                Console.WriteLine(string.Join(",", item.Select(x => x.ToString())));
        }

        /// <summary>
        /// 求一个数 有些数的之和组成的排列
        /// </summary>
        /// <param name="sum">和数</param>
        /// <param name="array">构成的数字</param>
        /// <returns>返回能够组成的一个集合</returns>
        public static List<List<int>> OutPut(int sum, int[] array)
        {
            return OutPut1(sum, array, new List<int>()).Select(x => x.ToList()).ToList();
        }

        private static IEnumerable<IEnumerable<int>> OutPut1(int sum, int[] array, IEnumerable<int> seed)
        {
            if (seed.Sum() == sum)
                yield return seed;
            else
                foreach (var item in array.Where(x => x >= seed.LastOrDefault()).Select(x => seed.Concat(new int[] { x })).Where(x => x.Sum() <= sum).SelectMany(x => OutPut1(sum, array, x)))
                    yield return item;

        }
    }
}
llkaximoduo 2013-01-29
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
C# code?123456789101112131415161718192021222324252627282930313233using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ c……
看着很困难,大神能否讲解下?
llkaximoduo 2013-01-29
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
C# code?123456789101112131415161718192021222324252627282930313233using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ c……
膜拜!!!
threenewbee 2013-01-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{    
    class Program
    {
        static void Main(string[] args)
        {
            var result = OutPut(16, new int[] { 1, 2, 4 }, new List<int>());
            foreach (var item in result)
                Console.WriteLine(string.Join(",", item.Select(x => x.ToString())));
        }

        /// <summary>
        /// 求一个数 有些数的之和组成的排列
        /// </summary>
        /// <param name="sum">和数</param>
        /// <param name="array">构成的数字</param>
        /// <returns>返回能够组成的一个集合</returns>
        public static IEnumerable<List<int>> OutPut(int sum, int[] array, IEnumerable<int> seed)
        {
            if (seed.Sum() == sum)
                yield return seed.ToList();
            else
                foreach (var item in array.Where(x => x >= seed.LastOrDefault()).Select(x => seed.Concat(new int[] { x })).Where(x => x.Sum() <= sum).SelectMany(x => OutPut(sum, array, x)))
                    yield return item;

        }
    }
}
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 1,1,1,1,1,1,1,1,1,1,1,1,2,2 1,1,1,1,1,1,1,1,1,1,1,1,4 1,1,1,1,1,1,1,1,1,1,2,2,2 1,1,1,1,1,1,1,1,1,1,2,4 1,1,1,1,1,1,1,1,2,2,2,2 1,1,1,1,1,1,1,1,2,2,4 1,1,1,1,1,1,1,1,4,4 1,1,1,1,1,1,2,2,2,2,2 1,1,1,1,1,1,2,2,2,4 1,1,1,1,1,1,2,4,4 1,1,1,1,2,2,2,2,2,2 1,1,1,1,2,2,2,2,4 1,1,1,1,2,2,4,4 1,1,1,1,4,4,4 1,1,2,2,2,2,2,2,2 1,1,2,2,2,2,2,4 1,1,2,2,2,4,4 1,1,2,4,4,4 2,2,2,2,2,2,2,2 2,2,2,2,2,2,4 2,2,2,2,4,4 2,2,4,4,4 4,4,4,4 Press any key to continue . . .
言多必失 2013-01-29
  • 打赏
  • 举报
回复
忘记说明,最好是效率高点的,自己写了一个感觉不是很好。

111,090

社区成员

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

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

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