-
Notifications
You must be signed in to change notification settings - Fork 3
/
beatspread.java
38 lines (35 loc) · 1.31 KB
/
beatspread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.*;
import java.util.*;
public class beatspread {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(in.readLine());
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(in.readLine());
int sum = Integer.parseInt(st.nextToken());
int diff = Integer.parseInt(st.nextToken());
if(diff > sum) {
out.println("impossible");
continue;
}
ArrayList<Integer> solved = solve(sum, diff);
if(solved.size() != 0) out.println(solved.get(1) + " " + solved.get(0));
else out.println("impossible");
}
out.close();
}
public static ArrayList<Integer> solve(int s, int d) {
ArrayList<Integer> nums = new ArrayList<Integer>();
for(int i = 0; i <= s; i++) {
for(int j = 0; j <= i; j++) {
if(i + j == s && Math.abs(i - j) == d) {
nums.add(i);
nums.add(j);
}
}
}
Collections.sort(nums);
return nums;
}
}