Problem Description
You are given a car odometer which displays the milestraveled as an integer. The odometer has a defect, however: it proceeds fromthe digit 2 to the digit 4 and from the digit 7 to the digit 9, always skippingover the digit 3 and 8. This defect shows up in all positions (the one's, theten's, the hundred's, etc.). For example, if the odometer displays 15229 andthe car travels one mile, odometer reading changes to 15240 (instead of 15230).
Input
Each line of input contains a positive integer in therange 1..999999999 which represents an odometer reading. (Leading zeros willnot appear in the input.) The end of input is indicated by a line containing asingle 0. You may assume that no odometer reading will contain the digit 3 and8.
Output
Each line of input will produce exactly one line ofoutput, which will contain: the odometer reading from the input, a colon, oneblank space, and the actual number of miles traveled by the car.
Sample Input
15
2005
250
1500
999999
0
Sample Output
15: 12
2005: 1028
250: 160
1500: 768
999999: 262143
思路:
1、 按照每一位多走的路程数打表
2、 把输入看成八进制数
我用的方法1,比赛时没有想到方法2
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[12][12];
int a[12];
int main()
{
int i, j;
memset(f, 0, sizeof(f));
for (j = 4; j <= 7; j++)
f[0][j] = -1;
f[0][9] = -2;
f[1][1] = -2;
int temp = -1;
for (i = 2; i<10; i++)
{
temp *= 10;
f[i][1] = f[i - 1][1] * 8 + temp * 2;
}
temp = -1;
for (i = 1; i<10; i++)
{
f[i][2] = 2 * f[i][1];
for (j = 4; j <= 7; j++)
f[i][j] = (j - 1)*f[i][1] + temp * 10;
f[i][9] = 7 * f[i][1] + temp * 20;
temp *= 10;
}
int n;
while (scanf("%d", &n) != EOF && n)
{
int d = 0, sum = n, ori = n;
while (n != 0)
{
a[d++] = n % 10;
n /= 10;
}
for (i = 0; i<d; i++)
{
sum += f[i][a[i]];
}
printf("%d: %d\n", ori, sum);
}
return 0;
}