+ 1
C++ Bus service question, my code works on the code playground, but not on the question itself.
#include <iostream> using namespace std; int main() { //your code goes here int totalPeople; cin >> totalPeople; int bus = 50; int trip1 = totalPeople - bus; int trip2 = totalPeople - bus * 2; int empty = bus % trip2; cout <<"the answer is.." << endl << trip1 << " after the first trip" << endl << trip2 << " after the second trip" << endl << empty << " total seats left empty on final trip" ; return 0; }
4 Réponses
+ 5
Matt James Roper Your program works only for 2 trips, i.e. only if the input is at most 150.
If you need to subtract 50 into a loop, or to use the modulo % operator
Also what is supposed to do the line with int empty?
+ 2
int totalPeople;
cin >> totalPeople;
int bus = 50;
int emptySeats = bus - totalPeople % bus;
cout << emptySeats;
0
Davide the bus has 50 seats, and there are 126 people at the bus station, int empty was supposed to show how many empty seats there are on the third and final bus trip by using the modulo % operator
0
Matt James Roper if you take 50 from 126 two times you are left with 26 people. The empty seats are just 50-26, no need for modulo here.
You need to use the modulo before, to subtract 50 multiple times.
126 was the example but in the testd there can be any number. The modulo % operator do exactly what you need to do.
https://en.wikipedia.org/wiki/Modulo_operation?wprov=sfla1