考研之路
题目描述
大三的 Rabbit 已经开始考研了,她需要参加数学、英语、政治、专业课四门考试,四门课的满分分别是 150,100,100,150。
不过她了解到考研与普通考试不同,要想被录取不仅要总分到达国家线(320分),而且单科成绩也必须到达单科线。
这里每门课的单科线为这门课满分的 60%。
过了几个月,考研成绩出来了,Rabbit 得到了班上所有 N 位同学的成绩,现在她想知道哪些同学能被录取,并根据她们的总分从大到小排序(若总分相同,则按照名字的字典序从小到大排序)。
注:到达指的是大于等于,数据保证学生名字是只由小写字母和大写字母组成的不同字符串,且至少有一位同学能被录取。
输入
输入数据第一行为 T,表示数据组数。(1<=T<=20)
每组数据第一行为 N,表示学生人数。(1<=N<=100)
接下来 N 行,每行首先是一个字符串 S,表示第 i 个学生的名字,接下来四个整数 M,E,P,Z,分别表示该学生的数学成绩,英语成绩,政治成绩,专业课成绩。(1<=|S|<=10,1<=E,P<=100,1<=M,Z<=150)
输出
对于每组数据输出若干行,每行输出被录取的学生按照成绩排序后的名字和总分,用空格隔开。
样例输入 Copy
1
3
Bob 105 70 65 110
John 135 55 70 120
Tom 100 75 70 120
样例输出 Copy
Tom 365
Bob 350
这是我的实现代码,用冒泡排序比较的,代码写的有点长,还有很多可以优化的地方
import java.util.*;
public class Main{
public static void main(String[]args) {
Scanner scan =new Scanner (System.in) ;
int T=scan.nextInt();
while(T-->0){
int n=scan.nextInt();
String[] name=new String[n];
int[][] score=new int[n][4];
boolean flag=false;
int[] sum=new int[n];
int luqu=0;
for(int i=0;i<n;i++) {
name[i]=scan.next();
String s=name[i];
for(int a=0;a<s.length();a++) {
if(Character.isLetter(s.charAt(a))) {
continue;
}
}
score[i][0]=scan.nextInt();
score[i][1]=scan.nextInt();
score[i][2]=scan.nextInt();
score[i][3]=scan.nextInt();
}
for(int i=0;i<n;i++) {
sum[i]=score[i][0]+score[i][1]+score[i][2]+score[i][3];
if(sum[i]>=320&&score[i][0]>=90&&score[i][0]<=150&&score[i][1]>=60&&score[i][1]<=100&&score[i][2]>=60&&score[i][2]<=100&&score[i][3]>=90&&score[i][3]<=150) {
flag=true;
luqu++;
}
else
continue;
}
flag=false;
int[] luqusum=new int[luqu];
String[] luquname=new String[luqu];
for(int i=0,j=0;i<n;i++) {
flag=false;
if(sum[i]>=320&&score[i][0]>=90&&score[i][0]<=150&&score[i][1]>=60&&score[i][1]<=100&&score[i][2]>=60&&score[i][2]<=100&&score[i][3]>=90&&score[i][3]<=150) {
flag=true;
}
if(flag==true) {
luquname[j]=name[i];
luqusum[j]=sum[i];
j++;}
}
for(int p=0;p<luqu-1;p++) {
for(int q=0;q<luqu-1-p;q++) {
if(luqusum[q]<luqusum[q+1]) {
int temp=luqusum[q];
luqusum[q]=luqusum[q+1];
luqusum[q+1]=temp;
String temps=luquname[q];
luquname[q]=luquname[q+1];
luquname[q+1]=temps;
}
}
}
for(int t=0;t<luqu-1;t++) {
for(int r=t+1;r<luqu;r++) {
if(luqusum[t]==luqusum[r]&&luquname[t].compareTo(luquname[r])>0) {
int temp=luqusum[r];
luqusum[r]=luqusum[t];
luqusum[t]=temp;
String temps=luquname[r];
luquname[r]=luquname[t];
luquname[t]=temps;
break;
}
}
}
for(int x=0;x<luqu;x++) {
if(luqusum[x]!=0)
System.out.println(luquname[x]+" "+luqusum[x]);
}
}
}
}
c++
#include <iostream>
#include <algorithm>
using namespace std;
struct student{
string name;
int m,e,p,z;
int sum;
}a[25];
bool cmp(student a,student b){
if (a.sum > b.sum) return 1;
else if (a.sum == b.sum){
if (a.name < b.name)
return 1;
else return 0;
}
else return 0;
}
int main(){
int T;
int n;
cin >> T;
while (T--){
cin >> n;
for (int i = 0;i < n;i ++){
cin >> a[i].name >> a[i].m >> a[i].e >> a[i].p >> a[i].z;
a[i].sum = a[i].m + a[i].e + a[i].p + a[i].z;
}
sort(a,a + n,cmp);
//cout << a[0].name << " " << a[0].sum << endl;
for (int i = 0;i < n;i ++){
if (a[i].sum >= 320 && a[i].m >= 90 && a[i].e >= 60 && a[i].p >= 60 && a[i].z >= 90)
cout << a[i].name << " " << a[i].sum << endl;
}
}
return 0;
}
选择排序实现
import java.util.*
public class Main {
public static void main(String[] args) {
Scanner i=new Scanner(System.in);
int n=i.nextInt();
for(int o=0;o<n;o++){
int t=i.nextInt();
int [] m=new int[t];
String [] w=new String[t];
for(int l=0;l<t;l++){
String s=i.next();
int a=i.nextInt(),b=i.nextInt(),c=i.nextInt(),d=i.nextInt();
if((a+b+c+d)>=320&&a>=90&&b>=60&&c>=60&&d>=90){
w[l]=s;
m[l]=a+b+c+d;
}
else
w[l]="";
}
for(int q=0;q<t;q++){
int x=0;
String v="";
for(int p=q+1;p<t;p++){
if(m[p]>m[q]){
v=w[p];
w[p]=w[q];
w[q]=v;
x=m[q];
m[q]=m[p];
m[p]=x;
}
};
}
for(int q=0;q<t;q++){
int x=0;
String v="";
for(int p=q+1;p<t;p++){
if(m[p]==m[q]&&w[q].compareTo(w[p])>0){
v=w[p];
w[p]=w[q];
w[q]=v;
x=m[q];
m[q]=m[p];
m[p]=x;
}
};
}
for(int p=0;p<t;p++)
if(m[p]!=0)
System.out.println(w[p]+" "+m[p]);
}
}
}
849

被折叠的 条评论
为什么被折叠?



