2015
05-23

# Faulty Odometer

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 2 to the digit 4 and from the digit 7 to the digit 9, always skipping over the digit 3 and 8. This defect shows up in all positions (the one’s, the ten’s, the hundred’s, etc.). For example, if the odometer displays 15229 and the car travels one mile, odometer reading changes to 15240 (instead of 15230).

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 3 and 8.

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 3 and 8.

15
2005
250
1500
999999
0

15: 12
2005: 1028
250: 160
1500: 768
999999: 262143

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int n;
char num[15];
while(scanf("%s",num))
{
if(num[0]=='0')
break;
int l=strlen(num);
int i;
int sum=0;
for(i=0;i<l;i++)
{
if(num[i]<'3')
sum+=(int)(num[i]-'0')*pow(8.0,l*1.0-i-1);
else if(num[i]<'8')
sum+=(int)(num[i]-'0'-1)*pow(8.0,l*1.0-i-1);
else
sum+=(int)(num[i]-'0'-2)*pow(8.0,l*1.0-i-1);
}
printf("%s: %d\n",num,sum);
}
}