sicily 1240. Faulty Odometer

1240. Faulty Odometer

Constraints

Time Limit: 1 secs, Memory Limit: 32 MBios

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 3 to the digit 5, always skipping over the digit 4. This defect shows up in all positions (the one's, the ten's, the hundred's, etc.). For example, if the odometer displays 15339 and the car travels one mile, odometer reading changes to 15350 (instead of 15340).git

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 4.app

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

Sample Input

13
15
2003
2005
239
250
1399
1500
999999
0

Sample Output

13: 12
15: 13
2003: 1461
2005: 1462
239: 197
250: 198
1399: 1052
1500: 1053
999999: 531440
此題題意爲將一個不用4來進行計數的十進制數轉換爲一個真實的十進制數,解答本題關鍵爲:首先須要得到該原始輸入數據對應十進制數的每一位的數字,而後對其進行轉換爲該真實的十進制數,實際上將原始數據看作一個九進制數,當某位上的數字大於5時,在轉換爲真實的十進制的過程當中,將該數字減一,而後若是其爲百位,則乘上pow(9,2),即9的平方,對應十進制的10的平方,例子:250:2*9*9+4*9+0*1 = 198

  

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
//得到十進制數的每一位 
void getDigitBit(long long digit, vector<int>& digitBits) {
	while (digit != 0) {
		int bit = digit % 10;
		digitBits.push_back(bit);
		digit /= 10;
	}
} 
int main() {
	long long odometer;
	
	while (cin >> odometer && odometer != 0) {
		vector<int> digitBits;
		long long actual_m = 0;
		getDigitBit(odometer, digitBits);
		int countBits = digitBits.size();
		
		for (int i = digitBits.size()-1; i >= 0; i--) {
			//若是位數大於5時,則減一 
			if (digitBits[i] >= 5) {
				digitBits[i]--;
			}
			//實際爲九進制計算問題 
			actual_m += digitBits[i] * pow(9, --countBits);
		} 
		cout << odometer << ": " << actual_m<< endl;
	}
	return 0;
} 
相關文章
相關標籤/搜索