-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLuckyNumber.java
72 lines (62 loc) · 1.19 KB
/
LuckyNumber.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
72
/**
*
*/
package jd96;
import java.util.Scanner;
/**
* @author Iver3on
* @date 2016年9月5日
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int[] t = new int[n];
for (int i = 0; i < n; i++) {
t[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
out(t[i]);
}
System.out.println();
}
}
/**
* @param i
*/
private static void out(int m) {
int sum = 0;
int num = 0;
for (int i = 1;; i++) {
sum = (int) (sum + Math.pow(2, i));
if (sum > m) {
num = i;
break;
}
}
int index = (int) (m - (sum - Math.pow(2, num)) - 1);
int[] result = new int[num];
int j = -1;
do {
if (index % 2 != 0) {
result[++j] = 1;
} else {
result[++j] = 0;
}
} while ((index /= 2) != 0);
StringBuffer sb = new StringBuffer();
for (int i = num - 1; i >= 0; i--) {
if (result[i] != 0) {
sb.append("7");
} else {
sb.append("4");
}
}
System.out.println(sb.toString().trim());
}
}