错误的里程表

题目:Faulty Odometer

问题描述

你将得到一个汽车里程表,它以整数形式显示行驶的英里数。然而,里程表有一个缺陷:它从数字3转到数字5,总是跳过数字4。这个缺陷出现在所有的位置(1,10,100,等等)。例如,如果里程表显示为15339,而汽车行驶了1英里,里程表的读数将更改为15350(而不是15340)。

Problem 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

Problem Source
Rocky Mountain 2005

分析:这是一个简单的进制转化问题,因为该题目这么叙述:遇到4则直接从3进位到5,所以这是一道典型的进制转化问题,遍历该数字的每一位(如果这一位的数字比4大,就要减去1,因为这个位跳过了一个4),然后再遍历该数字的每一位,将其转化为10进制。
所以这是一道典型的进制转化问题。
附上代码#include
#include
using namespace std;
int main()
{
string n;
for(int i=0;;i++)
{
cin>>n;
if(n[0]==‘0’)
{
break;
}
int j=n.size();
long long num=0,ans=1;
for(int k=j-1;k>=0;k–)
{
if((n[k]-‘0’)>4)
num+=((n[k]-‘0’)-1)*ans;
else
{
num+=(n[k]-‘0’)ans;
}
ans=ans
9;
}
cout<<n<<": "<<num<<endl;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码定义了一个名为 Car 的类,用于表示汽车。该类具有以下属性和方法: 属性: - make:汽车的制造商 - model:汽车的型号 - year:汽车的出厂年份 - odometer_reading:汽车的里程数 方法: - get_descriptive_name():返回整洁的描述性名称,由年份、制造商和型号组成。 - read_odometer():打印一条消息,指出汽车的里程数。 - update_odometer(mileage):将里程表读数设置为指定的值,并拒绝将里程表往回拨。 - increment_odometer(miles):将里程表读数增加指定的量。 但是,在这段代码中,有一个缩进错误。第 5 行的 class 关键字后面应该是一个空格和一个冒号,表示类的开始。但是这里多了一个缩进,导致后面的代码块被认为是类的一部分,从而导致后面的方法定义也出现了缩进错误。 请将第 5 行的缩进去掉,使其与上一行对齐,并在 class 关键字后面添加空格和冒号,即: ```python """一个可用于表示汽车的类""" class Car(): """一次模拟汽车的简单尝试""" def __init__(self, make, model, year): """初始化描述汽车的属性""" self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_descriptive_name(self): """返回整洁的描述性名称""" long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): """打印一条消息,指出汽车的里程""" print("This car has " + str(self.odometer_reading) + " miles on it.") def update_odometer(self, mileage): """ 将里程表读数设置为指定的值 拒绝将里程表往回拨 """ if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("You can't roll back an odometer!") def increment_odometer(self, miles): """将里程表读数增加指定的量""" self.odometer_reading += miles ``` 这样就可以解决这个问题了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值