fork download
  1. // TheTrip.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <math.h>
  8. #include <iomanip>
  9. #include <string>
  10. using namespace std;
  11.  
  12. double smoother(double raw_ave)
  13. {
  14. string::size_type n;
  15. string raw_string = to_string(raw_ave);
  16. n = raw_string.find(".");
  17. string decimal = raw_string.substr(n);
  18.  
  19. double new_ave = 0;
  20.  
  21. if (decimal.length() >= 5)
  22. {
  23.  
  24. string cmp = decimal.substr(0, 5);
  25. if (cmp == ".5000")
  26. {
  27. new_ave = floor(raw_ave);
  28. }
  29. else if (cmp == ".3333")
  30. {
  31. new_ave = floor(raw_ave);
  32. }
  33. else if (cmp == ".6666")
  34. {
  35. new_ave = floor(raw_ave);
  36. new_ave = new_ave + 0.66;
  37. }
  38. else
  39. {
  40. new_ave = raw_ave;
  41. }
  42. }
  43. else
  44. {
  45. new_ave = raw_ave;
  46. }
  47.  
  48. return new_ave;
  49. }
  50.  
  51. string calculate(int n, vector<double> v)
  52. {
  53.  
  54. double sum = 0;
  55.  
  56. for (int i = 0; i < n; i++)
  57. {
  58. v[i] = v[i] * 100;
  59. sum += v[i];
  60. }
  61.  
  62. double ave = sum / (double)n;
  63.  
  64. double new_ave = smoother(ave);
  65.  
  66. double result = 0;
  67. for (double diff : v)
  68. {
  69. if (diff < new_ave)
  70. {
  71. result += new_ave - diff;
  72. }
  73. }
  74.  
  75. string wholeResult = to_string((int)result);
  76. if (wholeResult.length() == 1)
  77. {
  78. wholeResult = "00" + wholeResult;
  79. }
  80. else if (wholeResult.length() == 2)
  81. {
  82. wholeResult = "0" + wholeResult;
  83. }
  84. string decimal = wholeResult.substr(wholeResult.length() - 2);
  85. wholeResult = wholeResult.substr(0, wholeResult.length() - 2);
  86.  
  87.  
  88. cout << "$" << wholeResult << "." << decimal << endl;
  89.  
  90. string output;
  91. output = "$" + wholeResult + "." + decimal;
  92. return output;
  93. }
  94. int main()
  95. {
  96. int n = 0;
  97. while (cin >> n)
  98. {
  99. if (n == EOF) break;
  100. if (n <= 0) break;
  101. vector<double> v;
  102. int counter = n;
  103. while (counter > 0)
  104. {
  105. double f;
  106. cin >> f;
  107. v.push_back(f);
  108. counter--;
  109. }
  110. calculate(n, v);
  111. }
  112. return 0;
  113. }
  114.  
Success #stdin #stdout 0s 3480KB
stdin
3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
5
5000.00
11.11
11.11
11.11
11.11
3
0.01
0.03
0.03
4
25.00
25.00
25.00
28.00
3
10.01
15.25
18.96
4
25.03
25.00
25.00
25.00
21 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.01 
0.03 
12 
123.12 
6.13 
9.44 
89.08 
278.78 
223.78 
78.45 
912.89 
554.76 
547.57 
1781.89 
907.07 
0
stdout
$10.00
$11.99
$3991.11
$0.01
$2.25
$4.73
$0.02
$0.01
$2407.09