fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <tuple>
  5.  
  6. typedef std::vector < std::tuple<int, int, double>> DType;
  7.  
  8. void AddAnswer(double d, std::size_t A, std::size_t B, DType& R){
  9. std::size_t i = 0;
  10. for (i = 0; i < R.size(); i++){
  11. if (std::get<2>(R[i]) == d) return;
  12. if (std::get<2>(R[i]) > d) break;
  13. }
  14.  
  15. for (int j = R.size(); j < i; j--){
  16. R[j] = R[j - 1];
  17. }
  18.  
  19. R.push_back(std::make_tuple(A, B, d));
  20. return;
  21. }
  22.  
  23. DType F170(std::size_t N){
  24.  
  25. DType R;
  26.  
  27. for (std::size_t i = 2; i <= N; i++){
  28. for (std::size_t j = 1; j < i; j++){
  29. AddAnswer(j / static_cast<double>(i), j, i, R);
  30. }
  31. }
  32.  
  33. for (std::size_t i = 0; i <= R.size(); i++){
  34. for (std::size_t j = i; j < R.size(); j++){
  35. if (std::get<2>(R[i])>std::get<2>(R[j]))std::swap(R[i], R[j]);
  36. }
  37. }
  38.  
  39.  
  40. return R;
  41. }
  42.  
  43. bool Show(DType& vec){
  44. for (auto& o : vec){
  45. std::cout << std::get<0>(o) << '/' << std::get<1>(o) << '=' << std::get<2>(o)<<std::endl;
  46. }
  47. return true;
  48. }
  49.  
  50. int main(){
  51.  
  52. DType R;
  53. R = F170(3);
  54. std::cout << "M => 3"<<std::endl;
  55. Show(R);
  56.  
  57. R = F170(5);
  58. std::cout << "M => 5"<<std::endl;
  59. Show(R);
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
M => 3
1/3=0.333333
1/2=0.5
2/3=0.666667
M => 5
1/5=0.2
1/4=0.25
1/3=0.333333
2/5=0.4
1/2=0.5
3/5=0.6
2/3=0.666667
3/4=0.75
4/5=0.8