-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRounding.java
More file actions
39 lines (34 loc) · 1.13 KB
/
Rounding.java
File metadata and controls
39 lines (34 loc) · 1.13 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
import java.io.*;
import java.util.*;
public class Rounding {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
long t = Long.parseLong(br.readLine());
while(t -- > 0) {
char[] digits = br.readLine().toCharArray();
int firstDig = digits[0] - '0';
int len = digits.length;
long ans = 0;
for(int i=1; i<len-1; i++) {
ans += stringOfOnes(i) * 5;
}
if(firstDig > 4) {
ans += 5 * stringOfOnes(len - 1);
}
else if(firstDig == 4) {
long toAdd = Math.max(0, Long.parseLong(new String(digits)) - 4 * stringOfOnes(len));
ans += toAdd;
}
pw.println(ans);
}
pw.close();
}
static long stringOfOnes(int length) {
long result = 0;
for (int i = 0; i < length; i++) {
result = result * 10 + 1;
}
return result;
}
}