1557 - 成绩排序2
从键盘输入4 个学生的数据(包括姓名、年龄和成绩),按成绩从高到底排序,并输出其中成绩次高者的所有数据。
Input
输入4行,每一行表示一个学生的数据,分别表示 姓名、年龄和成绩。
Output
输出4行,表示排序后的结果,格式参见样例。
Examples
Input
aa 10 90 bb 20 100 cc 18 98 dd 19 93
Output
bb 20 100 cc 18 98 dd 19 93 aa 10 90
Solution 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; }
Solution 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; }