fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. typedef std::map<double, std::pair<int, int>> DType;
  5.  
  6. DType MakeHoge(std::size_t M){
  7. DType R;
  8. double D = 0;
  9. for (std::size_t i = 2; i <= M; i++){
  10. for (std::size_t j = 1; j < i; j++){
  11. auto P = std::make_pair(j, i);
  12. D = j / static_cast<double>(i);
  13. if (D >= 1) continue;
  14. auto it = R.find(D);
  15. if (it == R.end()) R[D] = P;
  16. }
  17. }
  18.  
  19. return R;
  20.  
  21. }
  22.  
  23. bool Show(DType M){
  24.  
  25. for (auto& o : M)std::cout << o.second.first << '/' << o.second.second << '=' << o.first << std::endl;
  26. return true;
  27. }
  28.  
  29. int main(){
  30.  
  31. DType R;
  32.  
  33. std::cout << "M == 3" << std::endl;
  34. R = MakeHoge(3);
  35. Show(R);
  36. std::cout << "M == 5" << std::endl;
  37. R = MakeHoge(5);
  38. Show(R);
  39.  
  40. return 0;
  41.  
  42. }
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