-
Notifications
You must be signed in to change notification settings - Fork 0
/
luckyNumber.java
86 lines (77 loc) · 1.83 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.math.BigInteger;
import java.util.Scanner;
public class luckyNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try {
int L = in.nextInt();
int H = in.nextInt();
System.out.println(luckyNumber(L, H));
}catch(Exception e){
//Not sure for BigInteger part
int L = in.nextInt();
int H = in.nextInt();
System.out.println(luckyNumber(L, H));
}
}
public static int luckyNumber(int L, int H) {
int ans = 0;
for(int i=L; i<=H; i++) {
boolean eightNums = false;
boolean sixNums = false;
int x = i;
while(x != 0) {
if(x % 10 == 8) {
if(eightNums == false && sixNums == false) {
ans++;
eightNums = true;
}else if(sixNums == true) {
ans--;
break;
}
}
if(x % 10 == 6) {
if(eightNums == false && sixNums == false) {
ans++;
sixNums = true;
}else if(eightNums == true) {
ans --;
break;
}
}
x /= 10;
}
}
return ans;
}
public static int luckyNumber(BigInteger L, BigInteger H) {
int ans = 0;
for(BigInteger i=L; i.compareTo(H)<=0; i=i.add(BigInteger.ONE)) {
boolean eightNums = false;
boolean sixNums = false;
BigInteger x = i;
while(x.compareTo(BigInteger.ZERO) != 0) {
if(x.mod(BigInteger.TEN) == BigInteger.valueOf(8)) {
if(eightNums == false && sixNums == false) {
ans++;
eightNums = true;
}else if(sixNums == true) {
ans--;
break;
}
}
if(x.mod(BigInteger.TEN) == BigInteger.valueOf(6)) {
if(eightNums == false && sixNums == false) {
ans++;
sixNums = true;
}else if(eightNums == true) {
ans --;
break;
}
}
x.divide(BigInteger.TEN);
}
}
return ans;
}
}