zoukankan      html  css  js  c++  java
  • poj 2719 Faulty Odometer

    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
    

    Source

     
    对于每个位来说,3等于是不存在的所以说一个位上如果是4,那么他其实是3,如果是5,其实是4,而且每个位只剩下9个数字来表示大小,那不就是九进制吗,所以从低位开始,如果一个位的数字大于3,就减1,然后按九进制来换算。
    c++代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    #include <algorithm>
    #define MAX 0
    #define inf 0x3f3f3f3f
    using namespace std;
    int n;
    int main() {
        while(~scanf("%d",&n) && n) {
            int r = 0,w = n,mi = 1;
            while(w) {
                int d = w % 10;
                if(d > 3)d --;
                r += d * mi;
                w /= 10;
                mi *= 9;
            }
            printf("%d: %d
    ",n,r);
        }
        return 0;
    }

    python代码:

    while (True):
        a = int(input())
        if a == 0:
            break
        wa = a
        ra = 0
        mi = 1
        while a != 0 :
            d = a % 10
            if d > 3 :
                d -= 1
            ra += d * mi
            a = int(a / 10)
            mi *= 9
        print("%d: %d"%(wa,ra))
  • 相关阅读:
    Linux终端设置免密登陆ssh(以 XShell 为例)
    Docker入门(一)-安装
    find命令总结
    CentOS 恢复 rm -rf 误删除数据
    CentOS系统登陆root用户后发现提示符显示-bash-4.2#(已解决)
    一次在CentOS系统单用户模式下使用passwd命令破密失败的案例
    Ubuntu下配置IP地址
    安装CentOS 6.x报错"Disk sda contains BIOS RAID metadata"解决方法
    YUM命令总结
    git从安装到多账户操作一套搞定(二)多账户使用
  • 原文地址:https://www.cnblogs.com/8023spz/p/9512441.html
Copyright © 2011-2022 走看看