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

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跳到4,从7跳到9,现在给出里程表的数字,求其真实的里程。
思路:该里程表对每一位的计数应为
1 2 4 5 6 7 9
        1 2 3 4 5 6 7(若不看数值,相当于只能记录1-7,即八进制,因此大致思路是将显示数字转换为八进制,再转换为十进制) 里程表显示的每位数字,如果大于3,则说明该位跳过了一个数字,应将该位数字减1;如果等于9,说明之前跳过了两个数,该位应为7

code:

#include<iostream>
#include<math.h>
using namespace std;
int digit[20];
int main(){
	int n;
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		int tmp=n;
		int sum=0;
		int index=0;
		while(n){
			digit[index]=n%10;
			if(digit[index]==9){
				digit[index]=7;
			}
			else if(digit[index]>3){
				digit[index]--;
			}
			n/=10;
			index++;
		}
		
		for(int i=index-1;i>=0;i--){
		    sum+=digit[i]*(int)pow(8,index-1);
			index--;	
		}
		cout<<tmp<<": "<<sum<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值