主要是学习类如何进行排序利用 arrays 嗯 还有反正就是数组排序的学习
代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Comparator;
class node{
public int beginn , endd;
node(){}
node(int x,int y){
beginn = x;
endd = y;
}
}
public class Main {
private static Scanner sc;
public static void main(String[] args)throws FileNotFoundException{
try {
sc = new Scanner(new FileInputStream("E:\\input.txt"));
int n = sc.nextInt();
node s[] = new node[n + 1];
for(int i = 1; i <= n; i++)
{
s[i] = new node();
s[i].beginn = sc.nextInt();
s[i].endd = sc.nextInt();
}
Comparator<node> cmp = new Comparator<node>(){
@Override
public int compare(node o1, node o2) {
if(o1.endd != o2.endd)
return o1.endd - o2.endd; //从小到大
else
return o1.beginn - o2.beginn; //从小到大
}
};
Arrays.sort(s, 1, n + 1, cmp);
// for(int i = 1; i <= n; i++)
// System.out.println(s[i].beginn + " " + s[i].endd);
int ans = 1;//表示可以开多少场活动
int now = 1;//表示现在在哪里了
for(int i = 2; i <= n; i ++)
{
if(s[i].beginn >= s[now].endd)
{
ans ++;
now = i;
}
}
PrintStream ps = new PrintStream("E:\\output.txt");
System.setOut(ps);//把创建的打印输出流赋给系统。即系统下次向 ps输出
System.out.println(ans);
}catch(FileNotFoundException e) {
e.printStackTrace();
}
}
}
这个东西吧 主要学习
把类弄在外部,就可以实例化啦
Comparator<node> cmp = new Comparator<node>(){
@Override
public int compare(node o1, node o2) {
if(o1.endd != o2.endd)
return o1.endd - o2.endd; //从小到大
else
return o1.beginn - o2.beginn; //从小到大
}
};
Arrays.sort(s, 1, n + 1, cmp);
/*
Arrays的sort第一个是要排序的数组的名称
第二个是要排序开始的下标
第三个是排序结束的下标 这边要注意的就是左闭右开
第四个是自定义的排序方法
*/
/*
还有就是用Comparator <node> cmp = new Comparator<node>(){
};//这边记得加分号啊
还有奥 就是第一个减去第二个是从小到大......
第二个减去第一个是从大到小.....哭卿卿