Faulty Odometer
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9345 | Accepted: 5786 |
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
题意:你开着一辆里程表都跳不对的破车出去玩,这辆破车的里程表有个特点,就是不会经过数字4,比如现在里程是15339,那么再开一公里里程表就会跳到15350,现在你想知道你究竟开了多远,所以得写一个小程序来帮你计算。
分析:脑洞如果开大了也许会飙到字符串处理上啥的然后惨烈翻车,其实很简单,正常的里程表应该每行驶10公里跳一次,结果这破车行驶9公里就会跳一次,所以问题就变成了10-9进制转换问题,但是还有一个问题,这个并不是严格意义上的进制转换,假如个位数小于4,那里程就是实打实的,如果大于4,才会有一次虚高,所以我们要对源数据进行处理,如果当前位数字大于3(4...不存在的),进行-1操作,否则不管,之后再进行进制转换。
#include<iostream>
#include<cstring>
#define MAX 20
using namespace std;
int a[MAX]={0};
int cnt;
int cc(int n)
{
cnt=0;
while(n)
{
a[cnt++]=n%10; n/=10;
}
for(int i=0;i<cnt;i++)
{
if (a[i]>3) a[i]-=1;
}
}
int main()
{
int n;
while(cin >> n && n){
cc(n);
int ans=0;
for(int i=cnt-1;i>=0;i--)
{
ans*=9;
ans+=a[i];
}
cout << n<<": "<<ans<<endl;
}
return 0;
}