您的位置:首页 > 编程语言 > C语言/C++

Faulty Odometer

2015-09-14 00:16 274 查看
A - Faulty Odometer
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
4278

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 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). 

 

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

 

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

15
2005
250
1500
999999
0

 

Sample Output

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

 

题意:
   就是一个汽车的里程表其中3,8是不会出现的。如2+1!=3等于4。
思路:
    一开始都不知道怎么做,认为是dp吧。结果一直不行。最后问了一下同学,才知道是十进制转八进制。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int fx(int n)
{
int k=1;
while(n)
{
k*=8;n--;
}
return k;
}
int main()
{
int k,i,j,n;
char s[15];
while(scanf("%s",&s)&&s[0]!='0')
{
n=0;
for(i=0;s[i];++i){
n=n*10+s[i]-'0';
if(s[i]>'8')
s[i]-=2;
else if(s[i]>'3')
s[i]-=1;
}j=strlen(s)-1;
for(i=0,k=0;s[i];++i)
k+=(s[i]-'0')*fx(j-i);
printf("%d: %d\n",n,k);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++