Advertisement
AndryS1

Untitled

Sep 25th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. std::vector<double> gearbox_values = { -1, 1, 6.0 / 5, 3.0 / 2, 9.0 / 5, 2.0, 5.0 / 2, 3.0, 5.0 / 6, 2.0 / 3, 5.0 / 9, 1.0 / 2, 2.0 / 5, 1.0 / 3 };
  9. std::vector<std::string> gearbox_strings = { "1:-1", "1:1", "6:5", "3:2", "9:5", "2:1", "5:2", "3:1", "5:6", "2:3", "5:9", "1:2", "2:5", "1:3" };
  10.  
  11. size_t myPow(size_t x, size_t p)
  12. {
  13.     if (p == 0) return 1;
  14.     if (p == 1) return x;
  15.  
  16.     size_t tmp = myPow(x, p / 2);
  17.     if (p % 2 == 0) return tmp * tmp;
  18.     else return x * tmp * tmp;
  19. }
  20.  
  21.  
  22. std::string value_to_str(double value) {
  23.     const double fault = 0.001;
  24.     for (size_t i = 0; i < gearbox_values.size(); i++) {
  25.         if (std::fabs(value - gearbox_values[i]) < fault) {
  26.             return gearbox_strings[i];
  27.         }
  28.     }
  29.     return "unknown";
  30. }
  31.  
  32. std::pair<std::vector<double>, double> find_nearest_gearbox(double target_value, size_t gearbox_count) {
  33.     std::vector<double> closest_combination;
  34.     double closest_product = 0.0;
  35.     double min_difference = std::numeric_limits<double>::max();
  36.  
  37.     std::vector<double> combination(gearbox_count, 1);
  38.     size_t total_combinations = myPow(gearbox_values.size(), gearbox_count);
  39.  
  40.     for (size_t i = 0; i < total_combinations; ++i) {
  41.         size_t index = i;
  42.         for (int j = 0; j < gearbox_count; ++j) {
  43.             combination[j] = gearbox_values[index % gearbox_values.size()];
  44.             index /= gearbox_values.size();
  45.         }
  46.  
  47.         double product = 1.0;
  48.         for (const auto& value : combination) {
  49.             product *= value;
  50.         }
  51.  
  52.         double difference = std::fabs(product - target_value);
  53.  
  54.         if (difference < min_difference) {
  55.             min_difference = difference;
  56.             closest_combination = combination;
  57.             closest_product = product;
  58.         }
  59.     }
  60.  
  61.     return { closest_combination, closest_product };
  62. }
  63.  
  64. int main(int argc, char** argv) {
  65.     double target_value;
  66.     size_t gearbox_count;
  67.     bool interactive = false;
  68.     if (argc >= 3) {
  69.         target_value = std::atof(argv[1]);
  70.         gearbox_count = std::atoi(argv[2]);
  71.     }
  72.     else
  73.     {
  74.         interactive = true;
  75.         std::cout << "target_value: ";
  76.         std::cin >> target_value;
  77.         std::cout << "gearbox_count: ";
  78.         std::cin >> gearbox_count;
  79.         std::cout.put('\n');
  80.     }
  81.    
  82.     auto [closest_combination, closest_product] = find_nearest_gearbox(target_value, gearbox_count);
  83.  
  84.     std::cout << "Closest combination: ";
  85.     for (const auto& value : closest_combination) {
  86.         std::cout << value_to_str(value) << " ";
  87.     }
  88.     std::cout << "\nClosest product: " << closest_product << std::endl;
  89.     if (interactive) {
  90.         system("pause");
  91.     }
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement