-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11933.java
71 lines (42 loc) · 1.08 KB
/
11933.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* UVa 11933: Splitting Numbers
*
* Problem Statement: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3084
*/
import java.util.Scanner;
public class Main {
static String split(String bin, boolean index) {
StringBuilder sb = new StringBuilder(bin);
for (int i = bin.length() - 1; i >= 0; i--) {
if (bin.charAt(i) == '1') {
if (index)
sb.replace(i, i + 1, "0");
index = !index;
}
}
return sb.toString();
}
static int splitNum(int n, boolean index) {
String binStr = Integer.toBinaryString(n);
String splitStr = split(binStr, index);
return Integer.parseInt(splitStr, 2);
}
static int a(int n) {
return splitNum(n, false);
}
static int b(int n) {
return splitNum(n, true);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int a, b;
while(n != 0) {
a = a(n);
b = b(n);
System.out.println(a + " " + b);
n = scan.nextInt();
}
scan.close();
}
}