我想計算不同類型天數的數量,這些天數可以等於我的設定值。測驗:計算組合天數
例如,如果某人有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);
}
}
}
}
是的,可以,但你可以添加你的嘗試,也。 –
你必須添加你的嘗試/努力... – Rayon
好...................... – PriceCheaperton