创建了简单的二元类,排序。
1 //author:pz 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.Scanner; 7 8 class Data { 9 int ts; 10 int te; 11 12 Data(int s, int e) { 13 ts = s; 14 te = e; 15 } 16 17 void print() { 18 System.out.print("(" + ts + "," + te + ")"); 19 } 20 } 21 22 public class Main { 23 public static void main(String[] args) throws IOException { 24 Scanner in = new Scanner(System.in); 25 while (in.hasNext()) { 26 int n = in.nextInt(); 27 if (n == 0) 28 return; 29 List<Data> list = new ArrayList<Data>(); 30 for (int i = 0; i < n; i++) { 31 int a = in.nextInt(); 32 int b = in.nextInt(); 33 Data d = new Data(a, b); 34 insert(list, d); 35 } 36 int ans = 1; 37 int time = list.get(0).te; 38 for (int i = 1; i < n; i++) { 39 if (list.get(i).ts >= time) { 40 ++ans; 41 time = list.get(i).te; 42 } 43 } 44 System.out.println(ans); 45 } 46 } 47 48 private static void insert(List<Data> list, Data d) { 49 if (list.isEmpty()) { 50 list.add(d); 51 return; 52 } 53 for (int i = 0; i < list.size(); i++) { 54 if (list.get(i).te > d.te) { 55 list.add(i, d); 56 return; 57 } 58 } 59 list.add(d); 60 return; 61 } 62 }