Description
You is given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however:it proceeds from the digit 3 to the digit 5, always skipping over the digit 4. This defect shows up and all positions (the one ' s, the ten ' s, the Hundred ' s, etc). For example, if the odometer displays 15339 and the car travels one mile, odometer reading changes to 15350 (instead of 15 340).
Input
Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros won't appear in the input.) The end of input is indicated by a line containing a single 0. Assume that no odometer reading would contain the digit 4.
Output
Each line of input would produce exactly one line of output, which would contain:the odometer reading from the input, a col On, one blank space, and the actual number of miles traveled by the car.
Explanation: The car's odometer cannot pass the 4 number, that is to say 3 the next number is 5. Ask us to calculate the actual mileage based on the number of miles displayed
How to solve the problem: for displaying miles, we can actually think of it as a 9-digit number, because the number of bits per bit is only 9 0,1,2,3,5,6,7,8,9. First, the number is converted to a true 9-digit number, that is, a number greater than 4, minus 1. If 250 translates into a true 9 binary number is 240. Then convert the 9 binary number to the 10 binary number to get the actual mileage.
For this problem, processing the data in a character-processing way. In c++11, you can use the Auto keyword already scoped for to access each character of a string. for (auto C:stringa)
However, the Sicily compiler does not support C++11 and therefore does not support the use of auto and the use of scopes for, where the subscript can be used to access
for (String::size_type i = 0; i < stringa.size (); i++) {
Stringa[i]
}
#include <iostream> #include <string>using namespace std;int main (int argc, const char * argv[]) { //Inser T code here ... string Innum; while (CIN >> innum) { if (innum = = "0") break; string nonary; for (String::size_type i = 0; i < innum.size (); i++) { if (Innum[i] > ' 4 ') nonary + = Innum[i]-1; Data will be processed into 9 binary else nonary + = Innum[i]; } Long unsigned result = 0; for (String::size_type i = 0; i < nonary.size (); i + +) { //9 conversion to 10 binary result = result * 9 + (Nonary[i]-' 0 ' ); } cout << innum << ":" << result << Endl; } return 0;}
<oj_sicily>1240faulty_odometer