0
Please the Transportation Question in c++
Can someone teach me how to solve it
19 Réponses
+ 1
First you have to get the number of passengers
int passengers; cin >> passengers;
then you have to find how many passengers are left
int remainPass = passengers % 50;
then you have to find how many seats are left
int remainSeats = 50 - remainPass;
0
Sample Input:
126
Sample Output:
24
Explanation: The first bus will transport 50 passengers, leaving 126-50=76 in the station. The next one will leave 26 in the station, thus, the last bus will take all of the 26 passengers, having 50-26=24 seats left empty.
Let's decide step by step.
Step 1:
Create a variable and assign the input data to it, (the number of passengers at the stop)
0
The 26 and 14 you use are undefined variables and won’t be the same in every case. The amount of passengers on the last possible bus is dependent on the total start amount. That is (total start) % 50, which the example gives 126 % 50 = 26 ( 50 + 50 + 26). From there you just subtract 50 from you modulus value (26) for your remaining open seats (24) since.
Here is my code:
#include <iostream>
using namespace std;
int main() {
int passengers;
cin>>passengers; /*input given for total passengers */
if(passengers > 50){
cout<< 50 - (passengers%50);
/* passengers % 50 gives you the amount of passengers on the last bus. Subtract that from 50 to get the empty spaces on that bus*/
}else{
cout<<50-passengers;
/* if below 50 then theres only one bus trip so you can just subtract the passengers from the total amount of seats to get the empty seats.*/
}
return 0;
}
0
Vasiliy he seemed to be lacking basic understanding of modulus operation and if-else logic and he’d been stuck awhile so I thought maybe visual learning was the best
- 1
How long have you been trying to solve this problem and what results have you come to?
- 1
For weeks now
- 1
I wish I can screenshot the QQuestion for you
- 1
So theres two cases, more than 50 passengers and 50 passengers or less. 50 passengers or less, like the last line of the example: 50 - passengers = seats left empty. Over 50 passengers would be where you can use modulus operater %. So it would be passengers%50 giving you the remaining passengers (not seats) and you can use the math from the first case to solve for the remaining seats.
- 1
This can be done with an if-else statement, please show your attempt.
- 1
Thanks Sir I feel I have a little knowledge of C++
- 1
Roderick Davis , you gave a very good explanation for solving the problem, but why did you write a ready-made solution, why do you deprive Ricky of doing it himself 🙁
- 1
If he left me I will be very confused
- 1
It's ok I understand but i don't know how to use them and when I should use them.
- 2
Can you link the question?
- 2
You are making a program for a bus service.
A bus can transport 50 passengers at once.
Given the number of passengers waiting in the bus station as input, you need to calculate and output how many empty seats the last bus will have.
Sample Input:
126
Sample Output:
24
Explanation: The first bus will transport 50 passengers, leaving 126-50=76 in the station. The next one will leave 26 in the station, thus, the last bus will take all of the 26 passengers, having 50-26=24 seats left empty.
- 2
Please this the Question
- 2
Int remainder ;
remainder =(50%26)+14;
cout << remainder;
- 2
I'm not getting it clearly