Reputation: 25
So I'm a beginner at programming and I thought this short exercise (see attached image) was quite easy, but when I submit the code it gives back results for the test cases. I failed one of the test cases and I can't figure out what would be wrong (Sadly you can't see the input from that test case).
This is the code I had:
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(); // first distance
int m = sc.nextInt(); // fare for distance a
int n = sc.nextInt(); // fare for remaining distance
int d = sc.nextInt(); // total distance
int fare = (a*m)+((d-a)*n);
System.out.println(fare);
Can anyone see in what situation this calculation would be incorrect? It looks so easy but somehow I can't think of the solution right now.
In case this is relevant the sample input is: 1 2 3 5 Giving the output: 14
Upvotes: 1
Views: 340
Reputation: 1502
a
is not part of the distance traveled. It's just a threshold where the calculation switches from one price to the other. Let's put it this way: if d
is less than a
, will the driver start paying the customer? No. ;)
You should calculate the price for the first part of the route and then conditionally, if d
is greater than a
, add to that the price for the remaining part of the route.
Upvotes: 1