+ 4
Transportation C++
The transportation question makes 0 sense to me. 126 passengers, 50 people per bus. whats the remainder on the last bus (im assuming most people have seen this problem) answer is 24. however the code written to get 24 is incorrect somehow. int total = 126; int a = total % 50; int b = 50 - a; cout << b; the test case say that the outcome is actually 38 for some reason and the 2nd test case says the outcome is 19 and none of those numbers factor into this at all! the code that worked was int input; cin >> input; cout << 50 - input % 50; ^^^^^^^^^^^^^ This makes even less sense unless we're supposed to input 126 i think. I just want to understand, what am i not understanding about this whole thing?
13 Answers
+ 6
Warlock Kadash
Your code output is dependent on the challenge's input.
see and try this:
https://code.sololearn.com/cN9U512B13kt/?ref=app
+ 5
In challenges, every input is different in every test cases. To get inputs:
int x;
cin >> x;
Then it is up to you how you will make this work by getting the input and to get the expected output. So whatever the inputs may be, it will always satisfy the expected output. If you have more questions, feel free to ask. Happy Coding!
And example of getting input using cin (console in)
https://code.sololearn.com/ck1WsKCSag50/?ref=app
And I also explained here in this past thread why you should get inputs in challenges.
https://www.sololearn.com/Discuss/2647499/?ref=app
+ 3
The first test case provides 12 as the input, not 126. So the expected output of 38 (50 - 12%50) is correct.
+ 3
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
+ 3
Brian Thanks a lot I tried using modulo but I got confused I knew I could do it without loop cos d challenge is b4 loops. Thanks again
+ 2
《 Nicko12 》 OKAY! I get it now! the "cin" is used for getting the user input and that's what the question is asking for! I thought the question wanted you to show the work for reaching the answer of 24 empty seats! I now understand it wants you to make a program for finding the answer to not only the the sample but all of the test cases! That's what wasn't making sense to me! All the test cases can be solved be solved with 1 program and I am supposed to make that one program! Thank you for all your help!
+ 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
Okay I wrote the code in a separate IED so I could look at it better. I understand where 38 comes from now but test case 2 wants the output to be 19 and I don't understand where that factors in and how it relates to 24 empty seats.
+ 1
Finally solved it
#include <iostream>
using namespace std;
int main() {
//your code goes here
int a;
int b = 50;
int seatLeft;
cin>>a;
if(a<50){
seatLeft =50 - a;
cout<< seatLeft ;
}
else{
while(a>50){
a = a - 50;
}
seatLeft = 50 - a;
cout<< seatLeft ;
}
return 0;
}
+ 1
Fikayo Jetawo good job! It is a tough one for many people. Many struggle because they miss the tip given in the highlighted box about using the modulo (%) operator. It looks like you missed it, too, because I do not see it in your code.
The modulo operator can replace the while loop that repeatedly subtracts 50 until it finds a remainder. You can get the exact same result with a single operator!
All this code:
if(a<50){
seatLeft = 50 - a;
cout<< seatLeft;
}
else{
while(a>50){
a = a - 50;
}
seatLeft = 50 - a;
cout<< seatLeft;
}
Simplifies to two lines (or just one if you skip using the seatLeft variable and put the expression in the cout statement):
seatLeft = 50 - a%50;
cout << seatLeft;
That's the power of optimizing with modulo!
+ 1
It's funny that there was a single line solution 😆😄
#include <iostream>
using namespace std;
int main() {
//your code goes here
int a;
int b = 50;
cin>>a;
int seatLeft = 50 - a%50;
cout<< seatLeft ;
return 0;
}
+ 1
Darkhost the question asked
'Given the number of passengers waiting in the bus station as input'
0
The question is asked unusually.
The question is supposed to help the user understand that the bus station wants to input their own value to determine what the remainder of seats are. So they need cin to input their own custom value.