>>585 Java
いろいろ適当なのは気にしない
public class TheLuckyNumbers {
public static int count(int a, int b) {
if (a < 1 || a > 1000000000 || b < 1 || b > 1000000000 || a > b) return 0;
return count(b) - count(a - 1);
}
private static int count(int a) {
int result = 0, digits = 0, add = 0;
if ((digits = search(a)) != 0) { result = a % digits; a -= result; result += 1; }
digits = 1;
while (a != 0) {
for (int i = 0, j = a % 10; i < j; i++) {
result += (i == 4 || i == 7) ? digits : add;
}
a /= 10; add = add * 8 + digits * 2; digits *= 10;
}
return result;
}
private static int search(int a) {
int result = 0;
for (int digits = 1; a != 0; digits *= 10, a /= 10) {
if (a % 10 == 4 || a % 10 == 7) { result = digits; }
}
return result;
}
}