Repository navigation
Expand file tree
/
Copy pathdidi.java
More file actions
74 lines (70 loc) · 2.91 KB
/
Copy pathdidi.java
File metadata and controls
74 lines (70 loc) · 2.91 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
71
72
73
74
package didi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class didi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 2 * n - 1; i++) {
buffer.append(sc.next()).append(" ");
}
String str = buffer.toString();
String[] s = str.split(" ");
int[] number = new int[n];
String[] fuhao = new String[n - 1];
int i = 0;
int j = 0;
for (String a : s) {
if(a.equals("+") || a.equals("-") || a.equals("*") || a.equals("/")) fuhao[j++] = a;
else number[i++] = Integer.valueOf(a);
}
StringBuffer stringBuffer = new StringBuffer();
for (int z = 0; z < fuhao.length; z++) {
if (fuhao[z].equals("+")) {
int count = z + 1;
while (count < fuhao.length && fuhao[count].equals(fuhao[z])) count++;
List<Integer> list = new ArrayList<>();
for (int g = z; g < count; g++) {
list.add(number[g]);
}
z = count;
Collections.sort(list);
for (int num : list) stringBuffer.append(num).append(" ").append("+").append(" ");
}else if (fuhao[z].equals("-")) {
int count = z + 1;
while (count < fuhao.length && fuhao[count].equals(fuhao[z])) count++;
List<Integer> list = new ArrayList<>();
for (int g = z; g < count; g++) {
list.add(number[g]);
}
z = count;
Collections.sort(list);
for (int num : list) stringBuffer.append(num).append(" ").append("-").append(" ");
} else if (fuhao[z].equals("/")) {
int count = z + 1;
while (count < fuhao.length && fuhao[count].equals(fuhao[z])) count++;
List<Integer> list = new ArrayList<>();
for (int g = z; g < count; g++) {
list.add(number[g]);
}
z = count;
Collections.sort(list);
for (int num : list) stringBuffer.append(num).append(" ").append("/").append(" ");
} else if (fuhao[z].equals("*")) {
int count = z + 1;
while (count < fuhao.length && fuhao[count].equals(fuhao[z])) count++;
List<Integer> list = new ArrayList<>();
for (int g = z; g < count; g++) {
list.add(number[g]);
}
z = count;
Collections.sort(list);
for (int num : list) stringBuffer.append(num).append(" ").append("*").append(" ");
}
}
System.out.println(stringBuffer.toString());
}
}