Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.
For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM. If one such kind of student is missing, output "Absent" in the corresponding line, and output "NA" in the third line instead.
3<br/>Joe M Math990112 89<br/>Mike M CS991301 100<br/>Mary F EE990830 95
Mary EE990830<br/>Joe Math990112<br/>6
__author__ = 'Yaicky' while True: try: n = input() man = [] minGrade = 110 maxGrade = -1 female = [] for i in range(n): string = raw_input().strip().split() if string[1] == 'M': if int(string[3]) < minGrade: man.append([string[0],string[2]]) minGrade = int(string[3]) elif string[1] == 'F': if int(string[3]) > maxGrade: female.append([string[0],string[2]]) maxGrade = int(string[3]) flag = True if maxGrade == -1: print "Absent" flag = False else: rltStr = female.pop() print ' '.join(rltStr) if minGrade == 110: print "Absent" flag = False else: rltStr = man.pop() print ' '.join(rltStr) print maxGrade - minGrade if flag else "NA" except: break
//其实可以一个个输入进行判断缩减很多代码,但是java的接口方便就不用动脑子了。。。 import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; class stu{ String name; String sex; String id; int grade; public stu(String name, String sex, String id, int grade) { super(); this.name = name; this.sex = sex; this.id = id; this.grade = grade; } } public class Main { public static void main(String args[]){ Scanner in=new Scanner(System.in); int n=in.nextInt(); ArrayList<Integer> boys=new ArrayList<Integer>(); ArrayList<Integer> girls=new ArrayList<Integer>(); HashMap<Integer,stu> map=new HashMap<Integer,stu>(); for(int i=0;i<n;i++){ String name=in.next(); String sex=in.next(); String id=in.next(); int grade=in.nextInt(); stu s=new stu(name, sex, id, grade); map.put(grade,s); if(sex.equals("M")){ boys.add(grade); }else girls.add(grade); } Collections.sort(boys); Collections.sort(girls); int max=0; int min = 0; if(girls.isEmpty()){ System.out.println("Absent"); }else{ stu girl=map.get(girls.get(girls.size()-1)); System.out.println(girl.name+" "+girl.id); max=girl.grade; } if(boys.isEmpty()){ System.out.println("Absent"); }else{ stu boy=map.get(boys.get(0)); System.out.println(boy.name+" "+boy.id); min=boy.grade; } if(boys.isEmpty()||girls.isEmpty()){ System.out.println("NA"); }else{ System.out.println(max-min); } } }
#include<iostream> using namespace std; int main() { string id,name,gender,maxname,maxid,minname,minid; int grade,diff,N,maxg=0,ming=100; bool na=false,nof=true,nom=true; cin>>N; while(N--) { cin>>name>>gender>>id>>grade; if(maxg<=grade&&gender=="F") { maxg=grade; maxname=name; maxid=id; nof=false; } if(ming>=grade&&gender=="M") { ming=grade; minname=name; minid=id; nom=false; } } na=nof||nom; if(nof)cout<<"Absent"<<endl; else cout<<maxname<<" "<<maxid<<endl; if(nom)cout<<"Absent"<<endl; else cout<<minname<<" "<<minid<<endl; if(na)cout<<"NA"<<endl; else cout<<maxg-ming<<endl; return 0; }
#include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; struct Student{ int grade; char gender; string name, id; Student() {} void read(){ cin >> name >> gender >> id >> grade; } }; bool compare(Student s1, Student s2){ return s1.grade < s2.grade ; } int main(){ int N; int highest, lowest; vector<Student> mv; vector<Student> fv; cin >> N; while(N--){ Student s; s.read(); if(s.gender == 'M') mv.push_back(s); else if(s.gender == 'F') fv.push_back(s); } int fv_size = fv.size(); int mv_size = mv.size(); if(fv_size){ sort(fv.begin(), fv.end(), compare); auto iter = fv.crbegin(); highest = iter -> grade; cout << iter->name << " " << iter->id << endl; } else cout << "Absent" << endl; if(mv_size){ sort(mv.begin(), mv.end(), compare); auto iter = mv.begin(); lowest = iter -> grade; cout << iter->name << " " << iter->id << endl; } else cout << "Absent" << endl; if(fv_size && mv_size) cout << highest - lowest <<endl; else cout << "NA" << endl; return 0; }
自己做的,有点笨拙🤔 #include<bits/stdc++.h> using namespace std; const int Max=1010; struct T { string name; char gender; string id; int grade; } boy[Max],girl[Max]; bool cmp1(T a,T b) { return a.grade<b.grade; } bool cmp2(T a,T b) { return a.grade>b.grade; } int main() { int n; cin>>n; int index1=0,index2=0; for(int i=0; i<n; i++) { string name,id; char gender; int grade; cin>>name>>gender>>id>>grade; if(gender=='M') { boy[index1].name=name; boy[index1].gender=gender; boy[index1].id=id; boy[index1].grade=grade; index1++; } if(gender=='F') { girl[index2].name=name; girl[index2].gender=gender; girl[index2].id=id; girl[index2].grade=grade; index2++; } } sort(boy,boy+index1,cmp1); sort(girl,girl+index2,cmp2); if(index2==0) cout<<"Absent"<<endl; else cout<<girl[0].name<<" "<<girl[0].id<<endl; if(index1==0) cout<<"Absent"<<endl; else cout<<boy[0].name<<" "<<boy[0].id<<endl; if(index1==0||index2==0) cout<<"NA"<<endl; else cout<<girl[0].grade-boy[0].grade<<endl; return 0; }
package PAT; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Boy_vs_Girls { static class Node{ String name,gender,id; int s; public Node(String name,String gender,String id,int s) { this.name=name; this.gender=gender; this.id=id; this.s=s; } } //lowest:boy hightest:girl public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); ArrayList<Node> m=new ArrayList<>(); ArrayList<Node> f=new ArrayList<>(); for(int i=0;i<n;i++) { String[] te=sc.nextLine().split(" "); if(te[1].equals("M")) { m.add(new Node(te[0],te[1],te[2],Integer.parseInt(te[3]))); } else f.add(new Node(te[0],te[1],te[2],Integer.parseInt(te[3]))); } Comparator<Node> com=new Comparator<Node>() { public int compare(Node a,Node b) { return a.s-b.s; } }; Comparator<Node> cmp1=new Comparator<Node>() { public int compare(Node a,Node b) { return b.s-a.s; } }; Collections.sort(m,com); Collections.sort(f,cmp1); if(f.size()==0) System.out.println("Absent"); else System.out.println(f.get(0).name+" "+f.get(0).id); if(m.size()==0) System.out.println("Absent"); else System.out.println(m.get(0).name+" "+m.get(0).id); if(f.size()==0||m.size()==0) System.out.println("NA"); else System.out.println(f.get(0).s-m.get(0).s); } }
n = input() n = int(n) male,female = [],[] for i in range(0,n): tem = list(map(str,input().split())) tem[3] = int(tem[3]) if tem[1]=='M': male.append(tem) else: female.append(tem) female.sort(key=lambda x:(-x[3])) male.sort(key=lambda x:(x[3])) if len(female)!=0 and len(male)!=0: defer = int(female[0][3])-int(male[0][3]) else: defer = 'NA' if len(male)==0: outman = 'Absent' else: del male[0][1] male[0].pop() outman = ' '.join(male[0]) if len(female)==0: outwoman = 'Absent' else: del female[0][1] female[0].pop() outwoman = ' '.join(female[0]) print(outwoman) print(outman) print(defer)
#include <iostream> using namespace std; struct Student{ string name; char gender; string ID; int grade; }; int main() { ios::sync_with_stdio(false); int N; Student maxF{"",'\0',"",-1}; Student minM{"",'\0',"",101}; Student s; cin >> N; for(int i=0;i<N;i++){ cin >>s.name >> s.gender >> s.ID >> s.grade; if(s.gender == 'F'){ if(s.grade > maxF.grade) maxF = s; } else{ if(s.grade < minM.grade) minM = s; } } //output bool NA = false; if(maxF.gender=='F') cout << maxF.name << " " <<maxF.ID << "\n"; else{ cout << "Absent" << "\n"; NA = true; } if(minM.gender=='M') cout << minM.name << " " << minM.ID << "\n"; else{ cout << "Absent" << "\n"; NA = true; } if(NA) cout << "NA"; else cout << maxF.grade - minM.grade; return 0; }
// 思路:输入然后遍历记录下标,下标赋初值。 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; // The first line gives the name and ID of the female student with // the highest grade, and the second line gives that of the male // student with the lowest grade. The third line gives the difference // gradeF-gradeM. If one such kind of student is missing, output // "Absent" in the corresponding line, and output // "NA" in the third line instead. typedef struct { string name; char gender; string ID; int grade; }student; int main() { int N; cin >> N; string cinName; char cinGender; string cinID; int cinGrade; student *p = new student[N]; vector<student> v; for(int i=0; i<N; i++) { cin >> cinName >> cinGender >> cinID >> cinGrade; p[i].name = cinName; p[i].gender = cinGender; p[i].ID = cinID; p[i].grade = cinGrade; v.push_back(p[i]); } vector<student>::iterator it; int max = 0; int min = 100; int maxIndex = -1; int minIndex = -1; int i = 0; for(it=v.begin(); it!=v.end(); it++,i++) { if((*it).gender == 'F') { if((*it).grade >= max) { max = (*it).grade; maxIndex = i; } } else { if((*it).grade <= min) { min = (*it).grade; minIndex = i; } } } if(minIndex != -1 && maxIndex != -1) { cout << v[maxIndex].name << " " << v[maxIndex].ID << endl; cout << v[minIndex].name << " " << v[minIndex].ID << endl; cout << v[maxIndex].grade - v[minIndex].grade << endl; } else if(minIndex != -1 && maxIndex == -1) { cout << "Absent" << endl; cout << v[minIndex].name << " " << v[minIndex].ID << endl; cout << "NA" << endl; } else if(minIndex == -1 && maxIndex != -1) { cout << v[maxIndex].name << " " << v[maxIndex].ID << endl; cout << "Absent" << endl; cout << "NA" << endl; } else { cout << "Absent" << endl; cout << "Absent" << endl; cout << "NA" << endl; } }
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct Student{ string name; string gender; string id; int grade; }; int cmp(Student a, Student b){ return a.grade>b.grade; } int main(){ int n; cin>>n; vector<Student> girls, boys; for(int i=0; i<n; ++i){ Student st; cin>>st.name>>st.gender>>st.id>>st.grade; if(st.gender=="F") girls.push_back(st); else boys.push_back(st); } int n1=girls.size(), n2=boys.size(); sort(girls.begin(), girls.end(), cmp); sort(boys.begin(), boys.end(), cmp); if(n1>0) cout<<girls[0].name<<" "<<girls[0].id<<endl; else cout<<"Absent"<<endl; if(n2>0) cout<<boys[n2-1].name<<" "<<boys[n2-1].id<<endl; else cout<<"Absent"<<endl; if(n1>0&&n2>0) cout<<girls[0].grade-boys[n2-1].grade<<endl; else cout<<"NA"<<endl; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int num = in.nextInt(); in.nextLine(); String[][] str = new String[num][4]; if(num==0){ System.out.println("Absent"); System.out.println("Absent"); System.out.print("NA"); } else{ int Mmin = -1; int Fmax = -1; for(int i=0;i<num;i++){ str[i] = in.nextLine().split(" "); if(str[i][1].equals("M")){ if(Mmin==-1){ Mmin=i; }else{ if(Integer.parseInt(str[i][3])<Integer.parseInt(str[Mmin][3])){ Mmin=i; } } }else if(str[i][1].equals("F")){ if(Fmax==-1){ Fmax=i; }else{ if(Integer.parseInt(str[i][3])>Integer.parseInt(str[Fmax][3])){ Fmax=i; } } } } if(Mmin!=-1&&Fmax!=-1){ System.out.println(str[Fmax][0]+" "+str[Fmax][2]); System.out.println(str[Mmin][0]+" "+str[Mmin][2]); System.out.print(Integer.parseInt(str[Fmax][3])-Integer.parseInt(str[Mmin][3])); }else if(Mmin==-1){ System.out.println(str[Fmax][0]+" "+str[Fmax][2]); System.out.println("Absent"); System.out.print("NA"); }else{ System.out.println("Absent"); System.out.println(str[Mmin][0]+" "+str[Mmin][2]); System.out.print("NA"); } } } }
#include <iostream> #include <vector> #include <string> #include <map> #include<algorithm> using namespace std; typedef struct { string name; char gender; string ID; int grade; }node; bool cmpmale(const node&x, const node& y) { return x.grade < y.grade; } bool cmpfemale(const node &x, const node &y) { return x.grade > y.grade; } int main() { int length; while (cin >> length) { vector<node> female, male; for (int i = 0; i < length; i++) { node p; cin >> p.name >> p.gender >> p.ID >> p.grade; if (p.gender == 'F') female.push_back(p); else male.push_back(p); } sort(male.begin(), male.end(), cmpmale); sort(female.begin(), female.end(), cmpfemale); if (female.size() == 0) cout << "Absent" << endl; else cout << female[0].name << " " << female[0].ID << endl; if (male.size() == 0) cout << "Absent" << endl; else cout << male[0].name << " " << male[0].ID << endl; if (male.size() == 0 || female.size() == 0) cout << "NA" << endl; else cout << female[0].grade - male[0].grade << endl; } return 0; }
import java.util.*;public class Main { public static void main(String[] args){ Scanner in=new Scanner(System.in); int n=in.nextInt(); List<student>Male=new ArrayList<student>(); List<student>Female=new ArrayList<student>(); for(int i=0;i<n;i++){ student stu=new student(in.next(),in.next(),in.next(),in.nextInt()); if (stu.sex.equals("M")) Male.add(stu); else Female.add(stu); } Collections.sort(Male); Collections.sort(Female); if(Female.size()==0) System.out.println("Absent"); else System.out.println(Female.get(Female.size()-1).toString()); if(Male.size()==0) System.out.println("Absent"); else System.out.println(Male.get(0).toString()); if(Female.size()==0 ||Male.size()==0 ) System.out.println("NA"); else System.out.println(Female.get(Female.size()-1).score-Male.get(0).score); } } class student implements Comparable<student>{ String name; String id; String sex; int score; public student(){} public student(String name ,String sex,String id,int score){ this.id=id; this.name=name; this.score=score; this.sex=sex; } @Override public int compareTo(student s1){ return this.score-s1.score; } @Override public String toString(){ return this.name+" "+this.id; } }
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; struct STU { string name; char sex; string id; int score; }stu[200],boys[200],girls[200]; bool cmp1(STU a,STU b) { return a.score>b.score; } bool cmp2(STU a,STU b) { return a.score<b.score; } int main() { int bn=0,gn=0; int n; cin>>n; for(int i=0;i<n;i++){ cin>>stu[i].name>>stu[i].sex>>stu[i].id>>stu[i].score; if(stu[i].sex=='M') { boys[bn++]=stu[i]; } if(stu[i].sex=='F') { girls[gn++]=stu[i]; } } if(gn!=0) { sort(girls,girls+gn,cmp1); cout<<girls[0].name<<" "<<girls[0].id<<endl; } else { cout<<"Absent"<<endl; } if(bn!=0) {sort(boys,boys+bn,cmp2); cout<<boys[0].name<<" "<<boys[0].id<<endl; } else { cout<<"Absent"<<endl; } if(bn==0 || gn==0) cout<<"NA"<<endl; else cout<<girls[0].score-boys[0].score<<endl; return 0; }string 比较方便。。
#include <iostream> #include <vector> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; struct student{ char name[10]; char gender; char ID[10]; int grade; }; vector<student> boysAndGirls; bool judge = false; void Insert(int i){ cin >> boysAndGirls[i].name; cin >> boysAndGirls[i].gender; cin >> boysAndGirls[i].ID; cin >> boysAndGirls[i].grade; } void Output(int i){ cout << boysAndGirls[i].name << ' '; cout << boysAndGirls[i].ID << endl; } int main(int argc, char** argv) { int N; cin >> N; boysAndGirls.resize(N); for(int i = 0;i < N;i++){ Insert(i); } int LowestBoy = -1; int HighestGirl = -1; for(int i = 0;i < N;i++){ if(boysAndGirls[i].gender == 'M'){ if(LowestBoy == -1){ LowestBoy = i; } else{ if(boysAndGirls[i].grade < boysAndGirls[LowestBoy].grade){ LowestBoy = i; } } } if(boysAndGirls[i].gender == 'F'){ if(HighestGirl == -1){ HighestGirl = i; } else{ if(boysAndGirls[i].grade > boysAndGirls[HighestGirl].grade){ HighestGirl= i; } } } } if(HighestGirl != -1){ Output(HighestGirl); } else{ judge = true; cout << "Absent" << endl; } if(LowestBoy != -1){ Output(LowestBoy); } else{ judge = true; cout << "Absent" << endl; } if(judge){ cout << "NA" << endl; } else{ cout << boysAndGirls[HighestGirl].grade - boysAndGirls[LowestBoy].grade << endl; } return 0; }
lowestMale = [] highestFemale = [] N = input() for i in range(N): student = raw_input().strip().split() if student[1] == 'M': if not lowestMale or lowestMale[2] > int(student[3]): lowestMale = [student[0], student[2], int(student[3])] else: if not highestFemale or highestFemale[2] < int(student[3]): highestFemale = [student[0], student[2], int(student[3])] if highestFemale: print(' '.join(highestFemale[:2])) else: print('Absent') if lowestMale: print(' '.join(lowestMale[:2])) else: print('Absent') if highestFemale and lowestMale: print(highestFemale[2]-lowestMale[2]) else: print('NA')
#include <iostream> #include <string> using namespace std; int main(void) { int minG = 101, maxG = -1; int n, grade; cin >> n; char gender; string name, ID; pair<string, string> prMax(" ", " "), prMin(" ", " "); while (n--) { cin >> name >> gender >> ID >> grade; if (gender == 'F') { if (maxG < grade) { maxG = grade; prMax.first = name; prMax.second = ID; } } else { if (minG > grade) { minG = grade; prMin.first = name; prMin.second = ID; } } } bool exm = false, exf = false; if (prMax.first != " ") { exf = true; } if (prMin.first != " ") { exm = true; } if (exm && exf) { cout << prMax.first << " " << prMax.second << endl; cout << prMin.first << " " << prMin.second << endl; cout << maxG - minG << endl; } else if (!exm && exf) { cout << prMax.first << " " << prMax.second << endl; cout << "Absent" << endl; cout << "NA" << endl; } else if (exm && !exf) { cout << "Absent" << endl; cout << prMin.first << " " << prMin.second << endl; cout << "NA" << endl; } else { cout << "Absent" << endl; cout << "Absent" << endl; cout << "NA" << endl; } return 0; }