Advertisement
Guest User

Untitled

a guest
Jun 14th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. int numSwimmers = 16;
  8. int numLanes = 4;
  9.  
  10. int[,] results = new int[numSwimmers, numSwimmers];
  11. int[] strengths = new int[numSwimmers];
  12.  
  13. GenerateResults(numSwimmers, numLanes, results);
  14.  
  15. InitializeStrengths(numSwimmers, results, strengths);
  16.  
  17. // Best 5 swimmers
  18. int[] topSwimmers = GetTopSwimmers(numSwimmers, strengths);
  19.  
  20. // 1 to 5 place
  21.  
  22. for (int i = 0; i < 5; i++)
  23. {
  24. Console.WriteLine($"Place {i + 1}: Swimmer {topSwimmers[i]}");
  25. }
  26. }
  27.  
  28. static void GenerateResults(int numSwimmers, int numLanes, int[,] results)
  29. {
  30. // Generate random result
  31. Random random = new Random();
  32.  
  33. for (int i = 0; i < numSwimmers; i++)
  34. {
  35. for (int j = i + 1; j < numSwimmers; j++)
  36. {
  37. int winner = random.Next(2); // 0 - i winner, 1 - j winner
  38. results[i, j] = winner;
  39. results[j, i] = 1 - winner;
  40. }
  41. }
  42. }
  43.  
  44. static void InitializeStrengths(int numSwimmers, int[,] results, int[] strengths)
  45. {
  46. for (int i = 0; i < numSwimmers; i++)
  47. {
  48. strengths[i] = 0;
  49. }
  50.  
  51. for (int i = 0; i < numSwimmers; i++)
  52. {
  53. for (int j = 0; j < numSwimmers; j++)
  54. {
  55. if (i != j && results[i, j] == 1)
  56. {
  57. strengths[i]++;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. static int[] GetTopSwimmers(int numSwimmers, int[] strengths)
  64. {
  65. // Best 5 swimmers
  66.  
  67. int[] topSwimmers = new int[5];
  68.  
  69. for (int i = 0; i < 5; i++)
  70. {
  71. int maxStrength = -1;
  72. int maxIndex = -1;
  73.  
  74. for (int j = 0; j < numSwimmers; j++)
  75. {
  76. if (strengths[j] > maxStrength)
  77. {
  78. maxStrength = strengths[j];
  79. maxIndex = j;
  80. }
  81. }
  82.  
  83. topSwimmers[i] = maxIndex;
  84. strengths[maxIndex] = -1;
  85. }
  86.  
  87. return topSwimmers;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement