HDU 4278 - Faulty Odometer
2015-07-27 11:09
471 查看
Problem Description
You are given a car odometer which displays the milestraveled as an integer. The odometer has a defect, however: it proceeds fromthe digit 2 to the digit 4 and from the digit 7 to the digit 9, always skippingover the digit 3 and 8. This defect shows up
in all positions (the one's, theten's, the hundred's, etc.). For example, if the odometer displays 15229 andthe car travels one mile, odometer reading changes to 15240 (instead of 15230).
Input
Each line of input contains a positive integer in therange 1..999999999 which represents an odometer reading. (Leading zeros willnot appear in the input.) The end of input is indicated by a line containing asingle 0. You may assume that no odometer reading
will contain the digit 3 and8.
Output
Each line of input will produce exactly one line ofoutput, which will contain: the odometer reading from the input, a colon, oneblank 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
思路:
1、 按照每一位多走的路程数打表
2、 把输入看成八进制数
我用的方法1,比赛时没有想到方法2
You are given a car odometer which displays the milestraveled as an integer. The odometer has a defect, however: it proceeds fromthe digit 2 to the digit 4 and from the digit 7 to the digit 9, always skippingover the digit 3 and 8. This defect shows up
in all positions (the one's, theten's, the hundred's, etc.). For example, if the odometer displays 15229 andthe car travels one mile, odometer reading changes to 15240 (instead of 15230).
Input
Each line of input contains a positive integer in therange 1..999999999 which represents an odometer reading. (Leading zeros willnot appear in the input.) The end of input is indicated by a line containing asingle 0. You may assume that no odometer reading
will contain the digit 3 and8.
Output
Each line of input will produce exactly one line ofoutput, which will contain: the odometer reading from the input, a colon, oneblank 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
思路:
1、 按照每一位多走的路程数打表
2、 把输入看成八进制数
我用的方法1,比赛时没有想到方法2
代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int f[12][12]; int a[12]; int main() { int i, j; memset(f, 0, sizeof(f)); for (j = 4; j <= 7; j++) f[0][j] = -1; f[0][9] = -2; f[1][1] = -2; int temp = -1; for (i = 2; i<10; i++) { temp *= 10; f[i][1] = f[i - 1][1] * 8 + temp * 2; } temp = -1; for (i = 1; i<10; i++) { f[i][2] = 2 * f[i][1]; for (j = 4; j <= 7; j++) f[i][j] = (j - 1)*f[i][1] + temp * 10; f[i][9] = 7 * f[i][1] + temp * 20; temp *= 10; } int n; while (scanf("%d", &n) != EOF && n) { int d = 0, sum = n, ori = n; while (n != 0) { a[d++] = n % 10; n /= 10; } for (i = 0; i<d; i++) { sum += f[i][a[i]]; } printf("%d: %d\n", ori, sum); } return 0; }
相关文章推荐
- 电话薄
- JRTPLib的编译步骤
- Android面试宝典(一位优秀应届生面试经验,很多Android高阶问题)
- 面试题 ——— 二维数组的查找
- 编程经验
- 深入javascript: in 和 delete 运算符
- LeeCode(Database)-Combine Two Tables
- 运行该脚本出现/bin/sh^M: bad interpreter: No such file or directory
- 很好的客户端后台博客
- 【MYSQL】导入中文后乱码问题
- 使用fiddler修改请求和响应
- 如何在Eclipse中查看Java类库的源代码以及相应的api
- 获取手机屏幕的宽高
- 认识与入门 Markdown
- 图灵机器人
- 修改Xcode显示行号&缩进距离
- 欢迎使用CSDN-markdown编辑器
- HDOJ 2000 ASCII码排序(水题)(多方法)
- 虚拟机安装Oracle 11g并远程连接
- Word公式字体乱码终极解决(BY 冷家锋)