1004 成绩排名 (20 分)

195 阅读1分钟

题目链接

  1. 这道题比较简单一次遍历就可以,记录遍历过程中最小结果和最大结果。

C++代码

#include <iostream>
#include<string>
#include<vector>
#include <map>
using namespace std;

class student {
public:
	string name;
	string id;
	int score;
	student(string name, string id, int score) {
		this->name = name;
		this->id = id;
		this->score = score;
	}
};
int main() {
	student max("", "", -1);
	student min("", "", 1000);
	student temp("", "", 0);
	int n;
	cin >> n;
	while (n--) {
		cin >> temp.name >> temp.id >> temp.score;
		if (temp.score > max.score)
			max = temp;
		if (temp.score < min.score)
			min = temp;
	}
	cout << max.name << " " << max.id << endl;
	cout << min.name << " " << min.id << endl;
	return 0;
}

python3代码

class student:
    name=""
    id=""
    score = 0

    def __init__(self,name,id,score):
        self.name = name
        self.id = id
        self.score = score

def main():
    max = student("","",-1)
    min = student("","",1000)
    n = input()
    n = int(n)
    for i in range(n):
        s = input()
        tmp = s.split(" ")
        if(max.score < int(tmp[2])):
            max.name = tmp[0]
            max.id = tmp[1]
            max.score = int(tmp[2])

        if(min.score > int(tmp[2])):
            min.name = tmp[0]
            min.id = tmp[1]
            min.score = int(tmp[2])

    print('{} {}'.format(max.name,max.id))
    print('{} {}'.format(min.name, min.id))

main()