thomasfn

Untitled

Apr 10th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MathsShit
  8. {
  9.     class Program
  10.     {
  11.         struct RatingSet : IEquatable<RatingSet>
  12.         {
  13.             public int P1, P2, P3;
  14.  
  15.             public IEnumerable<RatingSet> GetPermutations()
  16.             {
  17.                 yield return new RatingSet { P1 = P1, P2 = P3, P3 = P2 };
  18.                 yield return new RatingSet { P1 = P2, P2 = P1, P3 = P3 };
  19.                 yield return new RatingSet { P1 = P2, P2 = P3, P3 = P1 };
  20.                 yield return new RatingSet { P1 = P3, P2 = P1, P3 = P2 };
  21.                 yield return new RatingSet { P1 = P3, P2 = P2, P3 = P1 };
  22.             }
  23.  
  24.             public int BiggestDiff
  25.             {
  26.                 get
  27.                 {
  28.                     return Math.Max(Math.Max(Math.Abs(P1 - P2), Math.Abs(P1 - P3)), Math.Abs(P2 - P3));
  29.                 }
  30.             }
  31.  
  32.             public override string ToString()
  33.             {
  34.                 return string.Format("{0}/10, {1}/10, {2}/10", P1, P2, P3);
  35.             }
  36.  
  37.             public MoneySet ToMoney()
  38.             {
  39.                 float sum = P1 + P2 + P3;
  40.                 return new MoneySet
  41.                 {
  42.                     P1 = (int)(P1 * 10000 / sum),
  43.                     P2 = (int)(P2 * 10000 / sum),
  44.                     P3 = (int)(P3 * 10000 / sum)
  45.                 };
  46.             }
  47.  
  48.             public bool Equals(RatingSet other)
  49.             {
  50.                 return P1 == other.P1 && P2 == other.P2 && P3 == other.P3;
  51.             }
  52.         }
  53.  
  54.         struct MoneySet
  55.         {
  56.             public int P1, P2, P3;
  57.  
  58.             public RatingSet JakesMethod()
  59.             {
  60.                 return new RatingSet
  61.                 {
  62.                     P1 = P1 / 500,
  63.                     P2 = P2 / 500,
  64.                     P3 = P3 / 500
  65.                 };
  66.             }
  67.         }
  68.  
  69.         static void Main(string[] args)
  70.         {
  71.             HashSet<RatingSet> considered = new HashSet<RatingSet>();
  72.             int cnt = 0;
  73.             for (int i = 1; i < 10; i++)
  74.             {
  75.                 for (int j = 1; j < 10; j++)
  76.                 {
  77.                     for (int k = 1; k < 10; k++)
  78.                     {
  79.                         RatingSet ratings = new RatingSet { P1 = i, P2 = j, P3 = k };
  80.                         MoneySet money = ratings.ToMoney();
  81.                         RatingSet jakeratings = money.JakesMethod();
  82.                         if (ratings.Equals(jakeratings) && jakeratings.BiggestDiff <= 2)
  83.                         {
  84.                             bool duplicate = false;
  85.                             foreach (var perm in jakeratings.GetPermutations())
  86.                             {
  87.                                 if (considered.Contains(perm))
  88.                                 {
  89.                                     duplicate = true;
  90.                                     break;
  91.                                 }
  92.                             }
  93.                             if (!duplicate)
  94.                             {
  95.                                 considered.Add(ratings);
  96.                                 Console.WriteLine("Permutation found! {0}", ratings);
  97.                                 cnt++;
  98.                             }
  99.                         }
  100.                     }
  101.                 }
  102.  
  103.             }
  104.             Console.WriteLine("End of testing. {0} permutations found.", cnt);
  105.             Console.ReadKey();
  106.  
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment