-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSretan.java
More file actions
68 lines (65 loc) · 1.47 KB
/
Sretan.java
File metadata and controls
68 lines (65 loc) · 1.47 KB
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
import java.util.Scanner;
public class Sretan {
/**
* @author Juan Pablo Camargo Lasso (juancholasso)
*
* Lucky Number - CodeChef
* Problem Code FP06
*
* All submissions for this problem are available.
* Digits 4 and 7 are lucky, while all others are unlucky. An integer is lucky if
* it contains only lucky digits
* in decimal notation. We would like to know the K-th lucky positive integer.
*
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = 0;
int ceros = 0;
int limite = 0;
String res = "";
String temp = "";
while(sc.hasNextInt()){
k = sc.nextInt();
if(k < 7){
switch(k){
case 1: System.out.println("4");
break;
case 2: System.out.println("7");
break;
case 3: System.out.println("44");
break;
case 4: System.out.println("47");
break;
case 5: System.out.println("74");
break;
case 6: System.out.println("77");
break;
}
}
else{
ceros = 3;
limite = 7;
while(k >= (limite*2)+1){
limite = limite*2+1;
ceros++;
}
res = Integer.toBinaryString(k-limite);
if(limite == k){
res = "";
for(int i = 0; i<ceros; i++)
res+="4";
System.out.println(res);
}
else{
temp = "";
for(int j = 0; j<(ceros-(int)res.length()); j++)
temp+="4";
res = res.replace('0', '4');
res = res.replace('1', '7');
System.out.println(temp+res);
}
}
}
}
}