Faulty Odometer

A - Faulty Odometer
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值