45 lines
798 B
C++
45 lines
798 B
C++
#include <iostream>
|
|
using namespace std;
|
|
|
|
int getMinIncrease(string num) {
|
|
int res = 0;
|
|
for (int i = 0; i < num.length(); i++) {
|
|
res += num[i] - '0';
|
|
}
|
|
|
|
res = res % 3;
|
|
return 3 - res;
|
|
}
|
|
|
|
|
|
int findPosToChange(string num, int INC) {
|
|
for (int i = 0; i < num.length(); i++) {
|
|
if (num[i] != '9' && num[i] + INC <= '9') {
|
|
return i;
|
|
}
|
|
}
|
|
return num.length() - 1;
|
|
}
|
|
|
|
|
|
int main() {
|
|
string num;
|
|
cin >> num;
|
|
|
|
const int INC = getMinIncrease(num);
|
|
const int POS = findPosToChange(num, INC);
|
|
|
|
// cout << INC << " " << POS << endl;
|
|
if (num[POS] + INC <= '9') {
|
|
int extra_space = (9 - (num[POS] - '0') - INC);
|
|
num[POS] = num[POS] + INC + extra_space - (extra_space % 3);
|
|
} else if (3 - INC != 0) {
|
|
num[POS] -= 3 - INC;
|
|
} else {
|
|
num[POS] -= 3;
|
|
}
|
|
|
|
cout << num << endl;
|
|
|
|
return 0;
|
|
} |