Помогите с задачей | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Помогите с задачей

В задаче просит вывести кол-во свободных мест в последнем автобусе, но откуда-то берёт цифру 38 в "ожидающем выводе". Если что, задача "Перевозка" по модулю основные понятия в С++. Помогите пожалуйста, потому что то-ли лыжи не едут, то-ли я.. программист. "Вы создаете программу для автобусной службы. Автобус может перевозить 50 пассажиров за один раз. Вам дано количество пассажиров, ожидающих на автобусной станции. Посчитайте и выведите, сколько будет свободных мест в последнем автобусе. Пример Входных Данных: 126 Пример Выходных Данных: 24 Объяснение: Первый автобус перевезет 50 пассажиров, оставив 126-50=76 на станции. Следующий автобус оставит 26 на станции, таким образом, последний автобус заберет всех 26 пассажиров, имея 50-26=24 свободных места. " Мой код: #include <iostream> using namespace std; int main() { int a = 126; int x = 50; int b; int c; int d; b = a - x; c = b - x; d = x - (b % x); cout << d << endl; return 0; }

28th Apr 2021, 1:26 AM
Halanskiy Illia
Halanskiy Illia - avatar
3 Answers
0
I see three issues to point out, plus an optional consideration: 1. The input variable (a) is being assigned a hard-coded value. Instead, the program must read it from the console. Use cin >> a. 2. The variable c is calculated and then not used. But it is not useful anyway. See item 3 below. 3. Repeated subtraction of x is not necessary. That is what modulo (%) does for you already. It is a shortcut for doing repeated subtractions until only the remainder is left. It is enough to only use d = x - (a % x) and eliminate variables b and c. 4. (Optional) Consider what happens when the last bus is completely full. The remaining open seats is zero, and d = 50 - 0 = 50. Are there are 50 open seats in the last bus? No, there are zero open seats. Consider adding one more line: d %= x. That will give correct output of zero for the special case.
28th Apr 2021, 3:53 PM
Brian
Brian - avatar
0
Thanks for the help, I solved the problem this way - #include <iostream> using namespace std; int main () { // your code int a, b, c, d; cin >> a; b = 50 - (a - = 50); c = 50 - (b - = 50); d = 50 - (c% = 50); cout << d << endl; return 0; }
7th May 2021, 5:46 PM
Halanskiy Illia
Halanskiy Illia - avatar
0
Very good. Your solution now works. Please allow me to point out that it could be simplified. The calculations for b and c are unnecessary. If I reduce your calculations mathematically, I find that c holds the same value as the original input value. Mathematically, c = 50 - (b - 50) = 50 - b + 50 c = 100 - b Now substitute b: c = 100 - (50 - (a - 50)) c = 100 - (50 - a + 50) c = 100 - (100 - a) = 100 - 100 + a c = a Therefore your code is doing the same as this shorter revision: int a, c, d; cin >> a; c = a; d = 50 - (c % 50); And it can be shortened further by eliminating c: int a, d; cin >> a; d = 50 - (a % 50); This simpler code gives the same result.
8th May 2021, 4:08 AM
Brian
Brian - avatar