题目:Faulty Odometer
问题描述
你将得到一个汽车里程表,它以整数形式显示行驶的英里数。然而,里程表有一个缺陷:它从数字3转到数字5,总是跳过数字4。这个缺陷出现在所有的位置(1,10,100,等等)。例如,如果里程表显示为15339,而汽车行驶了1英里,里程表的读数将更改为15350(而不是15340)。
Problem 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.
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
Problem Source
Rocky Mountain 2005
分析:这是一个简单的进制转化问题,因为该题目这么叙述:遇到4则直接从3进位到5,所以这是一道典型的进制转化问题,遍历该数字的每一位(如果这一位的数字比4大,就要减去1,因为这个位跳过了一个4),然后再遍历该数字的每一位,将其转化为10进制。
所以这是一道典型的进制转化问题。
附上代码#include
#include
using namespace std;
int main()
{
string n;
for(int i=0;;i++)
{
cin>>n;
if(n[0]==‘0’)
{
break;
}
int j=n.size();
long long num=0,ans=1;
for(int k=j-1;k>=0;k–)
{
if((n[k]-‘0’)>4)
num+=((n[k]-‘0’)-1)*ans;
else
{
num+=(n[k]-‘0’)ans;
}
ans=ans9;
}
cout<<n<<": "<<num<<endl;
}
}