我需要按照以下规则计算公交车票价:
我的代码在距离小于1公里时无法正常工作。有人可以建议吗?
这是我正在使用的代码:
function f=fare(dist1,age)
dist=round(dist1);
if age>=18 && age<=60
if dist<=10
f=(2+round(dist-1)*0.25)
else
f=(2+round(dist-1)*0.1)
end
else
if dist<=10
f=0.8*(2+round(dist-1)*0.25)
else
f=0.8*(2+round(dist-1)*0.1)
end
end
答案 0 :(得分:2)
你想使用千米作为整数,作为一个门槛来增加票价。
但是,您使用round
将距离四舍五入。
这意味着距离0.2
变为0
,但距离0.6
变为1
。在第一种情况下,round(0-1)->-1
所以你有负票价。
总的来说,你的程序逻辑很乱,很难遵循,因此出现了非常具体的错误。例如,在您的代码中,如果有人高于10公里,则每公里收费0.1
。旅行11公里比10公里便宜。试试吧。
这是编码同一事物的另一种更整洁的方式:
function f=fare(dist,age)
% first we are going to calculate the distances.
% there are 2 important distances: from 1 to 10 (9km)
% and from 10 to inf. We call these dist1 and dist 2 respectively
dist1=0;
dist2=0;
if dist>10
dist1=10-1;
dist2=floor(dist-10); % only charge if they have gone the whole km. 1.5km gets rounded to 1km.
elseif dist>1
dist1=floor(dist-1);
end
% Compute the whole fare
f=2+dist1*0.25+dist2*0.1;
% apply discount
if age<18 || age>60
f=f*0.8;
end
end
答案 1 :(得分:1)
我想我会演示如何对这个问题进行矢量化,以防你需要在不同的输入上多次运行该函数。下面的解决方案接受任意大小的dist
和age
数组,只要它对两个数组都相同。
function out = q41742418(dist,age)
%% Check inputs for compatible size
if any(size(dist) ~= size(age))
out = 0;
return;
end
%% Constants and definitions:
L_1 = 1; % Length of the 1st price section.
L_2 = 9; % Length of the 2nd price section.
P_0 = 2; % Price of the 1st section ( 0 < x <= L_1 )
P_1 = 0.25; % Price of the 2st section ( L_2 < x <= L_1+L_2)
P_2 = 0.1; % Price of the 3rd section ( L_1+L_2 < x < +Inf )
D_AGE_LOW = 18; % Minimal age for no discount.
D_AGE_HIGH = 60; % Maximal age for no discount.
DISCNT_PCT = 0.2; % Discount percentage
% Define a rectangular function for convenience:
rect = @(x,L,R)+(x > L & x <= R);
%% All the rest:
out = (P_0 ... % The 1st L_1 km
+ P_1*( dist > L_1) .* min(floor(dist-L_1),L_2) ... % The next L_2 km
+ P_2*( dist > L_1+L_2) .* floor(dist-(L_1+L_2)) ) ... % Rest of the distance
.* (1 - DISCNT_PCT*(~rect(age,D_AGE_LOW,D_AGE_HIGH))) ... % Apply a discount?
.* (age > 0) ... % Handling the edge case of unborns
.* rect(dist,0,Inf); % Handling the edge case of 0 distance.
查看代码中的注释。这是使用以下对Ander's implementation进行测试的:
for dists = 0.1:0.3:15
for ages = 0.1:10:70
assert(fare(dists,ages) == q41742418(dists,ages),...
['Discrepancy for (distance,age) = (' num2str(dists) ',' num2str(ages) ')']);
end
end
我冒昧地为年龄正好0
,18
和60
,或距离为0
时提供了自己的逻辑。
答案 2 :(得分:-3)
如果我们保留您当前的实现,则在距离小于1 km的情况下,您缺少“if”循环。其次,如果所覆盖的距离大于10公里,则1公里至10公里之间的距离将花费0.25美元而不是0.1美元。 以下应该正常工作:
function f = fare(dist1,age)
dist=round(dist1);
if age>=18 && age<=60
if dist <= 1
f = 2;
elseif dist<=10
f = (2 + round(dist-1)*0.25);
elseif dist > 10
f = (2 + 9*0.25 + round(dist-10)*0.1);
end
else
if dist <= 1
f = 1.6;
elseif dist <= 10
f = 0.8*(2 + round(dist-1)*0.25);
elseif dist > 10
f = 0.8*(2 + 9*0.25 + round(dist-10)*0.1);
end
end
end