1001. Score

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Student
{
    public:
        Student(string _name, int _score) {
            name = _name;
            score = _score;
        }
        bool operator < (const Student student)const {
            return score < student.score;
        }
        string name;
        int score;
};

int main()
{
    int n,m;
    cin>>n>>m;
    vector <Student> myStudent;


    for(int it=0; it<n; it++) {
        string name;
        int score;
        cin >> name >> score;
        Student temp(name, score);
        myStudent.push_back(temp);
    }
    sort(myStudent.begin(), myStudent.begin()+n);
    while(m--) {
        int num;
        cin>>num;
        cout<<myStudent[num-1].name<<" "<<myStudent[num-1].score<<endl;
    }
}                                

转载于:https://www.cnblogs.com/sysu-eeman-yang/p/5987437.html

import java.util.Arrays;import java.util.Comparator;class Student { String name; int id; String gender; int score1; int score2; int score3; public Student(String name, int id, String gender, int score1, int score2, int score3) { this.name = name; this.id = id; this.gender = gender; this.score1 = score1; this.score2 = score2; this.score3 = score3; } public String toString() { return name + "\t" + id + "\t" + gender + "\t" + score1 + "\t" + score2 + "\t" + score3; } }public class 理论2 { /** * @param args / public static void main(String[] args) { Student[] students = { new Student("张三", 1001, "男", 90, 80, 70), new Student("李四", 1002, "女", 80, 70, 60), new Student("王五", 1003, "男", 70, 60, 50), new Student("赵六", 1004, "女", 60, 50, 40) }; Arrays.sort(students, new Comparator<Student>() { public int compare(Student s1, Student s2) { int totalScore1 = s1.score1 + s1.score2 + s1.score3; int totalScore2 = s2.score1 + s2.score2 + s2.score3; return totalScore2 - totalScore1; } } ); System.out.println("姓名\t学号\t性别\t成绩1\t成绩2\t成绩3"); for (Student student : students) { System.out.println(student); } }import java.util.Arrays;import java.util.Comparator;class Student { String name; int id; String gender; int score1; int score2; int score3; public Student(String name, int id, String gender, int score1, int score2, int score3) { this.name = name; this.id = id; this.gender = gender; this.score1 = score1; this.score2 = score2; this.score3 = score3; } public String toString() { return name + "\t" + id + "\t" + gender + "\t" + score1 + "\t" + score2 + "\t" + score3; } }public class 理论2 { /* * @param args */ public static void main(String[] args) { Student[] students = { new Student("张三", 1001, "男", 90, 80, 70), new Student("李四", 1002, "女", 80, 70, 60), new Student("王五", 1003, "男", 70, 60, 50), new Student("赵六", 1004, "女", 60, 50, 40) }; Arrays.sort(students, new Comparator<Student>() { public int compare(Student s1, Student s2) { int totalScore1 = s1.score1 + s1.score2 + s1.score3; int totalScore2 = s2.score1 + s2.score2 + s2.score3; return totalScore2 - totalScore1; } } ); System.out.println("姓名\t学号\t性别\t成绩1\t成绩2\t成绩3"); for (Student student : students) { System.out.println(student); } }运行结果
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值