eolymp
bolt
Try our new interface for solving problems
Məsələlər

4 thought

4 thought

Write a program which, given an integer \textbf{n} as input, will produce a mathematical expression whose solution is \textbf{n}. The solution is restricted to using exactly four \textbf{4}'s and exactly three of the binary operations selected from the set \{\textbf{∗}, \textbf{+}, \textbf{−}, \textbf{/}\}. The number \textbf{4} is the \textbf{ONLY} number you can use. You are not allowed to concatenate fours to generate other numbers, such as \textbf{44} or \textbf{444}. For example given \textbf{n = 0}, a solution is \textbf{4 ∗ 4 − 4 ∗ 4 = 0}. Given \textbf{n = 7}, a solution is \textbf{4 + 4 − 4 / 4 = 7}. Division is considered truncating integer division, so that \textbf{1/4} is \textbf{0} (instead of \textbf{0.25}). Assume the usual precedence of operations so that \textbf{4 + 4 ∗ 4 = 20}, not \textbf{32}. Not all integer inputs have solutions using four \textbf{4}'s with the aforementioned restrictions (consider \textbf{n = 11}). \textit{\textbf{Hint}}: Using your forehead and some forethought should make an answer forthcoming. When in doubt use the fourth. \InputFile Input begins with an integer \textbf{1} ≤ \textbf{m} ≤ \textbf{1000}, indicating the number of test cases that follow. Each of the next \textbf{m} lines contain exactly one integer value for \textbf{n} in the range \textbf{−1000000} ≤ \textbf{n} ≤ \textbf{1000000}. \OutputFile For each test case print one line of output containing either an equation using four \textbf{4}'s to reach the target number or the phrase no solution. Print the equation following the format of the sample output; use spaces to separate the numbers and symbols printed. If there is more than one such equation which evaluates to the target integer, print any one of them.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
5
9
0
7
11
24
Çıxış verilənləri #1
4 + 4 + 4 / 4 = 9
4 * 4 - 4 * 4 = 0
4 + 4 - 4 / 4 = 7
no solution
4 * 4 + 4 + 4 = 24
Mənbə 2013 ACM-ICPC North American Qualification Contest