Need help I understanding the code C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need help I understanding the code C++

Hi, I just completed basic module test called Transportation. I wrote a code correctly, but I can't understand why it is correct when numbers are so big. I'm new to coding >.< Here goes the code: #include <iostream> using namespace std; int main() { //your code goes here int a; int b; int c; b=50; cin >> a; cout << ((a-500000*b)%b)*(-1) << endl; return 0; }

8th May 2021, 12:09 PM
Kacper Grabiński
Kacper Grabiński - avatar
4 Answers
+ 1
1. Nie używasz nigdzie zmiennej 'c', więc możesz się jej pozbyć. 2. (a-25'000'000)%50*(-1), więc mamy na przykład: 124-25'000'000%50 = -24'999'876%50=-26*(-1) Ale jeśli się zastanowić, to ten sam wynik otrzymamy wykonując tylko dwa działania: 50-a%50=50-124%50=50-24=26 operator% zwraca resztę z dzielenia, więc skoro w jednym autobusie mieści się 50 osób, a mamy wyświetlić ilosc wolnych miejsc w ostatnim autobusie, to wystarczy sprawdzić resztę z dzielenia wszystkich osób/liczbę miejsc w jednym autobusie. Twój kod nie działał przy mniejszych liczbach, bo jeśli 'a' będzie większe od tego 500'000*50, to otrzymasz ujemny wynik (a-25'000'000 będzie większe od zera). Widocznie testowana liczba była na tyle duża, że 'a' było większe od tej "dużej" liczby
8th May 2021, 1:39 PM
Michal Doruch
+ 1
I just tried different numbers and gradually increased them until every test passed, so I figured 'bigger the number, more tests passed'. And now I'm in the pickle 'cause I don't know why did it even work ;0
8th May 2021, 1:07 PM
Kacper Grabiński
Kacper Grabiński - avatar
0
Hi! very interesting solution! how and where did you get this number? by the way, you can reduce it to 10,000. I checked it out
8th May 2021, 12:49 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Ok guys I simplified it by a lot, 50-a%50 works perfectly in all tests. Thank you both for answers! Dzięki za wyjaśnienie w akapicie z operatorem %, pomogło bardzo mocno!
8th May 2021, 3:05 PM
Kacper Grabiński
Kacper Grabiński - avatar