-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbusnumber.java
78 lines (53 loc) · 1.36 KB
/
busnumber.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
import java.util.ArrayList;
import java.util.Scanner;
public class busnumber {
public static void solve(int[] a){
SelectionSort(a);
ArrayList<String> list = new ArrayList<>();
for(int i=0; i<a.length; i++){
list.add(a[i] + "");
}
for(int i=1; i<list.size()-1; i++){
if(a[i] + 1 == a[i+1] && a[i] - 1 == a[i-1]){
list.set(i, "-");
}
}
for(int i=0; i < list.size()-1; i++){
if(list.get(i).equals("-")){
System.out.print("-");
while(list.get(i+1).equals("-")){
i++;
}
}
else if(list.get(i+1).equals("-")) System.out.print(list.get(i));
else System.out.print(list.get(i) + " ");
}
System.out.print(list.get(list.size()-1));
}
public static void SelectionSort(int[] a){
for(int i=0; i < a.length-1; i++) {
int mindex=i;
for(int j=i+1; j<a.length; j++){
if(a[j] < a[mindex]) mindex = j;
}
if(mindex != i) {
int t = a[i];
a[i] = a[mindex];
a[mindex]= t;
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int times = scan.nextInt();
int[] nums = new int[times];
for(int i=0; i<times; i++){
nums[i] = scan.nextInt();
}
if(times == 1) System.out.print(nums[0]);
else{
solve(nums);
}
scan.close();
}
}