-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbusnumbers.java
More file actions
40 lines (33 loc) · 745 Bytes
/
busnumbers.java
File metadata and controls
40 lines (33 loc) · 745 Bytes
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class busnumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numbers = scan.nextInt();
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < numbers; i++)
nums.add(scan.nextInt());
Collections.sort(nums);
for (int i = 0; i < numbers; i++)
{
int length = 1;
System.out.print(nums.get(i));
for (int j = 1; i + j < nums.size(); j++)
if (nums.get(i + j) == nums.get(i + j - 1) + 1)
length++;
else
break;
if (length > 2)
{
System.out.print("-" + (nums.get(i) + length - 1) + " ");
i += length - 1;
}
else
{
System.out.print(" ");
}
}
scan.close();
}
}