题解 | #称砝码#
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
long size = long.Parse(Console.ReadLine().Trim());
long[] weightArray = Console.ReadLine().Trim().Split(' ').ToArray().Select(s => long.Parse(s)).ToArray();
long[] countArray = Console.ReadLine().Trim().Split(' ').ToArray().Select(s => long.Parse(s)).ToArray();
Dictionary<long,List<long>>dic = new Dictionary<long,List<long>>();
for (long i = 0; i < size; i++)
{
long weight = weightArray[i];
long count = countArray[i];
List<long> list = new List<long>();
//可能是0-n个
for (long n = 0; n <= count; n++)
{
list.Add(weight * n);
}
dic.Add(weight, list);
}
HashSet<long> resultSet = new HashSet<long>();
foreach (var item in dic)
{
HashSet<long> childSet = item.Value.ToHashSet();
MergeHashSet(resultSet, childSet);
}
System.Console.WriteLine(resultSet.Count);
}
public static void MergeHashSet(HashSet<long> fullSet, HashSet<long> childSet)
{
HashSet<long> tempSet= fullSet.ToHashSet();
foreach (var big in tempSet)
{
foreach (var small in childSet)
{
fullSet.Add(big+small);
}
}
foreach (var small in childSet)
{
fullSet.Add(small);
}
}
}
查看10道真题和解析