#include<iostream> #include<string> using namespace std; const int num=20; int main() { int i,j,t; string temp; string name[num]={"zhao","qian","sun","li","zhou","wu","zheng","wang","feng","chen","chu","wei","jiang","shen","han","yang","zhu","qin","yun","xu"}; double score[num]={79,84,65,89,60,92,78,56,62,55,54,16,48,99,82,100,35,85,75,20}; for(j=0;j<num-1;j++) for(i=0;i<num-j-1;i++) { if(score[i]<score[i+1]) { t=score[i]; score[i]=score[i+1]; score[i+1]=t; temp=name[i]; name[i]=name[i+1]; name[i+1]=temp; } } cout<<"按成绩排序后的结果是:"<<endl; for(i=0;i<num;i++) cout<<name[i]<<"\t"<<score[i]<<endl; cout<<endl; for(j=0;j<num-1;j++) for(i=0;i<num-j-1;i++) { if(name[i]<name[i+1]) { temp=name[i]; name[i]=name[i+1]; name[i+1]=temp; t=score[i]; score[i]=score[i+1]; score[i+1]=t; } } cout<<"按同学姓名排序后有结果是:"<<endl; for(i=0;i<num;i++) cout<<name[i]<<"\t"<<score[i]<<endl; return 0; }
运行结果: