#include<bits/stdc++.h> using namespace std; struct node { int st,et; }T[110]; bool cmp(node x,node y) { return x.et<y.et; } int n,ans,c; int main() { while(cin>>n&&n) { ans=1; for (int i=0;i<n;i++) cin>>T[i].st>>T[i].et; sort(T,T+n,cmp); c=T[0].et; for (int i=1;i<n;i++) { if (T[i].et>c&&T[i].st>=c) { ans++; c=T[i].et;; } } cout<<ans<<endl; } return 0; }转java
时间: 2023-07-15 16:14:13 浏览: 85
import java.util.Scanner;
import java.util.Arrays;
class Main {
static class Node {
int st, et;
Node(int st, int et) {
this.st = st;
this.et = et;
}
}
static int n, ans, c;
static Node[] T = new Node[110];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
n = sc.nextInt();
if (n == 0) break;
ans = 1;
for (int i = 0; i < n; i++) {
T[i] = new Node(sc.nextInt(), sc.nextInt());
}
Arrays.sort(T, 0, n, (x, y) -> Integer.compare(x.et, y.et));
c = T[0].et;
for (int i = 1; i < n; i++) {
if (T[i].et > c && T[i].st >= c) {
ans++;
c = T[i].et;
}
}
System.out.println(ans);
}
sc.close();
}
}
阅读全文