110,539
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
var a = new List<int>() { 23, 35, 45 };
var b = new List<int>() { 13, 30, 50 };
Console.WriteLine("{0} vs {1} =>", string.Join(",", a), string.Join(",", b));
foreach (var x in AssignALL(a, b))
{
Console.WriteLine(string.Join(",", x));
}
a = new List<int>() { 14, 26, 43 };
b = new List<int>() { 12, 58 };
Console.WriteLine("{0} vs {1} =>", string.Join(",", a), string.Join(",", b));
foreach (var x in AssignALL(a, b))
{
Console.WriteLine(string.Join(",", x));
}
Console.ReadKey();
}
static IEnumerable<List<int>> AssignALL(List<int> a, List<int> b)
{
foreach (var y in Perm(b.ToArray()))
{
foreach (var x in Perm(a.ToArray()))
{
var r = Assign(x.ToList(), y.ToList());
if (r.Count >= x.Length && !r.Contains(-1))
{
Console.Write(" [{0}]:[{1}] ", string.Join(",", x), string.Join(",", y));
yield return r;
}
}
}
}
static List<int> Assign(List<int> a, List<int> b)
{
if (b.Count == 1) return a;
var sa = a.Sum();
var sb = b.Sum();
if (sa < sb) return new List<int>() { -1 };
var res = new List<int>();
if (a[0] >= b[0])
{
if (sa - a[0] >= sb - b[0])
{
res.Add(a[0]);
res.AddRange(Assign(a.Skip(1).ToList(), b.Skip(1).ToList()));
}
else if (sa - (a[0] - b[0]) >= sb - b[0])
{
res.Add(b[0]);
a[0] = a[0] - b[0];
res.AddRange(Assign(a, b.Skip(1).ToList()));
}
}
return res;
}
static void Swap<T>(ref T a, ref T b)
{
T t = a; a = b; b = t;
}
static IEnumerable<int[]> Perm(int[] arr, int pos=0)
{
if (pos == arr.Length) { yield return arr; }
for (int i = pos; i < arr.Length; ++i)
{
Swap(ref arr[i], ref arr[pos]);
foreach (var j in Perm(arr, pos + 1)) yield return j;
Swap(ref arr[i], ref arr[pos]);
}
}
private void test()
{
int[] param1 = { 23, 35, 45 };
int[] param2 = { 13, 30, 50 };
Array.Sort(param1);
Array.Sort(param2);
Dictionary<int, int> dic1 = new Dictionary<int, int>();
Dictionary<int, int> dic2 = new Dictionary<int, int>();
for (int i = 0; i < param1.Length; i++)
{
int item = param1[i] - param2[i];
if (item > 0)
dic1.Add(i, item);
else
dic2.Add(i, item);
}
Dictionary<int, int> dic1_SortedByKey = dic1.OrderByDescending(p => p.Value).ToDictionary(p => p.Key, o => o.Value);
Dictionary<int, int> dic2_SortedByKey = dic2.OrderByDescending(p => p.Value).ToDictionary(p => p.Key, o => o.Value);
int negative = 0;
foreach (var items in dic2_SortedByKey)
negative += -1 * items.Value;
int positive = 0;
List<int> list = new List<int>(dic1_SortedByKey.Keys);
int count = dic1_SortedByKey.Count;
for (int j = 0; j < dic1_SortedByKey.Count; j++)
{
if (positive < negative)
positive += dic1_SortedByKey[list[j]];
else
count = j;
}
int lastNum = positive - negative;
Dictionary<int, int> dic3 = new Dictionary<int, int>();
Dictionary<int, int> dic4 = new Dictionary<int, int>();
List<int> list1 = new List<int>(dic2_SortedByKey.Keys);
for (int x = 0; x < count; x++)
{
int key = list[x];
dic3.Add(key, param2[key] - param1[key]);
}
int key1 = 0;
for (int y = 0; y < dic2_SortedByKey.Count; y++)
{
key1 = list1[y];
dic4.Add(key1, param2[key1] - param1[key1]);
}
dic4[key1] = dic4[key1] + lastNum;
var query = dic3.Union(dic4);
foreach (KeyValuePair<int, int> obj in query)
{
Console.WriteLine(param1[obj.Key] + " " + obj.Value);
}
}