{ }codelper
declaring variablesP0251

Marbles passed in turn

Three friends, Jiwoo, Minseo and Hajun, have a, b and c marbles respectively. They pass marbles once each, in this order. 1.
Jiwoo gives half of the marbles he has now to Minseo. 2.
Minseo gives half of the marbles she has now to Hajun. 3.
Hajun gives half of the marbles he has now to Jiwoo. Half means the quotient of dividing by 2, dropping the remainder (half of 7 is 3, half of 1 is 0). The marbles someone has "now" include the ones received in the earlier steps. One line gives a, b and c separated by single spaces. After the three passes, print the numbers of marbles Jiwoo, Minseo and Hajun have, in that order, on one line separated by single spaces.

Input
0 ≤ a, b, c ≤ 1,000,000 (integers)
Output
One line with the marble counts of Jiwoo, Minseo and Hajun (three integers)
Example 1 · stdin
7 2 5
stdout
7 3 4
Example 2 · stdin
10 0 0
stdout
6 3 1
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

The key is to store the amount to pass in a new variable first.
If you compute it twice, as in a = a - a / 2; b = b + a / 2;, the a on the second line has already shrunk: you take 3 out of 7 but give Minseo only 2, half of 4.
So make one variable such as give = a / 2 and use the same value for both the subtraction and the addition.
Each language declares variables differently.
In Python and Craykon a variable comes into being the moment you first assign it (Craykon give = Mathf.Quot(a, 2);).
JS and TS declare it with const give = …, and a variable you will assign again with let.
TS can also add a type, as in const give: number.
Java, C and C++ write the type in front, as in int give = a / 2;; C# var give and C++ auto give let the type come from the value on the right.
The second trap is the division that finds half.
Integer / in C, C++, Java and C# keeps only the quotient, but Python needs //, JS/TS Math.floor(a / 2) and Craykon Mathf.Quot(a, 2).
In JS, TS and Craykon / makes 7 / 2 into 3.5.
Third, do not compute all three halves from the starting values: Minseo and Hajun give half of what they have after receiving.
Even all together the values are at most 3 million, so int is enough.

Pythonmain.py · 16 lines
a, b, c = map(int, input().split()) # 건넬 몫을 먼저 새 변수에 담아 두고, 주는 쪽과 받는 쪽에 같은 값을 쓴다give = a // 2a -= giveb += give give = b // 2   # 민서는 방금 받은 구슬까지 센 수의 절반을 준다b -= givec += give give = c // 2c -= givea += give print(a, b, c)
JavaScriptmain.js · 16 lines
let [a, b, c] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number); // 건넬 몫은 const로 한 번 만들고 두 번 쓴다(JS의 /는 소수까지 나누니 Math.floor)const give1 = Math.floor(a / 2);a -= give1;b += give1; const give2 = Math.floor(b / 2);b -= give2;c += give2; const give3 = Math.floor(c / 2);c -= give3;a += give3; console.log(`${a} ${b} ${c}`);
TypeScriptmain.ts · 16 lines
import { readFileSync } from "node:fs";let [a, b, c]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number); const give1: number = Math.floor(a / 2);a -= give1;b += give1; const give2: number = Math.floor(b / 2);b -= give2;c += give2; const give3: number = Math.floor(c / 2);c -= give3;a += give3; console.log(`${a} ${b} ${c}`);
JavaMain.java · 25 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int a = sc.nextInt();        int b = sc.nextInt();        int c = sc.nextInt();         // int끼리 /는 몫만 남는다        int give = a / 2;        a -= give;        b += give;         give = b / 2;        b -= give;        c += give;         give = c / 2;        c -= give;        a += give;         System.out.println(a + " " + b + " " + c);    }}
C#Main.cs · 26 lines
using System; class Program{    static void Main()    {        string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int a = int.Parse(p[0]);        int b = int.Parse(p[1]);        int c = int.Parse(p[2]);         var give = a / 2;   // var는 오른쪽을 보고 int로 정한다        a -= give;        b += give;         give = b / 2;        b -= give;        c += give;         give = c / 2;        c -= give;        a += give;         Console.WriteLine($"{a} {b} {c}");    }}
C++main.cpp · 20 lines
#include <iostream> int main() {    int a, b, c;    std::cin >> a >> b >> c;     auto give = a / 2;   // auto는 오른쪽을 보고 int로 정한다    a -= give;    b += give;     give = b / 2;    b -= give;    c += give;     give = c / 2;    c -= give;    a += give;     std::cout << a << " " << b << " " << c << "\n";}
Cmain.c · 21 lines
#include <stdio.h> int main(void) {    int a, b, c;    scanf("%d %d %d", &a, &b, &c);     int give = a / 2;   /* 건넬 몫을 먼저 담아 둔다 */    a -= give;    b += give;     give = b / 2;    b -= give;    c += give;     give = c / 2;    c -= give;    a += give;     printf("%d %d %d\n", a, b, c);    return 0;}
Craykonmain.cray · 19 lines
p = Console.ReadLine().Trim().Split(" ");a = p[0].ToNumber();b = p[1].ToNumber();c = p[2].ToNumber(); // 크레이콘은 처음 넣는 순간 변수가 생긴다. /는 소수까지 나누니 수학.몫준다 = Mathf.Quot(a, 2);a = a - 준다;b = b + 준다; 준다 = Mathf.Quot(b, 2);b = b - 준다;c = c + 준다; 준다 = Mathf.Quot(c, 2);c = c - 준다;a = a + 준다; Console.WriteLine(a + " " + b + " " + c);