fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdint>
  4. #include <tuple>
  5. #include <string>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <map>
  9.  
  10. typedef std::tuple<std::uint64_t, std::uint64_t> Data;
  11. typedef std::vector<Data> DType;
  12. typedef std::map<double, DType> MType;
  13. typedef std::vector<std::string> SType;
  14.  
  15. bool CheckInput(std::string & S){
  16. char Ch[] = "01234567890 /";
  17. bool F = false;
  18.  
  19. for (std::size_t i = 0; i < S.size(); i++){
  20. F = false;
  21. for (auto& o : Ch){
  22. if (S[i] == o){
  23. F = true;
  24. break;
  25. }
  26. }
  27. if (F == false) return false;
  28. }
  29. return true;
  30. }
  31. std::uint64_t gcd(std::uint64_t a, std::uint64_t b){
  32. return b == 0 ? a : gcd(b, a % b);
  33. }
  34.  
  35.  
  36. SType GetInput(){
  37. std::string S;
  38. SType ST;
  39.  
  40. while (std::cin >> S){
  41. if (CheckInput(S) == true){
  42. ST.push_back(S);
  43. }
  44. else{
  45. std::cout << S << " is BAD INPUT!" << std::endl;
  46. }
  47. }
  48.  
  49. return ST;
  50. }
  51.  
  52. Data MakeData(std::string& S){
  53. std::stringstream SS;
  54. std::uint64_t A = 0;
  55. std::uint64_t B = 0;
  56. std::uint64_t C = 0;
  57. char Ch = '\0';
  58.  
  59. SS << S;
  60.  
  61. if (S.find_first_of(' ') != -1)SS >> A;
  62. SS >> B;
  63. SS >> Ch;
  64. SS >> C;
  65.  
  66. B += A*C;
  67.  
  68. return std::make_tuple(B, C);
  69.  
  70. }
  71.  
  72. MType MakeHoge(SType &S){
  73. MType M;
  74. Data D;
  75. for (auto& o : S){
  76. D = MakeData(o);
  77. M[std::get<0>(D) / static_cast<double>(std::get<1>(D))].push_back(D);
  78. }
  79.  
  80. return M;
  81. }
  82.  
  83. bool Show(MType& M){
  84.  
  85. std::uint64_t A = 0;
  86. std::uint64_t B = 0;
  87. std::uint64_t C = 0;
  88. for (auto& oo : M){
  89. A = std::get<0>(oo.second[0]) / std::get<1>(oo.second[0]);
  90. B = std::get<0>(oo.second[0]) % std::get<1>(oo.second[0]);
  91. C = gcd(std::get<0>(oo.second[0]), std::get<1>(oo.second[0]));
  92. C = std::max<std::uint64_t>(1, C);
  93. std::cout << '[';
  94. if (A != 0) std::cout << A;
  95. if (A != 0 && B != 0) std::cout << '+';
  96. if (B != 0) std::cout <<(std::get<0>(oo.second[0])-A*std::get<1>(oo.second[0])) / C << '/' << std::get<1>(oo.second[0]) / C;
  97. if (A == 0 && B == 0) std::cout << 0;
  98. std::cout << ']';
  99. for (auto o : oo.second){
  100. A = std::get<0>(o) / std::get<1>(o);
  101. B = std::get<0>(o) % std::get<1>(o);
  102. if (A != 0 && B!=0) std::cout << A << '+';
  103. if (B != 0){
  104. std::cout << (std::get<0>(o)-A*std::get<1>(o)) << '/' << std::get<1>(o) << ',';
  105. }else if (B != 0 || A != 0){
  106. std::cout << std::get<0>(o) << '/' << std::get<1>(o) << ',';
  107. }
  108. if (A == 0 && B == 0) std::cout << 0;
  109. }
  110. std::cout << std::endl;
  111. }
  112.  
  113. return true;
  114. }
  115.  
  116. int main(){
  117. SType S{ "1/2", "1/3", "2/3", "1 1/3", "2/2", "3/2", "4/2", "2/8", "9/6","2/4" };
  118. MType M;
  119.  
  120. //S = GetInput();
  121. M = MakeHoge(S);
  122. Show(M);
  123.  
  124. return 0;
  125. }
Success #stdin #stdout 0s 3284KB
stdin
Standard input is empty
stdout
[1/4]2/8,
[1/3]1/3,
[1/2]1/2,2/4,
[2/3]2/3,
[1]2/2,
[1+1/3]1+1/3,
[1+1/2]1+1/2,1+3/6,
[2]4/2,