QUIZ:计算天数组合

本文关键字:数组 组合 计算 QUIZ | 更新日期: 2023-09-27 17:59:14

我想计算不同类型的天数,这些天数等于我的设定值。

例如,如果有人在那里工作,有30天的年假,我想计算一下他们可以休什么样的假期。

例如:

5、10、5、2、2、1、5

正如你所看到的,以上将等于30。

计算的目的是让未来的员工知道他们可以休什么样的假。

返回的值也可以是:

10,10,10

这意味着我需要计算与年假总数相等的数字组合。

挑战可以用任何编程语言完成!

我尝试过以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // find all possible combinations of this list
            var input = new[] { "1", "2", "3", "4", "5", "6","7","8","9","10","11","12","13","14" };
            var output = FastPowerSet(input);
            Print(output);
            Console.ReadLine();
        }
        static T[][] FastPowerSet<T>(T[] seq)
        {
            var powerSet = new T[1 << seq.Length][];
            powerSet[0] = new T[0]; // starting only with empty set
            for (var i = 0; i < seq.Length; i++)
            {
                var cur = seq[i];
                var count = 1 << i; // doubling list each time
                for (var j = 0; j < count; j++)
                {
                    var source = powerSet[j];
                    var destination = powerSet[count + j] = new T[source.Length + 1];
                    for (var q = 0; q < source.Length; q++)
                        destination[q] = source[q];
                    destination[source.Length] = cur;
                }
            }
            return powerSet;
        }
        static void Print<T>(T[][] seq)
        {
            for (var i = 0; i < seq.Length; i++)
            {
                var line = new StringBuilder();
                for (var j = 0; j < seq[i].Length; j++)
                {
                    line.AppendFormat("{0}, ", seq[i][j]);
                }
                Console.WriteLine(line);
            }
        }
    }
}

QUIZ:计算天数组合

感谢PriceCheaperton!

一个更广泛的问题已经得到了回答这里也可以给出给定和的部分的大小的限制。

在上面链接顶部的Python解决方案中,您可以修改对函数的调用,以解决您的特定示例(我不知道15..30是否有效,否则您需要像我开始时一样完成列表):

subset_sum([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15..30],30)

谨致问候,Mats