Created
March 14, 2020 17:00
-
-
Save sandiks/686089587109d4661d3349d9588cf34a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace Console.MathTester | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int N = 1000_000; | |
float[] acc = new float[N]; | |
Random randNum = new Random(); | |
for (int i = 0; i < acc.Length; i++) | |
{ | |
acc[i] = randNum.Next(10, 100_000); | |
} | |
var base_level = (float)acc.Sum() / acc.Count(); | |
var sw = Stopwatch.StartNew(); | |
for (int i = 0; i < 100; i++) | |
{ | |
var surplus = ProcessFatAccounts(acc, base_level); | |
ProcessPoorAccounts(acc, base_level, surplus); | |
System.Console.WriteLine($"[ {i}] снято с жирных счетов #{surplus}"); | |
} | |
System.Console.WriteLine($"elapsed {sw.Elapsed}"); | |
} | |
private static float ProcessFatAccounts(float[] acc, float base_level) | |
{ | |
float Sum = 0; | |
for (int i = 0; i < acc.Length; i++) | |
{ | |
var mm = acc[i]; | |
if (mm > base_level) | |
{ | |
float f = (float)Math.Pow(mm / base_level, 1.3); | |
if (acc[i] - f > base_level) | |
{ | |
acc[i] -= f; | |
Sum += f; | |
} | |
} | |
} | |
return Sum; | |
} | |
private static void ProcessPoorAccounts(float[] acc, float BaseLevel, float Benefits) | |
{ | |
float SmallAmountLimit(float ff) => ff > 20 ? 20 : ff; | |
float AllFactors = 0; | |
for (int i = 0; i < acc.Length; i++) | |
{ | |
var mm = acc[i]; | |
if (mm < BaseLevel) AllFactors += SmallAmountLimit(BaseLevel / mm); | |
} | |
var AmountToAdd = Benefits / AllFactors; | |
for (int i = 0; i < acc.Length; i++) | |
{ | |
var mm = acc[i]; | |
if (mm < BaseLevel) | |
{ | |
var f = SmallAmountLimit(BaseLevel / mm) * AmountToAdd; | |
acc[i] += f; | |
} | |
} | |
} | |
} | |
} | |
elapsed 00:00:05.9377225 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment