zoukankan      html  css  js  c++  java
  • sicily 1240. Faulty Odometer

    1240. Faulty Odometer

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

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

    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.

    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

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

      

  • 相关阅读:
    多线程的创建方式
    ArrayList 初探
    java创建对象的几种方式
    select2动态查询及多选
    RabbitMQ 消息发送、消息监听
    tr命令
    集群,分布式,微服务概念和区别理解
    xargs命令
    shell中的EOF用法
    字段分隔符IFS
  • 原文地址:https://www.cnblogs.com/xieyizun-sysu-programmer/p/4116069.html
Copyright © 2011-2022 走看看