-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSample.java
80 lines (70 loc) · 1.45 KB
/
Sample.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.*;
class Sample{
public long[][] Result;
public void category(long[] Original_Data, long mean){
int Length = Data.length;
long[] Data = Original_Data;
long[] A = new long[Length];
long[] B = new long[Length];
Result = new long[Length][Length];
for(int i=0; i<Length; i++){
if(Data[i] > 0)
{
A[i] = Data[i];
B[i] = 0;
}else if(Data[i] < 0)
{
A[i] = 0;
B[i] = Data[i];
}else{
Result[i][i] = 0;
}
}
// The first run to find an exactly equal user.
for(int i=0; i<Length; i++){
if(A[i] != 0) {
for(int j=0; j<Length; j++){
if((B[j] != 0)&&(A[i] == (-B[j]))){
Result[i][j] = A[i];
Result[j][i] = B[j];
A[i] = 0;
B[j] = 0;
}
}
}
}
// The second run: two users pay for one.
for(int i=0; i<Length; i++){
if(A[i] != 0) {
for(int j=0; j<Length; j++){
if((B[j] != 0)&&(A[i] > (-B[j]))){
long d = A[i] + B[j];
for(int k=0; k<Length; k++){
if(d == (-B[k])){
Result[i][j] = -B[j];
Result[i][k] = -B[k];
Result[j][i] = B[j];
Result[k][i] = B[k];
A[i] = 0;
B[j] = 0;
B[k] = 0;
}
}
}else if((B[j] != 0)&&(A[i] < (-B[j]))){
long d = A[i] + B[j];
for(int k=0; k<Length; k++){
if(d == (-A[k])){
Result[i][j] = A[i];
Result[k][j] = A[k];
Result[j][i] = -A[i];
Result[j][k] = -A[k];
A[i] = 0;
B[j] = 0;
A[k] = 0;
}
}
}
}
}
}
}