- 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.
17 Antworten
+ 2
What do you mean by logic? And where is your attempt so you can be assisted?
+ 2
A͢J Justice Thanks a lot for helping me out. The problem is solved now.
https://code.sololearn.com/clm8bAC21L7P/?ref=app
+ 1
Logic is - use modulus operator (%) to get left seats
+ 1
asim saeed I believe you have boilerplate (given code) for the inputs already. Can you post your code along with your attempt?
+ 1
asim saeed That's fine. Show us. It might be a simple bug but we don't know because we can't see.
+ 1
Justice
#include <iostream>
using namespace std;
int main() {
int passengers, seats;
cin>>passengers;
seats = 50 - passengers;
cout<<seats;
return 0;
}
+ 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.
+ 1
Justice i am stuck because of this reason. I can't solve it without using loops.
+ 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.
+ 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.
+ 1
Asim Saeed
Nice
You can solve in a short way:
cout << 50 - passengers % 50;
0
asim saeed
Review % modula
0
A͢J Thank you so much could you please elaborate which code we should use for different inputs.
0
Rik Wittkopp could you please explain it more if we be taking different inputs.
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
0
Justice how can i make it applicable for inputs more than 50?