1004 成绩排名 (20分)

n = gets.chomp.to_i
Student = Struct.new(:name, :num, :score)
maxstd = Student.new("","",0)
minstd = Student.new("","",101)
std = Student.new
(0...n).each { |i|
    line = ""
    line = gets.chomp.split
    std = Student.new(line[0],line[1],line[2].to_i)
    # std.name = line[0]
    # std.num = line[1]
    # std.score = line[2].to_i
    if (std.score < minstd.score)
        minstd = std
    end
    if (std.score > maxstd.score)
        maxstd = std
    end
}
puts (maxstd.name+ " " +maxstd.num)
puts (minstd.name+ " " +minstd.num)


C++:
#include <iostream>
#include <string>
using namespace std;
struct Student{
    string name;
    string num;
    int score;
};
int main(){
   int n;
   cin >> n;
   Student maxstd,minstd,std;
   maxstd.score = -1;
   minstd.score = 101;
   while(n--){
       cin >> std.name >> std.num >> std.score;
       if (std.score > maxstd.score)
           maxstd = std;
       if (std.score < minstd.score)
           minstd = std;
   }
   cout << maxstd.name << " " << maxstd.num << endl;
   cout << minstd.name << " " << minstd.num << endl;
    return 0;
}

Python:

n = int(input())
std_info = []
for i in range(3):
    std = input()
    std_info.append(std.split(' '))
res = sorted(std_info,key=lambda x:int(x[2]),reverse=True)
print(res[0][0] + ' ' + res[0][1])
print(res[-1][0]+ ' ' + res[-1][1],end='')
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。