Repository navigation
Expand file tree
/
Copy pathhash.java
More file actions
69 lines (55 loc) · 1.47 KB
/
Copy pathhash.java
File metadata and controls
69 lines (55 loc) · 1.47 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
import java.io.*;
import java.util.*;
public class hash {
private HashSet<Integer> register;
private int[] sums = new int[]{231552,234756,596873,648219,726312,981237,988331,1277361,1283379};
private int[] result = new int[9];
public hash(){
register = new HashSet<Integer>();
for(int i = 0; i < result.length; i++)
result[i] = 0;
}
public void checkSums(String filePath){
if(loadData(filePath) < 0){
System.out.println("Fail to read data");
return;
}
Iterator<Integer> it;
for(int i = 0; i < result.length; i++){
it = register.iterator();
while(it.hasNext()){
if(register.contains(sums[i]-it.next())){
result[i] = 1;
System.out.println(sums[i]+" = "+it.next()+" + "+(sums[i]-it.next()));
break;
}
}
}
System.out.println("The result is ");
for(int i = 0; i < result.length; i++)
System.out.print(result[i]);
}
private int loadData(String filePath){
try{
BufferedReader bReader = new BufferedReader(new FileReader(filePath));
String line = null;
while((line = bReader.readLine()) != null){
register.add(Integer.parseInt(line));
}
bReader.close();
System.out.println("Get "+register.size()+" lines of data");
} catch (Exception e){
e.printStackTrace();
return -1;
}
return 0;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
hash h = new hash();
h.checkSums("//media//Work//src//HashInt.txt");
}
}