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