Advertisement
Alexander_B

Histogram

Feb 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 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 DomashnoHistogram
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14.  
  15. // count numbers
  16.  
  17. int p1 = 0; // <200
  18. int p2 = 0; // >=200 <=399
  19. int p3 = 0; // >=400 <=599
  20. int p4 = 0; // >=600 <=799
  21. int p5 = 0; // >=800
  22.  
  23. for (int i = 0; i < n; i++)
  24. {
  25. int input = int.Parse(Console.ReadLine());
  26.  
  27. if (input < 200)
  28. {
  29. p1++;
  30. }
  31. else if (input >= 200 && input <= 399)
  32. {
  33. p2++;
  34. }
  35. else if (input >= 400 && input <= 599)
  36. {
  37. p3++;
  38. }
  39. else if (input >= 600 && input <= 799)
  40. {
  41. p4++;
  42. }
  43. else
  44. {
  45. p5++;
  46. }
  47. }
  48.  
  49. // calculate percentage
  50.  
  51. int pSum = p1 + p2 + p3 + p4 + p5;
  52.  
  53. double p1Percent = 0.0;
  54. double p2Percent = 0.0;
  55. double p3Percent = 0.0;
  56. double p4Percent = 0.0;
  57. double p5Percent = 0.0;
  58.  
  59. if (p1 > 0)
  60. {
  61. p1Percent = 1.00 * p1 / pSum * 100;
  62. }
  63.  
  64. if (p2 > 0)
  65. {
  66. p2Percent = 1.00 * p2 / pSum * 100;
  67. }
  68.  
  69. if (p3 > 0)
  70. {
  71. p3Percent = 1.00 * p3 / pSum * 100;
  72. }
  73.  
  74. if (p4 > 0)
  75. {
  76. p4Percent = 1.00 * p4 / pSum * 100;
  77. }
  78.  
  79. if (p5 > 0)
  80. {
  81. p5Percent = 1.00 * p5 / pSum * 100;
  82. }
  83.  
  84.  
  85. // print
  86.  
  87. Console.WriteLine($"{p1Percent:f2}%");
  88. Console.WriteLine($"{p2Percent:f2}%");
  89. Console.WriteLine($"{p3Percent:f2}%");
  90. Console.WriteLine($"{p4Percent:f2}%");
  91. Console.WriteLine($"{p5Percent:f2}%");
  92.  
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement