Winner
Autumn Tournament (2008)
Submission name: winner.cpp
Problem summary
Transform a three-digit number. Add its hundreds and tens digits; if this sum has two digits, add those digits again to get the first output digit. If the original units digit is even, double it; otherwise add the original tens digit to it. This result supplies the last two output digits, with a leading zero when needed.
Input
One three-digit positive integer k.
Output
The resulting three-digit integer.
Constraints
100 ≤ k ≤ 999.
Worked example
Original statement sample.
Input
387
Output
215
Solution analysis
- Extract hundreds with k/100, tens with (k/10)%10, and units with k%10. Keep these original digits for both rules.
- The hundreds-plus-tens sum is at most 18, so one further digit sum is sufficient. Form the last part by testing the parity of the original units digit. Output 100 × first + last.
- For 387, 3 + 8 = 11 reduces to 2. The units digit 7 is odd, so the last part is 7 + 8 = 15. The answer is 215.
Why it works
Digit extraction yields exactly the three original digits. Each branch implements the corresponding parity rule, and the optional second sum gives the prescribed first digit. Multiplying it by 100 leaves the final two places for the computed last part.
Watch out. Do not use the modified digit sum instead of the original tens digit. A last part such as 8 occupies two places: 624 becomes 808.
Commented C++17 solution
An independently written complete program with standard headers.
// Key idea: Digit extraction yields exactly the three original digits. Each branch implements the
// corresponding parity rule, and the optional second sum gives the prescribed first digit.
#include<algorithm>
#include<iostream>
#include<limits>
#include<string>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int number;
cin>>number;
int hundreds=number/100;
int tens=(number/10)%10;
int units=number%10;
int first=hundreds+tens;
if(first>=10){
first=first/10+first%10;
}
int last=(units%2==0)?2*units:units+tens;
cout<<100*first+last<<'\n';
}