+ 3
Transportation project help
There is a project for a transportation of buses to calculate how many seats are left here in SoloLearn using C++, I canât seem to understand how would this be solved.
13 Respostas
+ 4
A simple three lines of code implementation.
First, you use the modulus function to find the remainder, which are the passengers on the last bus.
Then, to find the empty seats, you use bus capacity minus away the last passengers.
https://code.sololearn.com/cAsIKRZH3quK/?ref=app
+ 2
Have you come across the modulo "%" operator?
You'd need to use it for this problem.
+ 2
examples are provided for clarification
sololearn checks your code with 5 or 6 different inputs (with different data to test various possibilities)
essentially your code will run 5 or 6 times
if your code is good enough and the output matches their expected input you win the challange.
here is code with a few comments to explain what each line does.
int waiting = 0; // define variable
int seats =0;
cin >> waiting; //get total waiting as an input value
seats = waiting % 50; //mod operator will return seats filled in last bus
cout << 50 - seats ; //total seats(50) - occupied seats is the expected answer
+ 1
busCapacity - NoOfpeople % busCapacity.
+ 1
There are multiple ways of doing this, the simplest way I can think of is to have 3 integers, 1 for last bus passengers, 1 for user input, and the last one for the seats remaining.
so what you would do is something along the lines of :
int n; // this will be one for user input
int lastBus; // the passengers on the last bus, this is used to calculate the seats remaining
int seatsRemaining; // this will be your answer
cin >> n; // this will take the user input
lastBus = n%50; // this uses modulus operator to figure out the amount of passengers on the last bus
seatsRemaining = 50-lastBus; // this takes bus capacity which is 50 - the passengers on the last bus to calculate open seats
cout << seatsRemaining; // this gives your output that sololearn will check
There are other ways of doing this but that will use things you will only learn later on, the above code only uses what you should have learned so far. Post might be late but I hope it will help others that come on a later date.
0
yeah but i cant make sense of it
0
this is my code but its asking for both outputs of 38 and 19 seperately
#include <iostream>
using namespace std;
int main() {
int x;
int bus;
int res;
bus = 50;
//res = bus - 12;
//cout << res << endl;
x = 50 * 4;
x = 231 - x;
x = bus % x;
cout << x << endl;
return 0;
}
0
oh wow i did not think of that
0
Im so lost here.
- 1
int totalNumPass = 0;
int maxBusSeats = 50;
int emptySeats = 0;
cin>> totalNumPass;
if ( totalNumPass > maxBusSeats ) {
while (totalNumPass > maxBusSeats) {
totalNumPass -= maxBusSeats;
}
emptySeats = maxBusSeats % totalNumPass;
cout << emptySeats;
}
else if ( totalNumPass < maxBusSeats ) {
emptySeats = maxBusSeats - totalNumPass;
cout << emptySeats;
}
else if( totalNumPass = maxBusSeats) {
cout << 0;
}
else if ( totalNumPass < 0) {
cout << "Error";
}