<OJ_Sicily>1240Faulty_Odometer
2016-05-10 16:35
288 查看
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.
题目解释:汽车的里程表不能经过4这个数字,也就是说3的下一个数字是5.要求我们根据显示里程数求出实际里程数
解题思路:对于显示里程数,我们其实可以看作是一个9进制数,因为每个位的位数只有0,1,2,3,5,6,7,8,9这9个数。首先将这个数转化为真正的9进制数,也就是对于大于4 的数字,进行减1。如250转化为真正的9进制数是240.然后将9进制数转化为10进制数便可以得到实际的里程数。
对于这道题,采用字符处理的方式处理数据。在C++11可以使用auto关键字已经范围for来对字符串的每个字符进行访问。for(auto c: stringA)
但是Sicily编译器不支持C++11,因此也不支持auto的使用以及范围for的使用,在这里可以使用下标的方式访问
for( string::size_type i = 0; i < stringA.size(); i++){
stringA[i]
}
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.
题目解释:汽车的里程表不能经过4这个数字,也就是说3的下一个数字是5.要求我们根据显示里程数求出实际里程数
解题思路:对于显示里程数,我们其实可以看作是一个9进制数,因为每个位的位数只有0,1,2,3,5,6,7,8,9这9个数。首先将这个数转化为真正的9进制数,也就是对于大于4 的数字,进行减1。如250转化为真正的9进制数是240.然后将9进制数转化为10进制数便可以得到实际的里程数。
对于这道题,采用字符处理的方式处理数据。在C++11可以使用auto关键字已经范围for来对字符串的每个字符进行访问。for(auto c: stringA)
但是Sicily编译器不支持C++11,因此也不支持auto的使用以及范围for的使用,在这里可以使用下标的方式访问
for( string::size_type i = 0; i < stringA.size(); i++){
stringA[i]
}
#include <iostream> #include <string> using namespace std; int main(int argc, const char * argv[]) { // insert code here... string inNum; while (cin >> inNum) { if(inNum == "0")break; string nonary; for (string::size_type i = 0; i < inNum.size(); i++) { if (inNum[i] > '4') nonary += inNum[i] -1; // 将数据处理成9进制 else nonary += inNum[i]; } long unsigned result = 0; for (string::size_type i = 0; i < nonary.size(); i ++) { // 9进制转换为10进制 result = result * 9 + (nonary[i] - '0'); } cout << inNum << ": " << result << endl; } return 0; }
相关文章推荐
- 函数调用约定
- 【Java】List接口的应用,与C++中vector应用比较 (一)
- Java web项目使用webSocket
- PAT (Basic Level) Practise (中文)1038. 统计同成绩学生(20)
- 最大值——传纸条
- 关于C++ double浮点数精度丢失的分析
- 信息安全技术--轮转机密码
- 微信公众号第三方开发之五配置自定义菜单
- 【数据挖掘】基础
- Leetcode - 88. Merge Sorted Array
- 分页实例
- 省赛之一冲刺赛
- oracle 控制文件多路复用
- 2015唯品会运维实习生笔试题目(包含个人解答)
- 教养的迷思和人群形成
- SpringMVC使用@ResponseBody返回是报406错误
- python面向对象编程III(定制类)
- 第10章 基本内存管理
- 信息安全技术--一次一密钥密码
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3