-
Notifications
You must be signed in to change notification settings - Fork 2
/
House_Number.java
42 lines (36 loc) · 1005 Bytes
/
House_Number.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
package com.company;
import java.util.Scanner;
public class House_Number {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t != 0) {
int n = s.nextInt();
System.out.println(houseNumber(n));
t--;
}
}
private static long houseNumber(long n) {
long[] arr = new long[13];
for (int i = 1; i < 13; i++) {
arr[i] = (long) Math.pow(10, i) - 1;
}
for (int i = 2; i < 13; i++) {
for (int j = 1; j <= i - 1; j++) {
arr[i] = arr[i] - arr[j];
}
}
long count = 0;
for (int i = 1; i < 13; i++) {
if (n - arr[i] >= 0) {
count += arr[i] * i;
n = n - arr[i];
} else {
count += n * i;
break;
}
}
return count;
}
}
//refernce https://youtu.be/5Q2-PTtWZK8