-
Notifications
You must be signed in to change notification settings - Fork 0
/
NJinZhi.java
46 lines (43 loc) · 1.23 KB
/
NJinZhi.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
package test;
import java.util.Scanner;
public class NJinZhi {
private static final int EFFECTIVE_NUMBER = 10;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextDouble()) {
double m = scanner.nextDouble();
int n = scanner.nextInt();
if (isLegal(m, n)) {
getResult(m, n);
}
}
}
private static boolean isLegal(double m, int n) {
return n > 1 && n < 10 && !(m <= 0.0000009) && !(m >= 1.0);
}
private static void getResult(double m, int n) {
// ¼ÆËã½á¹û
int[] result = doCalculate(m, n);
// ´òÓ¡½á¹û
printResult(result);
}
private static int[] doCalculate(double m, int n) {
int[] result = new int[10];
int index;
for (index = 0; index < EFFECTIVE_NUMBER; index++) {
m *= n;
result[index] = (int)m;
if (m >= 1.0) {
m -= result[index];
}
}
return result;
}
private static void printResult(int[] result) {
System.out.print("0.");
for (int index = 0; index < EFFECTIVE_NUMBER; index++) {
System.out.printf("%d", result[index]);
}
System.out.println();
}
}