Advertisement
Dang_Quan_10_Tin

XEPHANG HSGTPVinh L9 2019-2020

Jan 8th, 2022
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #define task "XEPHANG"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 50 + 5;
  12. struct Number
  13. {
  14.     string s;
  15.  
  16.     // Nhập dữ liệu
  17.     friend istream &operator>>(istream &in, Number &x)
  18.     {
  19.         in >> x.s;
  20.         return in;
  21.     }
  22.  
  23.     // Xuất dữ liệu
  24.     friend ostream &operator<<(ostream &out, Number &x)
  25.     {
  26.         out << x.s;
  27.         return out;
  28.     }
  29.  
  30.     bool operator<(const Number &a)
  31.     {
  32.         int sign1 = 1, sign2 = 1;
  33.         int piv1 = 0, piv2 = 0;
  34.  
  35.         string Int1, Real1,
  36.             Int2, Real2;
  37.  
  38.         if (s[0] == '-')
  39.         {
  40.             sign1 = -1;
  41.             ++piv1;
  42.         }
  43.         if (a.s[0] == '-')
  44.         {
  45.             sign2 = -1;
  46.             ++piv2;
  47.         }
  48.  
  49.         // Trái dấu
  50.         if (sign1 < sign2)
  51.             return true;
  52.         else if (sign1 > sign2)
  53.             return false;
  54.  
  55.         while (piv1 < (int)s.size() && s[piv1] != '.')
  56.             Int1.push_back(s[piv1++]);
  57.  
  58.         while (piv2 < (int)a.s.size() && a.s[piv2] != '.')
  59.             Int2.push_back(a.s[piv2++]);
  60.  
  61.         // Phần nguyên khác nhau
  62.         if (Int1.size() > Int2.size())
  63.             return (sign1 == -1);
  64.         else if (Int1.size() < Int2.size())
  65.             return (sign1 == 1);
  66.         else if (Int1 > Int2)
  67.             return (sign1 == -1);
  68.         else if (Int1 < Int2)
  69.             return (sign1 == 1);
  70.  
  71.         ++piv1;
  72.         ++piv2;
  73.  
  74.         while (piv1 < (int)s.size())
  75.             Real1.push_back(s[piv1++]);
  76.         while (piv2 < (int)a.s.size())
  77.             Real2.push_back(a.s[piv2++]);
  78.  
  79.         // Phần thực có thể có các số 0 vô nghĩa
  80.         while (!Real1.empty() && Real1.back() == '0')
  81.             Real1.pop_back();
  82.         while (!Real2.empty() && Real2.back() == '0')
  83.             Real2.pop_back();
  84.  
  85.         // Phần thực khác nhau
  86.         if (Real1.size() > Real2.size())
  87.             return (sign1 == -1);
  88.         else if (Real1.size() < Real2.size())
  89.             return (sign1 == 1);
  90.         else if (Real1 > Real2)
  91.             return (sign1 == -1);
  92.         else if (Real1 < Real2)
  93.             return (sign1 == 1);
  94.  
  95.         // Hai số bằng nhau
  96.         return false;
  97.     }
  98.  
  99. } k, a[N];
  100. int n;
  101.  
  102. void Read()
  103. {
  104.     cin >> n >> k;
  105.  
  106.     for (int i = 1; i <= n; ++i)
  107.         cin >> a[i];
  108. }
  109.  
  110. void Solve()
  111. {
  112.     for (int i = 1; i <= n; ++i)
  113.         if (k < a[i])
  114.         {
  115.             // Đã tìm được một đáp án
  116.             cout << i << "\n";
  117.  
  118.             for (int j = 1; j < i; ++j)
  119.                 cout << a[j] << " ";
  120.             cout << k << " ";
  121.  
  122.             for (int j = i; j <= n; ++j)
  123.                 cout << a[j] << " ";
  124.             return;
  125.         }
  126.  
  127.     cout << n + 1 << "\n";
  128.     for (int i = 1; i <= n; ++i)
  129.         cout << a[i] << " ";
  130.     cout << k;
  131. }
  132.  
  133. int32_t main()
  134. {
  135.     ios::sync_with_stdio(0);
  136.     cin.tie(0);
  137.     cout.tie(0);
  138.     if (fopen(task ".INP", "r"))
  139.     {
  140.         freopen(task ".INP", "r", stdin);
  141.         freopen(task ".OUT", "w", stdout);
  142.     }
  143.  
  144.     Read();
  145.     Solve();
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement