-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumMoves.java
More file actions
71 lines (51 loc) · 1.59 KB
/
Copy pathMinimumMoves.java
File metadata and controls
71 lines (51 loc) · 1.59 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
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
package quiz;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
/**
* @Author : yion
* @Date : 2017. 6. 16.
* @Description :
*/
public class MinimumMoves {
public static void main(String[] args) throws IOException {
int[] a = {1234, 4321};
int[] b = {2345, 3214};
int count = minimumMoves(a, b);
System.out.println("count : " + count);
}
static int minimumMoves(int[] a, int[] m) {
int size = a.length;
int result = 0;
for (int i = 0; i < size; i++) {
String first = a[i]+"";
String second = m[i]+"";
System.out.println("first : " + first); // 1234
System.out.println("second : " + second); // 2345
char[] ins = first.toCharArray();
char[] rst = second.toCharArray();
int value = counting(ins, rst);
result += value;
}
return result;
}
static int counting(char[] ins, char[] rst) {
int count = 0;
for (int idx = 0; idx < ins.length; idx++) {
if (ins[idx] > rst[idx]) {
count += calc(ins[idx], rst[idx]);
} else if (ins[idx] < rst[idx]) {
count += calc(rst[idx], ins[idx]);
}
}
System.out.println("result : " + count);
return count;
}
private static int calc(char c, char i) { // 2, 1
int x = Character.getNumericValue(c);
int y = Character.getNumericValue(i);
return x - y;
}
}