diff --git a/2019/republic/11_d2/numere.cpp b/2019/republic/11_d2/numere.cpp new file mode 100644 index 0000000..32b7797 --- /dev/null +++ b/2019/republic/11_d2/numere.cpp @@ -0,0 +1,45 @@ +#include +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; +} \ No newline at end of file