首页 > 试题广场 >

成绩排名 (20)

[编程题]成绩排名 (20)
  • 热度指数:3893 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入描述:
每个测试输入包含1个测试用例,格式为

第1行:正整数n
第2行:第1个学生的姓名 学号 成绩
第3行:第2个学生的姓名 学号 成绩
... ... ...
第n+1行:第n个学生的姓名 学号 成绩

其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。


输出描述:
对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。
示例1

输入

3<br/>Joe Math990112 89<br/>Mike CS991301 100<br/>Mary EE990830 95

输出

Mike CS991301<br/>Joe Math990112

python 解法:

a, arr = int(input()), []
for i in range(a):
    arr.append(input().split())
arr.sort(key=lambda c: int(c[2]))
print(arr[-1][0] + " " + arr[-1][1])
print(arr[0][0] + " " + arr[0][1])
发表于 2017-10-11 09:21:26 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;//人数
    int i = 0;
    string name;//临时姓名
    string id;//临时学号
    int score, maxscore = 0, minscore = 100;//score临时分数
    string max_name_id, min_name_id;//max_name_id最高分数对应的姓名和学号
    cin >> n;
    for (i = 0; i < n; i++)
    {
        cin >> name >>id >>score;
        if (score >= maxscore)//找最高分
        {
            maxscore =score;
            max_name_id = name + ' ' + id;//姓名和学号放在一个字符变量里,之间添加一个空格
        }
        if (score <= minscore)//找最低分
        {
            minscore = score;
            min_name_id = name + ' ' + id;
        }
    }
    cout <<max_name_id << endl << min_name_id;
    return 0;
}

发表于 2015-06-19 16:21:53 回复(0)
#include "bits/stdc++.h"
using namespace std;
#define MAX 10000
struct student
{
	char id[100];
	float score;
	char name[100];
} stu[MAX];
bool cmp(struct student a,struct student b)
{
	return a.score>b.score;
}
int main()
{
	int n,i=0;
	cin>>n;
	for(i=0; i<n; i++)
		{
			cin>>stu[i].name>>stu[i].id>>stu[i].score;
		}
	sort(stu,stu+n,cmp);
	cout<<stu[0].name<<" "<<stu[0].id<<endl;
	cout<<stu[n-1].name<<" "<<stu[n-1].id<<endl;
    return 0;
}

编辑于 2018-04-11 13:00:57 回复(1)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main implements Comparable<Main>{
	String name;
	String number;
	int score;	
	public Main(String name, String number, int score) {
		super();
		this.name = name;
		this.number = number;
		this.score = score;
	}
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		ArrayList<Main> array=new ArrayList<Main>();
		while(n-->0){
			Main main=new Main(in.next(),in.next(),in.nextInt());		
			array.add(main);
		}
		Collections.sort(array);
		System.out.println(array.get(0).name+" "+array.get(0).number);
		System.out.println(array.get(array.size()-1).name+" "+array.get(array.size()-1).number);
	}	
	public int compareTo(Main o) {		
		return o.score-this.score;
	}
}

发表于 2017-03-01 22:50:25 回复(0)
#include <stdio.h>
#include <string.h>

typedef struct Student{
    char name[11];
    char num[11];
    int score;
}Student;

int main()
{
    int i,n;
    struct Student Student[2];
    Student[0].score=-1;
    Student[1].score=101;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        char name[11],num[11];
        int score;
        scanf("%s %s %d",name,num,&score);
        if(score>Student[0].score){
            strcpy(Student[0].name,name);
            strcpy(Student[0].num,num);
            Student[0].score=score;
        }
        if(score<Student[1].score){
            strcpy(Student[1].name,name);
            strcpy(Student[1].num,num);
            Student[1].score=score;
        }
    }
    printf("%s %s\n",Student[0].name,Student[0].num);
    printf("%s %s",Student[1].name,Student[1].num);
    return 0;
}


发表于 2016-01-04 08:59:09 回复(0)
#include <stdio.h>
#include <string.h>
 
int main(){
    int n,i,j,chengji;
    char name[10],xuehao[10];
    char a[10001][11],b[10001][11];
    int c[10001];
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s%s%d",name,xuehao,&chengji);
        for(j=0;j<strlen(name);j++){
            a[i][j]=name[j];
        }
        for(j=0;j<strlen(xuehao);j++){
            b[i][j]=xuehao[j];
        }
        c[i]=chengji;
    }
    int max=c[0],min=c[0];
    int MAX=0,MIN=0;
    for(i=0;i<n;i++){
        if(c[i]>max){
            max=c[i];MAX=i;
        }
        if(c[i]<min){
            min=c[i];MIN=i;
        }
    }
    printf("%s %s\n",a[MAX],b[MAX]);
    printf("%s %s\n",a[MIN],b[MIN]);
}
求dl帮忙看看,样例本地输出无误,但上传后却说答案错误,不知道哪个细节没注意到
发表于 2020-11-22 22:12:08 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            ArrayList<Student> list=new ArrayList<Student>();
            for(int i=0;i<n;i++){
                String name=sc.next();
                String course=sc.next();
                int score=sc.nextInt();
                Student student=new Student(name,course,score);
                list.add(student);
            }
            Collections.sort(list, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    if(o1.score<o2.score){
                        return -1;
                    }else if(o1.score>o2.score){
                        return 1;
                    }else{
                        return 0;
                    }
                }
            });
            System.out.println(list.get(list.size()-1).name+" "+list.get(list.size()-1).course);
            System.out.println(list.get(0).name+" "+list.get(0).course);
        }
    }
}

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

发表于 2018-10-02 00:52:19 回复(0)
// 每次输入比较分数,大于最高分则赋予最大值,小于最小值则赋予最小值 #include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string name, IDNum;
    int score;
    string max_name, max_IDNum, min_name, min_IDNum;
    int max_score, min_score;
    max_score = 0;
    min_score = 100;
    cin>>n;
    while(n--)
    {
        cin>>name>>IDNum>>score;
        if (score > max_score)
        {
            max_score = score;
            max_name = name;
            max_IDNum = IDNum;
        }
        if (score < min_score)
        {
            min_score = score;
            min_name = name;
            min_IDNum = IDNum;
        }
    }
    cout<<max_name<<' '<<max_IDNum<<endl;
    cout<<min_name<<' '<<min_IDNum<<endl;
    system("pause");
    return 0;
}

发表于 2018-01-21 13:46:06 回复(0)
__author__ = 'Yaicky'

while True:
    try:
        n = input()
        highmarks = []
        lowmarks = []
        highmark = -1
        lowmark = 101
        for i in range(n):
            string = raw_input().strip().split()
            if int(string[2]) < lowmark:
                lowmarks.append([string[0], string[1]])
                lowmark = int(string[2])
            if int(string[2]) > highmark:
                highmarks.append([string[0], string[1]])
                highmark = int(string[2])


        rltStr = highmarks.pop()
        print ' '.join(rltStr)
        rltStr = lowmarks.pop()
        print ' '.join(rltStr)

    except:
        break

发表于 2016-07-01 11:27:04 回复(0)
#include<bits/stdc++.h>
using namespace std;

const int Max=1001;

struct Student{
	string name;
	string id;
	int score;
	bool operator <(const Student & a){
		return score>a.score;
	}
}s[Max];

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s[i].name>>s[i].id>>s[i].score;
	}
	sort(s,s+n);
	cout<<s[0].name<<" "<<s[0].id<<endl;
	cout<<s[n-1].name<<" "<<s[n-1].id<<endl;
	return 0;
}

发表于 2022-11-03 20:09:50 回复(1)


水题吧,砍瓜切菜。。。
记录最大、最小得分,如果超过了两个界限值,更新记录的信息即可。

#include <iostream>
(720)#include <cstring>
using namespace std;

int main() {
    //最大分数、最小分数,注意初始化赋值
    int n = 0, maxScore = 0, minScore = 100, score = 0;
    //最大得分者的name、no、最小得分者的name、no
    char maxName[11], maxNo[11], minName[11], minNo[11], name[11], no[11];
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%s %s %d", name, no, &score);
        if (score > maxScore) {
            //超过了已有的最大分数,更新
            maxScore = score;
            strcpy(maxName, name);
            strcpy(maxNo, no);
        }
        if (score < minScore) {
            //小于已有的最小得分,更新
            minScore = score;
            strcpy(minName, name);
            strcpy(minNo, no);
        }
    }
    printf("%s %s\n%s %s\n", maxName, maxNo, minName, minNo);
    return 0;
}
————————————————
版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://hestyle.blog.csdn.net/article/details/104754806
发表于 2020-03-09 16:14:19 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX 65535
 
typedef struct student
{
    char name[20];
    char no[20];
    int grade;
}STU;
 
STU stu[MAX];
 
int cmp(const void* a, const void* b)
{
    STU *stu1 = (STU*) a;
    STU *stu2 = (STU*) b;
    return (stu2->grade - stu1->grade);
}
 
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        for(int i = 0; i < n; i++)
            scanf("%s %s %d", stu[i].name, stu[i].no, &stu[i].grade);
        qsort(stu, n, sizeof(stu[0]), cmp);
        printf("%s %s\n%s %s\n", stu[0].name, stu[0].no, stu[n-1].name, stu[n-1].no);
    }
 
    return 0;
}

编辑于 2020-03-04 18:07:23 回复(0)
同样的例子我在编译器没问题,但是在这里就错??
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int num, score, i, j, minscore = 101, maxscore = -1, count = 0;
    char sname[10],sno[10];
    char min_sname[10], max_sname[10], min_sno[10], max_sno[10];
    
    scanf("%d",&num);
    for(i = 0; i < num; i++){
        scanf("%s %s %d",sname, sno, &score);
        if(score>maxscore){    // 找最高分 
            maxscore = score;
            for(j = 0; j < 10; j++){
                max_sname[j] = sname[j];
                max_sno[j] = sno[j];
                count = j;
            }
            max_sname[count] = '\0';
            max_sno[count] = '\0';
        }
        if(score<minscore){ // 找最低分 
            minscore = score;
            for(j = 0; j < 10; j++){
                min_sname[j] = sname[j];
                min_sno[j] = sno[j];
                count = j;
            }
            min_sname[count] = '\0';
            min_sno[count] = '\0';
        }
    }
    printf("%s %s\n",max_sname,max_sno);
    printf("%s %s\n",min_sname,min_sno);    
    return 0;    
}
请大佬康康
发表于 2020-01-14 18:17:04 回复(0)
def main():
    num = int(input())
    low = ['','','100']
    high = ['','','0']
    for i in range(num):
        temp = input().split()
        if int(temp[-1]) < int(low[-1]):
            low = temp
        if int(temp[-1]) > int(high[-1]):
            high = temp
    print(high[0],high[1])
    print(low[0],low[1])


if __name__ == '__main__':
    main()

发表于 2019-04-27 21:01:03 回复(0)
学渣快乐题

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef struct student{
    string name;
    string stn;//student'snumber
    int grd;//student's grade
}student;
int main(){
    int n;
    cin>>n;
    student stud;
    cin>>stud.name>>stud.stn>>stud.grd;
    student goodstud=stud,badstud=stud;
    while(--n){
        cin>>stud.name>>stud.stn>>stud.grd;
        if(stud.grd<badstud.grd)badstud=stud;
        if(stud.grd>goodstud.grd)goodstud=stud;
    }
    cout<<goodstud.name<<" "<<goodstud.stn<<endl;
    cout<<badstud.name<<" "<<badstud.stn<<endl;
    return 0;
}

发表于 2019-04-02 17:11:08 回复(0)
import java.util.*;

/**
 * 成绩排名
  * 利用集合的sort方法进行排序
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(new Student(scanner.next(), scanner.next(), scanner.nextInt()));
        }
        Collections.sort(list);
        System.out.println(list.get(0).name + " " + list.get(0).id);
        System.out.println(list.get(n - 1).name + " " + list.get(n - 1).id);
    }
}

class Student implements Comparable<Student> {
    String name;
    String id;
    Integer score;

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

    @Override
    public int compareTo(Student student) {
        return student.score.compareTo(this.score);
    }
}

编辑于 2018-12-24 14:15:34 回复(0)
思路: 快排的应用。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

struct stu
{
    string name;
    string ID;
    int score;
};

bool Cmp(struct stu & a, struct stu & b)
{
    return a.score > b.score;
}

int main()
{
    int n;
    while (cin >> n)
    {
        vector<struct stu> v(n);
        for (int i = 0; i < n; i++)
        {
            cin >> v[i].name >> v[i].ID >> v[i].score;
        }
        sort(v.begin(), v.end(), Cmp);
        cout << v[0].name << " " << v[0].ID << endl;
        cout << v[v.size()-1].name << " " << v[v.size() - 1].ID << endl;
    }
}

发表于 2018-08-13 22:27:31 回复(0)

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, Integer> map = new HashMap<String, Integer>();
while(scanner.hasNext()){
int n = scanner.nextInt();
for(int i=0;i<n;i++){ String name = scanner.next(); String xh = scanner.next(); int value = scanner.nextInt(); String key = name+" "+xh; map.put(key, value); } Collection<Integer> collection = map.values();
Object[] objects = collection.toArray();
Arrays.sort(objects);
Object maxValue = objects[objects.length-1];
Object minValue = objects[0];
String maxKey = getKey(map,maxValue);
String minKey = getKey(map,minValue);
System.out.println(maxKey);
System.out.println(minKey);
}
}
private static String getKey(Map<String, Integer> map, Object value) {
String key = null;
for(String getKey:map.keySet()){
if (map.get(getKey).equals(value)) {
key = getKey;
}
}
return key;
}

发表于 2018-07-02 17:38:38 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){
     int n= Integer.parseInt(sc.nextLine());
     List<String>list0=new ArrayList<String>();
     List<String>list1=new ArrayList<String>();
     List<Integer>list2=new ArrayList<Integer>();
    for(int i=0;i<n;i++){
        String[] str=sc.nextLine().split(" ");
        list0.add(str[0]);
        list1.add(str[1]);
        list2.add(Integer.parseInt(str[2]));
    }
int Max=0;
int MaxIndex=0;
int Min=100;    
int MinIndex=0;
    for(int i=0;i<list2.size();i++){
        if(list2.get(i)>Max){
            Max=list2.get(i);
            MaxIndex=i;
             }
        if(list2.get(i)<=Min){
            Min=list2.get(i);
            MinIndex=i;
            }
        }
      System.out.println(list0.get(MaxIndex)+" "+list1.get(MaxIndex));
      System.out.println(list0.get(MinIndex)+" "+list1.get(MinIndex));
      }
   }
}
发表于 2018-06-22 11:34:01 回复(0)
upo头像 upo
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

struct Student{   //Student 的申明必须在(cmp)前面
    string name;
    string id;
    int score;
};
bool cmp( struct Student s1 , struct Student s2){ 
    return s1.score > s2.score;
}

int main(){
    int n;
    cin>>n;
    vector<Student> vec(n);
    Student stu;

    for(int i=0;i<n;i++){
        cin>>stu.name>>stu.id>>stu.score;
        vec.push_back(stu);
    }
    sort(vec.begin(),vec.end(),cmp);
    cout<<vec[0].name<<" "<<vec[0].id<<endl;
    cout<<vec[n-1].name<<" "<<vec[n-1].id;

return 0;
}
发表于 2018-03-17 02:01:53 回复(0)