本文整理汇总了C#中HashSet.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# HashSet.Sum方法的具体用法?C# HashSet.Sum怎么用?C# HashSet.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashSet
的用法示例。
在下文中一共展示了HashSet.Sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
Dictionary<int, int> sums = new Dictionary<int, int>();
sums.Add(0, 0);
foreach (int n in Enumerable.Range(1, 10000))
{
if(sums.ContainsKey(n)== false)
{
sums.Add(n, GetSumOfDivisors(n));
}
if(sums.ContainsKey(sums[n]) == false)
{
sums.Add(sums[n], GetSumOfDivisors(sums[n]));
}
}
HashSet<int> amicables = new HashSet<int>();
foreach (var item in sums)
{
if (item.Key > 10000)
continue;
if(item.Key == sums[item.Value] && item.Key != item.Value)
{
amicables.Add(item.Key);
amicables.Add(item.Value);
}
}
Console.WriteLine(amicables.Sum());
Console.ReadLine();
}
示例2: Main
static void Main()
{
var digits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var matching = new HashSet<int>();
var perms = digits.GetPermutations();
Action<int[], int,int> a = (perm, i, j) =>
{
var multiplicand = DigitsToInt(perm, 0, i - 1);
var multiplier = DigitsToInt(perm, i, i + j - 1);
var product = DigitsToInt(perm, i + j, perm.Length - 1);
if (multiplicand * multiplier == product)
{
matching.Add(product);
}
};
foreach (var perm in perms)
{
var permArray = perm.ToArray();
a(permArray,1, 4);
a(permArray,2, 3);
}
Console.WriteLine(matching.Sum());
Console.ReadLine();
}
示例3: Main
void Main()
{
String digits = "123456789";
permutation("", digits);
HashSet<int> pandigitalNumbers = new HashSet<int>();
foreach (string s in numbers)
{
int three = Convert.ToInt32(s.Substring(0, 3));
int two = Convert.ToInt32(s.Substring(3, 2));
int one = Convert.ToInt32(s.Substring(0, 1));
int four = Convert.ToInt32(s.Substring(1, 4));
int total = Convert.ToInt32(s.Substring(5, 4));
if (three * two == total || one * four == total)
{
Console.WriteLine("ADDING: " + s);
pandigitalNumbers.Add(total);
}
}
Console.WriteLine(pandigitalNumbers.Sum());
}
示例4: Main
static void Main(string[] args)
{
int N = int.Parse(Console.ReadLine());
HashSet<House> Houses = new HashSet<House>();
long amountOfCable = 0;
for (int i = 0; i < N; i++)
{
string[] inputs = Console.ReadLine().Split(' ');
Houses.Add(new House { X = int.Parse(inputs[0]), Y = int.Parse(inputs[1]) });
}
//Core: Calculate Average House Y index
double Avg = Houses.Sum(x => x.Y) / N;
//Core:find the house closest to the Avg Y and use its Y coordinate
int closest = Houses.OrderBy(x => Math.Abs(x.Y - Avg)).First().Y;
//lay the mainline
amountOfCable += (Houses.Max(x => x.X) - Houses.Min(x => x.X));
//per other House calculate distance from location to mainline
foreach (var i in Houses)
{
amountOfCable += i.Y > closest ? i.Y - closest : closest - i.Y;
}
// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");
Console.WriteLine(amountOfCable);
}
示例5: Main
static void Main()
{
var collection = new HashSet<double> { 5.2, 8, -3.14, 0, 55 };
Console.WriteLine(collection.Sum());
Console.WriteLine(collection.Product());
Console.WriteLine(collection.Min());
Console.WriteLine(collection.Max());
Console.WriteLine(collection.Average());
}
示例6: S
public int S(int n)
{
var set = new HashSet<int>();
for(int k=2;k<=n;k++)
{
set.Add(mps(k));
}
return set.Sum();
}
示例7: Main
private string Main()
{
permutation("", "123456789");
HashSet<int> pandigitalNumbers = new HashSet<int>();
foreach (string s in numbers.AsParallel().Where(
x => Convert.ToInt32(x.Substring(0, 3)) * Convert.ToInt32(x.Substring(3, 2)) == Convert.ToInt32(x.Substring(5, 4))
|| Convert.ToInt32(x.Substring(0, 1)) * Convert.ToInt32(x.Substring(1, 4)) == Convert.ToInt32(x.Substring(5, 4))))
pandigitalNumbers.Add(Convert.ToInt32(s.Substring(5, 4)));
return pandigitalNumbers.Sum().ToString();
}
示例8: Solve
public long Solve()
{
var primes = new Utils.Prime(51L);
var squereFree = new HashSet<long>();
for (var row = 1; row <= 50; row++)
{
for (var number = 1; number <= row; number++)
{
squereFree.Add(this.GetSquereFreeBinomilaNumber(number, row, primes));
}
}
return squereFree.Sum();
}
示例9: GetSumOfDivisors
private static int GetSumOfDivisors(int n)
{
int sq = (int) Math.Sqrt(n);
HashSet<int> divisors = new HashSet<int>();
for(int i = 1; i <= sq; i++)
{
if(n % i == 0)
{
divisors.Add(i);
divisors.Add(n / i);
}
}
divisors.Remove(n);
return divisors.Sum();
}
示例10: CalculateSum
public int CalculateSum(int upperLimit)
{
var uniqueMultiples = new HashSet<int>();
for (int i = 0; i < upperLimit; i++)
{
if (i%3 == 0 || i%5 == 0)
{
uniqueMultiples.Add(i);
}
}
var sum = uniqueMultiples.Sum();
return sum;
}
示例11: P032
public P032 ()
{
ulong max = 987654321 / 2;
var all = new HashSet<ulong> ();
for (ulong a = max; a > 0; a--) {
if (false
|| (a % 10000000 == 0)
|| (a < 10000000 && a % 100000 == 0)
|| (a < 1000000 && a % 10000 == 0)
|| (a < 100000 && a % 1000 == 0)
|| (a < 10000 && a % 100 == 0)
|| (a < 1000 && a % 10 == 0)
|| (a < 100 && a % 1 == 0)
) {
Console.WriteLine ("a = " + a);
}
if (0 == Pandigital.Is1to9Pandigital (a)) {
continue;
}
if (a % 10 == 1) {
// x1 * yz = ??z
continue;
}
for (ulong b = a; b <= max / a; b++) {
if (b % 10 == 1) {
// x1 * yz = ??z, thus can never be pandigital
continue;
}
ulong c = a * b;
var pan = Pandigital.Is1to9Pandigital (a, b, c);
if (9 != pan) {
continue;
}
all.Add (c);
Console.WriteLine (a + " x " + b + " = " + c + "\t\ttotal: " + all.Count + ", sum " + all.Sum (x => (long) x));
}
}
}
示例12: Solve
public void Solve() {
HashSet<int> p = new HashSet<int>(); //guarantees uniqueness of items
foreach (var i in 2.To(100)) {
int start = 1234;
if (i > 9)
start = 123;
foreach (var j in start.To(10000 / i + 1)) {
string s = (i.ToString() + j.ToString() + (i * j).ToString());
if (s.ToLong().IsPanDigital()) {
p.Add(i * j);
}
}
}
Util.WL("The sum of all products whose multiplicand/multiplier/product\nidentity can be written as a 1 through 9 pandigital is {0}"
.FormatWith(p.Sum()));
}
示例13: Solve
public long Solve()
{
var values = new HashSet<long> { 1 };
for (var number = 2L; number <= Math.Sqrt(Limit); number++)
{
var powerSpecial = number * number;
var sumSpecial = number + powerSpecial;
while ((sumSpecial + 1) <= Limit)
{
values.Add(sumSpecial + 1);
powerSpecial = powerSpecial * number;
sumSpecial = sumSpecial + powerSpecial;
}
}
return values.Sum();
}
示例14: Answer
public static int Answer()
{
var d = Util.Memoize<int, int>(n => MathEx.Divisors(n).Where(i => i != n).Sum());
var amicableNumbers = new HashSet<int>();
int max = 9999;
for (int a = 1; a <= max; a++)
{
for (int b = a; b <= max; b++)
{
if (a != b && d(a) == b && d(b) == a)
{
amicableNumbers.Add(a);
amicableNumbers.Add(b);
}
}
}
return amicableNumbers.Sum();
}
示例15: Main
static void Main(string[] args)
{
HashSet<int> amicableNumbers = new HashSet<int>();
for (int i = 220; i < 10000; i++)
{
if (amicableNumbers.Contains(i)) continue;
int sum = Factorize(i).Sum();
int sumCheck = Factorize(sum).Sum();
if (sumCheck == i && sum != i)
{
Console.WriteLine(i + " " + sum);
amicableNumbers.Add(i);
amicableNumbers.Add(sum);
}
}
Console.WriteLine();
Console.WriteLine(amicableNumbers.Sum());
Console.ReadKey();
}