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
| {
public int nextBigNumber(int n)
{
int answer = 0;
char temp;
String a = Integer.toBinaryString(n);
char[] str = a.toCharArray();
int idx = a.lastIndexOf("01");
str[idx] = '1';
str[idx+1] = '0';
for(int i=idx+2;i<str.length;i++) {
if(str[i]=='1') {
for(int j=str.length-1;j>i;j--) {
if(str[j]=='0') {
str[j]='1';
str[i]='0';
break;
}
}
}
}
String b = new String(str);
answer = Integer.parseInt(b, 2);
return answer;
}
public static void main(String[] args)
{
TryHelloWorld test = new TryHelloWorld();
int n = 78;
System.out.println(test.nextBigNumber(n));
}
}
|