You are 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 in 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 15340).
Input
Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros will not appear in the input.) The end of input is indicated by a line containing a single 0. You may assume that no odometer reading will contain the digit 4.
Output
Each line of input will produce exactly one line of output, which will contain: the odometer reading from the input, a colon, one blank space, and the actual number of miles traveled by the car.
Sample Input
13 15 2003 2005 239 250 1399 1500 999999 0
Sample Output
13: 12 15: 13 2003: 1461 2005: 1462 239: 197 250: 198 1399: 1052 1500: 1053 999999: 531440
//把4之后的数都减一然后就是真正的9进制数
#include <iostream>
#include <string>
using namespace std;
string s;
int main() {
while(cin >> s && s != "0")
{
int tmp;
int k = 1;
int i = s.size() - 1;
tmp = s[i] - '0';
if(s[i] > '4') tmp--;
for(i--; i >= 0; i--)
{
k *= 9;
if(s[i] > '4') tmp += (s[i] - '0' - 1) * k;
else tmp += (s[i] - '0') * k;
}
cout << s << ": " << tmp << endl;
}
return 0;
}
本文介绍了一款特殊的里程计算器程序,该程序处理一个显示已行驶英里的汽车里程表读数,但由于缺陷,在所有位置(个位、十位、百位等)从数字3跳到数字5,省略了4。文章提供了输入输出示例及实现代码。
1070

被折叠的 条评论
为什么被折叠?



