- 4

What's the logic and how to solve it?

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.

14th Jun 2022, 9:57 AM
Asim Saeed
Asim Saeed - avatar
17 Answers
+ 2
What do you mean by logic? And where is your attempt so you can be assisted?
14th Jun 2022, 10:17 AM
Justice
Justice - avatar
+ 2
A͢J Justice Thanks a lot for helping me out. The problem is solved now. https://code.sololearn.com/clm8bAC21L7P/?ref=app
14th Jun 2022, 3:42 PM
Asim Saeed
Asim Saeed - avatar
+ 1
Logic is - use modulus operator (%) to get left seats
14th Jun 2022, 10:25 AM
A͢J
A͢J - avatar
+ 1
asim saeed I believe you have boilerplate (given code) for the inputs already. Can you post your code along with your attempt?
14th Jun 2022, 11:56 AM
Justice
Justice - avatar
+ 1
asim saeed That's fine. Show us. It might be a simple bug but we don't know because we can't see.
14th Jun 2022, 11:59 AM
Justice
Justice - avatar
+ 1
Justice #include <iostream> using namespace std; int main() { int passengers, seats; cin>>passengers; seats = 50 - passengers; cout<<seats; return 0; }
14th Jun 2022, 12:03 PM
Asim Saeed
Asim Saeed - avatar
+ 1
Are you sure you don't want to use some kind of loop? Remember, the passengers outweigh the number of seats per bus, so you need to decrease multiple times depends on how many passengers there are.
14th Jun 2022, 12:07 PM
Justice
Justice - avatar
+ 1
Justice i am stuck because of this reason. I can't solve it without using loops.
14th Jun 2022, 12:08 PM
Asim Saeed
Asim Saeed - avatar
+ 1
Try breaking the problem down into pseudocode first then instead of jumping right into code. What are something that you are sure you need to do, step-by-step, based on the task in plain English? Also, remember that seats don't really decrease with passengers. Seats is a variable that should not be changed but rather change something like the number of passengers accommodated.
14th Jun 2022, 12:14 PM
Justice
Justice - avatar
+ 1
Asim Saeed Dont think about different inputs, just think about common (generic) logic which may accept different inputs and give result according to that.
14th Jun 2022, 3:09 PM
A͢J
A͢J - avatar
+ 1
Asim Saeed Nice You can solve in a short way: cout << 50 - passengers % 50;
14th Jun 2022, 4:34 PM
A͢J
A͢J - avatar
0
asim saeed Review % modula
14th Jun 2022, 10:25 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
A͢J Thank you so much could you please elaborate which code we should use for different inputs.
14th Jun 2022, 11:44 AM
Asim Saeed
Asim Saeed - avatar
0
Rik Wittkopp could you please explain it more if we be taking different inputs.
14th Jun 2022, 11:46 AM
Asim Saeed
Asim Saeed - avatar
0
Justice i attempted to solve the problem 3 or 4 times. The code was good for 2 or 3 cases out of 10, but the code didn't work for cases with different inputs
14th Jun 2022, 11:59 AM
Asim Saeed
Asim Saeed - avatar
14th Jun 2022, 12:06 PM
Asim Saeed
Asim Saeed - avatar
0
Justice how can i make it applicable for inputs more than 50?
14th Jun 2022, 12:07 PM
Asim Saeed
Asim Saeed - avatar