-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathSimpleSorting.java
More file actions
55 lines (48 loc) · 1.6 KB
/
Copy pathSimpleSorting.java
File metadata and controls
55 lines (48 loc) · 1.6 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
package simple_sorting;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
public class SimpleSorting {
private static long sum(long number, long system){
int sum = 0;
while (number > 0){
sum += number % system;
number /= system;
}
return sum;
}
public static void main(String[] args) throws IOException {
long n = 0, k = 0, m = 0;
ArrayList<Long> data = new ArrayList<>();
Scanner scan = new Scanner(new FileReader("input.txt"));
int index = 0;
while (scan.hasNextLine()){
StringTokenizer token = new StringTokenizer(scan.nextLine(), " ");
if(index == 0){
k = Integer.valueOf(token.nextToken());
m = Integer.valueOf(token.nextToken());
}else {
while (token.hasMoreTokens()){
data.add(Long.valueOf(token.nextToken()));
}
}
index++;
}
for(int i = 0; i < data.size(); i++){
long newNumber = sum(data.get(i), k) * sum(data.get(i), m);
data.set(i, newNumber);
}
Collections.sort(data);
StringBuilder result = new StringBuilder();
for(long number : data){
result.append(String.valueOf(number)).append(" ");
}
FileWriter out = new FileWriter("output.txt");
out.write(String.valueOf(result).trim());
out.close();
}
}