Each input file contains one test case. Each case is given in the following format:
N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2
where name[i] and ID[i] are strings of no more than 10 characters with no space, grade[i] is an integer in [0, 100], grade1 and grade2 are the boundaries of the grade's interval. It is guaranteed that all the grades are distinct.
For each test case you should output the student records of which the grades are in the given interval [grade1, grade2] and are in non-increasing order. Each student record occupies a line with the student's name and ID, separated by one space. If there is no student's grade in that interval, output "NONE" instead.
4 Tom CS000001 59 Joe Math990112 89 Mike CS991301 100 Mary EE990830 95 60 100
Mike CS991301 Mary EE990830 Joe Math990112
import java.util.*; class Course{ String name; String courseName; int score; public Course(String name,String courseName,int score){ this.name=name; this.courseName=courseName; this.score=score; } } public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); ArrayList<Course> courseList=new ArrayList<Course>(); for(int i=0;i<n;i++){ String name=sc.next(); String courseName=sc.next(); int score=sc.nextInt(); Course course=new Course(name,courseName,score); courseList.add(course); } int min=sc.nextInt(); int max=sc.nextInt(); Collections.sort(courseList,new Comparator<Course>(){ public int compare(Course o1,Course o2){ if(o1.score>o2.score) return -1; else if(o1.score<o2.score) return 1; else return 0; } }); int flag=0; for(int i=0;i<courseList.size();i++){ if(courseList.get(i).score>=min && courseList.get(i).score<=max){ flag=1; System.out.println(courseList.get(i).name+" "+courseList.get(i).courseName); } } if(flag==0){ System.out.println("NONE"); } } }
思路 : 排序后输出。 问题描述,就是给你 一堆同学的成绩单,要求按照中间一段分数节点进行输出。要求从大到小输出。 #include <iostream> #include <string> #include <vector> #include <algorithm> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 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 argc, char** argv) { int N; cin >> N; vector<struct stu> students; students.resize(N); for(int i=0; i<N; i++) { cin >> students[i].name >> students[i].ID >> students[i].score; } int gradeMin,gradeMax; cin >> gradeMin >> gradeMax; sort(students.begin(), students.end(), Cmp); bool count = false; for(int i=0; i<N; i++) { if(students[i].score >= gradeMin && students[i].score <= gradeMax) { cout << students[i].name << " " << students[i].ID << endl; count = true; } } if(!count) { cout << "NONE" << endl; } return 0; }
#include<stdio.h> #include<string.h> struct S{char name[10],id[10];}s[101]; int main (){//the shorter,the better. int n,i,b,e,g,c;char name[10],id[10]; for(;~scanf("%d",&n);c?:printf("NONE\n")){ for (memset(s,0,sizeof(struct S)*101),c=i=0; i < n&&~scanf("%s %s %d",name,id,&g);strcpy(s[g].name,name),strcpy(s[g].id,id),i++); for (scanf("%d %d",&b,&e),i=e;i>=b;strlen(s[i].id)==0?:(++c,printf("%s %s\n",s[i].name,s[i].id)),--i); } }
#include <iostream> #include <algorithm> using namespace std; struct student{ char name[15],id[15]; int grade; }s[10010]; bool cmp(student a,student b){ return a.grade>b.grade; } int main(){ int n;cin>>n; for(int i=0;i<n;i++) cin>>s[i].name>>s[i].id>>s[i].grade; sort(s,s+n,cmp); int g1,g2,num=0; cin>>g1>>g2; for(int i=0;i<n;i++){ if(s[i].grade>=g1 && s[i].grade<=g2){ cout<<s[i].name<<" "<<s[i].id<<'\n'; num++; } } if(num==0) cout<<"NONE"; return 0; }
#include <iostream> #include <vector> #include <string> #include <map> using namespace std; typedef pair<string, string> PAIR; int main() { int length, start, end, num; while (cin >> length) { string s1, s2; map<int, PAIR> data; for (int i = 0; i < length; i++) { cin >> s1 >> s2 >> num; PAIR p = make_pair(s1, s2); data[num] = p; } cin >> start >> end; bool sta = false; for (map<int, PAIR>::reverse_iterator it = data.rbegin(); it != data.rend(); it++) if (it->first >= start&&it->first <= end) { cout << it->second.first << " " << it->second.second << endl; sta = true; } if (false == sta) cout << "NONE" << endl; } return 0; }
//这一题很好的体现了Java中类的思想,代码看起来简洁明了。 //况且直接调用collections.sort(优化快排)能节省大量代码工作以及运行时间。 //思路很简单:因为每组grade不同,所以让grade作为map里的key,类stu为value。 //只要对key进行排序,之后取出key对应的类中的name和id即可。 import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; class stu{ String name; String id; public stu(String name, String id) { super(); this.name = name; this.id = id; } } public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); HashMap<Integer,stu> map=new HashMap<Integer,stu>(); ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0;i<n;i++){ String name=in.next(); String id=in.next(); int grade=in.nextInt(); map.put(grade,new stu(name,id)); list.add(grade); } int min=in.nextInt(); int max=in.nextInt(); int count=0; Collections.sort(list); for(int i=n-1;i>=0;i--){ int grade=list.get(i); if(grade>=min&&grade<=max){ count++; stu s=map.get(grade); System.out.println(s.name+" "+s.id); } } if(count==0){ System.out.println("NONE"); } } }
没有难度🤣 #include<bits/stdc++.h> using namespace std; struct P { char name[15]; char id[15]; int grade; bool operator <(const P & a) { return grade>a.grade; } } stu[10010]; int main() { int n; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%s %s %d",stu[i].name,stu[i].id,&stu[i].grade); } sort(stu,stu+n); int grade1,grade2; scanf("%d %d",&grade1,&grade2); bool flag=0; for(int i=0; i<n; i++) { if(grade1<=stu[i].grade&&stu[i].grade<=grade2) { printf("%s %s\n",stu[i].name,stu[i].id); flag=1; } } if(!flag) printf("NONE\n"); return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int grade; List<List> grades = new ArrayList<>(); boolean tag; for (int i = 0; i < n; i++) { tag = true; List list = new ArrayList<>(); list.add(sc.next()); list.add(sc.next()); grade = sc.nextInt(); list.add(grade); for (int j = 0; j < grades.size(); j++) { if(grade > (int) grades.get(j).get(2)){ grades.add(j,list); tag = false; break; } } if(tag)grades.add(grades.size(), list); } int grade1 = sc.nextInt(); int grade2 = sc.nextInt(); tag = true; for (List list : grades) { grade = (int)list.get(2); if(grade>=grade1&&grade<=grade2){ System.out.println(list.get(0)+" "+list.get(1)); tag = false; } } if(tag){ System.out.println("NONE"); } } }
不用排序,题目给定要求就是每个分数不想等,直接数组就好 #include<iostream> #include<string> #include<vector> using namespace std; int main(){ vector<pair<string, string>> grades(101,{"null", "null"}); int len;cin>>len; for(int i=0;i<len;i++){ string a, b; int c; cin>>a>>b>>c; grades[c] = {a, b}; } int up, low;cin>>low>>up; int cal = 0; for(int i=up;i>=low;i--){ if(grades[i].second!="null"){ cout<<grades[i].first<<" "<<grades[i].second<<endl; cal++; } } if(cal==0) cout<<"NONE"<<endl; return 0; }
// A1083ListGrade.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include<string> #include<vector> #include<algorithm> using namespace std; struct student { string id, name; int grade; }; bool cmp(student* s1, student* s2) { if (s1->grade != s2->grade) { return s1->grade > s2->grade; } } void ListGrade() { int n; vector<student*> vs; cin >> n; for (int i = 0; i < n; i++) { student* s = new student(); cin >> s->name >> s->id >> s->grade; vs.push_back(s); } sort(vs.begin(), vs.end(), cmp); int grade1, grade2; cin >> grade1 >> grade2; vector<student*>vs2; for (auto it = vs.begin(); it != vs.end(); it++) { if ((*it)->grade >= grade1 && (*it)->grade <= grade2) { vs2.push_back(*it); } } if (vs2.size() != 0) { for (int i = 0; i < vs2.size(); i++) { // cout << vs2[i]->name << vs[2]->id << vs[2]->grade; printf("%s %s\n", vs2[i]->name.c_str(), vs2[i]->id.c_str()); } } else { printf("%s", "NONE"); } } int main() { ListGrade(); // std::cout << "Hello World!\n"; }
a = int(input());b = [] for i in range(a): b.append(input().split(' ')) c = input().split(' ') d = {} for i in b: if int(i[2]) >= int(c[0]) and int(i[2]) <= int(c[1]): d[int(i[2])] = i[0] + ' ' + i[1] if d: for i in sorted(d,reverse = True): print(d[i]) else: print('NONE')
#include <bits/stdc++.h> using namespace std; struct stu { string name,ID; //学生的姓名和学号 int grade; }; bool cmp(stu a,stu b) //按照分数降序排列 { return a.grade > b.grade; } int main() { int N; cin >> N; vector<stu> v; for(int i = 0; i < N; i++) { string name,ID; int grade; cin >> name >> ID >> grade; v.push_back({name,ID,grade}); } int grade1,grade12; cin >> grade1 >> grade12; sort(v.begin(),v.end(),cmp); //将学生按照分数降序排列 bool flag = false; //判断有没有学生的分数在[grade1,grade2]这个区间内 for(auto it : v) { if(it.grade >= grade1 && it.grade <= grade12) { flag = true; cout << it.name << " " << it.ID << endl; } } if(!flag) { cout << "NONE" << endl; } return 0; }
#include <algorithm> #include <iostream> #include <cstdio> #include <string> #include <vector> using namespace std; class Student{ private: string s_name; string s_no; int s_grade; public: void set_value(string name, string no, int grade){ s_name = name; s_no = no; s_grade = grade; } void show(){ cout << s_name << " " << s_no; } int show_grade(){ return s_grade; } }; bool compare(Student s1, Student s2){ return s1.show_grade() > s2.show_grade(); } int main(){ int n, grade1, grade2, grade; string name, no; vector<Student> std_vector; scanf("%d", &n); while(n--){ cin >> name >> no >> grade; Student s; s.set_value(name, no, grade); std_vector.push_back(s); } scanf("%d %d", &grade1, &grade2); sort(std_vector.begin(), std_vector.end(), compare); auto iter1 = std_vector.begin(); auto iter2 = std_vector.end() - 1; if(iter2->show_grade() > grade2 || iter1->show_grade() < grade1 || grade1 > grade2){ cout << "NONE" << endl; }else{ bool ok = false; for(auto iter = std_vector.begin();iter!=std_vector.end();iter++){ if(iter->show_grade() >= grade1 && iter->show_grade() <= grade2) { ok = true; iter->show(); cout << endl; } } if(!ok) cout << "NONE" << endl; } return 0; }
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=102; struct student{ char name[12],id[12]; int grade; }; student stu[maxn]; int n,grade1,grade2; bool cmp(const student &a,const student &b){ return a.grade<b.grade; } void solve(){ sort(stu,stu+n,cmp); if(stu[0].grade>grade2||stu[n-1].grade<grade1||grade1>grade2){ printf("NONE\n"); } else{ bool ok=false; for(int i=n-1;i>=0;i--){ if(stu[i].grade>=grade1&&stu[i].grade<=grade2){ ok=true; printf("%s %s\n",stu[i].name,stu[i].id); } else if(stu[i].grade<grade1) break; } if(!ok){ printf("NONE\n"); } } } int main(){ //freopen("in.txt","r",stdin); scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s%s%d",stu[i].name,stu[i].id,&stu[i].grade); } scanf("%d%d",&grade1,&grade2); solve(); return 0; }
#include <iostream> #include <vector> #include <algorithm> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; struct record{ char name[11]; char ID[11]; int grade; }; bool compare(record r1,record r2){ return r1.grade > r2.grade; } bool judge = false; vector<record> Records; int main(int argc, char** argv) { int N, grade1, grade2; cin >> N; for(int i = 0;i < N;i++){ record r; cin >> r.name; cin >> r.ID; cin >> r.grade; Records.push_back(r); } cin >> grade1 >> grade2; sort(Records.begin(),Records.end(),compare); for(int i = 0;i < Records.size();i++){ record r = Records[i]; if(r.grade >= grade1 && r.grade <= grade2){ cout << r.name << " " << r.ID << endl; judge = true; } } if(judge == false){ cout << "NONE" << endl; } return 0; }
class student: def __init__(self, name, number, score): self.name = name self.number = number self.score = score def __cmp__(self, other): return -cmp(self.score, other.score) def out(self): print('%s %s' % (self.name, self.number)) N = input() group = ['']*N for i in range(N): name, number, score = raw_input().split() group[i] = student(name, number, int(score)) L, H = raw_input().split() L = int(L); H = int(H) conditionalGroup = [i for i in group if i.score>=L and i.score<=H] conditionalGroup.sort() if conditionalGroup: for i in conditionalGroup: i.out() else: print('NONE')
#include<iostream> #include<cstdio> #include<algorithm> #include<string> using namespace std; typedef struct { string name,Id;int grade; }stu; stu s[101]; bool cmp(stu a,stu b) { return a.grade>b.grade; } int main() { int n,i,a,b,flag; while(cin>>n) { flag=1; for(i=0;i<n;i++) cin>>s[i].name>>s[i].Id>>s[i].grade; sort(s,s+n,cmp);cin>>a>>b; for(i=0;i<n;i++) if(s[i].grade>=a&&s[i].grade<=b) { flag=0;cout<<s[i].name<<" "<<s[i].Id<<endl; } if(flag) cout<<"NONE"<<endl; } return 0; } //结构体排序,水过。
把每个学生的信息保存到Student对象中,并把该对象加入链表中,最后对链表排序,遍历链表并调用print方法即可。另外,还要注意如果没有满足条件的学生,输出NONE。
完整代码如下