[Kick Start 2022 Round A] Challenge Nine

Kick Start 2022 Round A 2022 Challenge Nine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;
string solve(string& num) {
int sum = 0;
for(auto ch : num) {
sum = (sum + (ch & 0b1111)) % 9;
}
int append = (9 - sum) % 9;
if(append == 0)
return string(1,num[0]) + "0" + num.substr(1);
for(int i = 0; i < num.length(); i++) {
if((num[i] & 0b1111) > append)
return num.substr(0,i) + to_string(append) + num.substr(i);
}
return num + to_string(append);
}
int main() {
int t;
cin>>t;
for(int i = 1; i <= t; i++) {
string n;
cin>>n;
cout<<"Case #"<<i<<": "<< solve(n)<<endl;
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/20/PS/Google/challenge-nine/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.