ŠŸŠ¾Š¼Š¾Š³ŠøтŠµ с Š·Š°Š“Š°Ń‡ŠµŠ¹ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
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