字串判断+成绩排序

二叉树的前中后序列递归实现

public boolean contains(CharSequence s) ,用作当且仅当此字符串包含指定的 char 值序列时,返回 true。注意这里的参数是一个CharSequence,如果我们用char作为参数,比如:
String s = “abcd”;
System.out.println(s.contains(‘a’));
会报语法错误:The method contains(CharSequence) in the type String is not applicable for the arguments (char)。参数类型不匹配。

成绩排序

字串判断+成绩排序_第1张图片字串判断+成绩排序_第2张图片输入:
3
0
fang 90
yang 50
ning 70

输出:
fang 90
ning 70
yang 50

import java.util.*;


class Student{
        String name;
        int score;
     Student(String name,int score){
        this.name=name;
        this.score=score;
        }
    }

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int k = in.nextInt();
            List<Student> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                list.add(new Student(in.next(), in.nextInt()));
            }
            //降序
            if (k == 0) {
                Collections.sort(list, new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.score - o1.score;
                    }
                });
            }
            //升序
            else if (k == 1) {
                Collections.sort(list, new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o1.score - o2.score;
                    }
                });
            }

            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).name + " " + list.get(i).score);
            }
        }
    }
}
``

你可能感兴趣的:(java)