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

猜你喜欢

转载自blog.csdn.net/yx970326/article/details/80223881
E
e'e
e4e