Korotkodul

Битик. Шаг в будущее

Oct 30th, 2021 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. string to(int x){
  9.     string rs = "";
  10.     while (x > 0){
  11.         char y = (x % 2) + 48;
  12.         x /= 2;
  13.         rs += y;
  14.     }
  15.     reverse(rs.begin(), rs.end());
  16.     while (rs.size() < 8){
  17.         rs = '0' + rs;
  18.     }
  19.     return rs;
  20. }
  21.  
  22. int fr(string y){
  23.     int sz = y.size();
  24.     reverse(y.begin(), y.end());
  25.     int ans;
  26.     for (int i = 0;i<sz;++i){
  27.         ans += pow(2,i) * (y[i] - '0');
  28.     }
  29.     //cout<<"ans = "<<ans<<'\n';
  30.     return ans;
  31. }
  32.  
  33. //Если число нечётное, то циклически смещаем все двоичные цифры в байте на 2 позиции вправо.
  34. string odd(string x){
  35.     string A,B,C;
  36.     int sz = x.size();
  37.     //cout<<"x = "<<x<<'\n';
  38.     B = x.substr(sz-2);
  39.     A = x.substr(0, sz-2);
  40.     //cout<<"A = "<<A<<'\n';
  41.     //cout<<"B = "<<B<<'\n';
  42.     C = B + A;
  43.     //cout<<"C = "<<C<<'\n';
  44.     return C;
  45. }
  46. //Если число чётное, то смещаем все двоичные цифры в байте на 3 позиции влево нециклически (записываем на образовавшиеся места 0).
  47. string even(string x){
  48.     //cout<<"x = "<<x<<'\n';
  49.     //cout<<"x[3] = "<<x[3]<<'\n';
  50.     string A = x.substr(3);
  51.     //cout<<"A = "<<A<<'\n';
  52.     //cout<<"res = "<<A+"000"<<'\n';
  53.     return A + "000";
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59.     //odd("01101001");
  60.     //fr(odd("01101001"));
  61.     //even(to(106));
  62.     //fr(even(to(106)));
  63.     int a,b;
  64.     cin>>a>>b;
  65.     int mx = -1;
  66.     int ans = -666;
  67.     for (int i =a;i<=b;++i){
  68.         //cout<<"i = "<<i<<'\n';
  69.         string to2, chg; //change
  70.         to2 = to(i);
  71.         int res = -775;
  72.         if (i % 2 == 0){
  73.             chg = even(to2);
  74.         }
  75.         else {
  76.             chg = odd(to2);
  77.         }
  78.         res = fr(chg);
  79.         //cout<<"res = "<<res<<'\n';
  80.         if (res >= mx){
  81.             ans = i;
  82.             mx = res;
  83.         }
  84.     }
  85.     cout<<ans<<'\n';
  86. }
  87. /*
  88. 200 250
  89. 247+
  90. 100 150
  91. 126+
  92. 150 198
  93. 195+
  94. 59 61
  95. 60+
  96. 46 48
  97. 47+
  98. */
  99.  
  100.  
  101.  
Add Comment
Please, Sign In to add comment