Description
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.
题目解释:汽车的里程表不能经过4这个数字,也就是说3的下一个数字是5.要求我们根据显示里程数求出实际里程数
解题思路:对于显示里程数,我们其实可以看作是一个9进制数,因为每个位的位数只有0,1,2,3,5,6,7,8,9这9个数。首先将这个数转化为真正的9进制数,也就是对于大于4 的数字,进行减1。如250转化为真正的9进制数是240.然后将9进制数转化为10进制数便可以得到实际的里程数。
对于这道题,采用字符处理的方式处理数据。在C++11可以使用auto关键字已经范围for来对字符串的每个字符进行访问。for(auto c: stringA)
但是Sicily编译器不支持C++11,因此也不支持auto的使用以及范围for的使用,在这里可以使用下标的方式访问
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[]) { // insert 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; // 将数据处理成9进制 else nonary += inNum[i]; } long unsigned result = 0; for (string::size_type i = 0; i < nonary.size(); i ++) { // 9进制转换为10进制 result = result * 9 + (nonary[i] - '0'); } cout << inNum << ": " << result << endl; } return 0; }