2019, Republic, 11 day 2, numere solution added
This commit is contained in:
45
2019/republic/11_d2/numere.cpp
Normal file
45
2019/republic/11_d2/numere.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user