【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

  • 解题思路
    遇见4则直接跳到5,那么我们可以理解为这个题中数字的自增方式为1,2,3,5,6,7,8,9,0;那么我们是否可以看成1,2,3,4,5,6,7,8,0只不过是后面大于4的全部减一而已。
    看到1,2,3,4,5,6,7,8,0仔细一想这不是9进制的自增方式吗,本题数据在自增的过程中缺少一个4那就相当与是九进制转换为十进制。只不过需要考虑每位数字是否大于4,大于4的需要减1;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int fun(long long n)
{long long sum=0;int y=1;while(n){int x = n%10;if(x>4)x--;sum+=x*y;y*=9;n/=10;}return sum;
}int main()
{long long x;while(cin >> x && x){cout << x << ": " << fun(x) << endl;}return 0;
}

【POJ 2719 --- Faulty Odometer】相关推荐

  1. 【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宫问题】

    POJ3126Prime Path 给定两个四位素数a  b,要求把a变换到b 变换的过程要 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数  与  前一步得到的素数  只能有一个位 ...

  2. 【poj题集整理】【存下来并不会看】

    主要是整理起来自己用的.网上有多个版本. 初级: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)   ...

  3. 【POJ/算法】 3259 Wormholes(Bellman-Ford算法, SPFA ,FLoyd算法)

    Bellman-Ford算法 Bellman-Ford算法的优点是可以发现负圈,缺点是时间复杂度比Dijkstra算法高.而SPFA算法是使用队列优化的Bellman-Ford版本,其在时间复杂度和编 ...

  4. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  5. BZOJ 2287 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...

  6. 【POJ 3026】Borg Maze

    [POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...

  7. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  8. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...

  9. 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)

    2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...

  10. bzoj2287【POJ Challenge】消失之物 缺一01背包

    bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...

最新文章

  1. MYSQL备份与恢复精华篇
  2. 【错误记录】Android Studio 导入外部 so 动态库报错 ( java.lang.UnsatisfiedLinkError | 指定 APK 打包动态库的 CPU 架构 )
  3. 随时随地能写代码, vscode.dev 出手了
  4. php 数组对比 unset,如何区分PHP中unset,array_splice的区别
  5. html:(40):块级元素和内联块级元素
  6. 最新研究显示人类智力逐渐下降且变得更愚蠢
  7. 《从零开始走进FPGA》导读
  8. Laravel系列教程一:安装及环境配置
  9. 高响应比优先调度算法(HRRN)例题详解
  10. 学习java之前应该先了解哪些知识?
  11. wps怎么关闭那个登录界面_关闭wps窗口快捷键是什么?
  12. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。...
  13. 修改游戏服务器ip,修改游戏 服务器ip地址
  14. 北京中医药大学远程教育 计算机基础考试,北京中医药大学远程教育计算机基础(2008版)-第4次...
  15. 人工智能+名片,我们看到了营销圈中的“阿尔法
  16. 以卖单车为例形象理解23种设计模式
  17. Redis系列之——Redis-Cluster
  18. 使用asp.net从零开始制作设计网站---转载
  19. 使用Typora工具绘制流程图
  20. 主成分分析二级指标权重_主成分分析法 - 确定多因素影响权重

热门文章

  1. codeforces-703(好题)
  2. 保龄球积分c语言程序,C语言保龄球积分.doc
  3. Channel 用法
  4. 武大三行情书第一名---《螃蟹在剥我的壳》
  5. 微信小程序获取收货地址流程
  6. 奇迹mu技术分享:奇迹服务端中的【DATA】文件详细说明
  7. 语言栏多出EN、JP等键盘布局的解决方案
  8. c语言临时内存变量释放,C语言中的内存分配与释放
  9. 百度地图API申请——服务端
  10. 写一下安装composer,以及composer安装依赖包时候遇到的烦人问题吧