Advertisement
mateuspl

URI: 2013 - No Máximo Duas Vezes

Feb 9th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     string number;
  8.     cin >> number;
  9.  
  10.     int ocurr[10] = {0};
  11.     int greater(9);
  12.  
  13.     string::iterator c = number.begin();
  14.     int n = *c - '0';
  15.  
  16.     while (ocurr[n] < 2 && c != number.end())
  17.     {
  18.         ocurr[n]++;
  19.         c++;
  20.         n = *c - '0';
  21.     }
  22.  
  23.     if (c != number.end())
  24.     {
  25.         while (n > 0 && ocurr[n] == 2)
  26.         {
  27.             (*c)--;
  28.             n--;
  29.         }
  30.  
  31.         while (n == 0 && ocurr[0] == 2 && c != number.begin())
  32.         {
  33.             do
  34.             {
  35.                 c--;
  36.                 n = *c - '0';
  37.                 ocurr[n]--;
  38.             }
  39.             while (n == 0);
  40.  
  41.             do
  42.             {
  43.                 (*c)--;
  44.                 n--;
  45.             }
  46.             while (n > 0 && ocurr[n] == 2);
  47.         }
  48.  
  49.         if (c != number.begin())
  50.             ocurr[*c - '0']++;
  51.  
  52.         for (c++; c != number.end(); c++)
  53.         {
  54.             while (ocurr[greater] == 2)
  55.                 greater--;
  56.  
  57.             *c = greater + '0';
  58.             ocurr[greater]++;
  59.         }
  60.     }
  61.  
  62.     const char* str = number.c_str();
  63.     cout << (str[0] == '0' ? str + 1 : str) << endl;
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement