1557 - 成绩排序2
时间限制 : 1 秒
内存限制 : 32 MB
从键盘输入4 个学生的数据(包括姓名、年龄和成绩),按成绩从高到底排序,并输出其中成绩次高者的所有数据。
题目输入
输入4行,每一行表示一个学生的数据,分别表示 姓名、年龄和成绩。
题目输出
输出4行,表示排序后的结果,格式参见样例。
输入/输出样例
输入格式
aa 10 90 bb 20 100 cc 18 98 dd 19 93
输出格式
bb 20 100 cc 18 98 dd 19 93 aa 10 90
C语言解答
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char c[5][20]={'0'}; int a[5][2]; char temp[20]; int temp1; for(int i=0;i<4;i++) { scanf("%s",&c[i]); scanf("%d%d",&a[i][0],&a[i][1]); } for(int i=0;i<3;i++) for(int j=1;j<4-i;j++) if(a[j-1][1]<a[j][1]) { strcpy(temp,c[j-1]); strcpy(c[j-1],c[j]); strcpy(c[j],temp); temp1=a[j-1][0]; a[j-1][0]=a[j][0]; a[j][0]=temp1; temp1=a[j-1][1]; a[j-1][1]=a[j][1]; a[j][1]=temp1; } for(int i=0;i<4;i++) { printf("%s ",c[i]); printf("%d %d\n",a[i][0],a[i][1]); } //system("pause"); return 0; }
C++解答
#include<iostream> #include<fstream> #include<string> using namespace std; struct student { string name; int age; int score; }; int main() { student st[4]; student s; student t; int i=0; for(i=0; i<4; i++) { cin>>st[i].name>>st[i].age>>st[i].score; } for(i=0; i<4; i++) for(int j=i+1; j<4; j++) if(st[i].score<st[j].score) { t=st[i]; st[i]=st[j]; st[j]=t; } for(i=0; i<4; i++) cout<<st[i].name<<" "<<st[i].age<<" "<<st[i].score<<endl; return 0; }
Java解答
import java.text.DecimalFormat; import java.util.Arrays; import java.util.Scanner; public class Main { private static Scanner s = new Scanner(System.in) ; private static DecimalFormat df = new DecimalFormat("0.0") ; public static void main(String[] args) { A a[] = new A[4] ; for (int i = 0; i < 4; i++) { String name = s.next() ; int age = s.nextInt() ; int score = s.nextInt() ; a[i] = new A(name, age, score) ; } Arrays.sort(a) ; for (int i = a.length-1; i>=0; i--) { System.out.println(a[i].name+" "+a[i].age+" "+a[i].score); } } } class A implements Comparable{ String name ; int age ; int score ; public A(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } @Override public int compareTo(Object o) { A a = (A) o ; if(a.score<this.score){ return 1 ; }else if(a.score==this.score){ return 0; }else return -1 ; } }