ACM大学刚考完大学英语4级考试,想知道全校第3名学生的成绩是多少?如果最高分有好多个学生,则相同成绩的学生都算第一名;同理,如果第二高分的有多个学生,都算第二名。当然这是简单题,请你快速编一个程序找到第3名的成绩。输入:输入有多组,每组有2行,第一行是学生人数N(1<=N<10000),第二行有N个整数,分别表示每个学生的成绩(0到1e9)。当输入的N为0的时候结束程序。输出:对于每组输入,输出只有一行,即第3名学生的成绩,如果找不到,则输出No such score !Sample input:1090 84 90 60 70 65 73 85 98 98590 90 89 90 900Sample output:85No such score !难度:for beginnerTime limit: 50ms Memory: 200K --試題來源:飛燕之家c/c++學習論壇 解決代碼: #include <iostream>#include <assert.h>#include <memory.h>using namespace std; //define some constantsconst int MAXNUM =10000;const int THENUM =3;const int UPSCORELIMIT =0;const int DOWNSCORELIMIT=100; //forward declaration //prototype of functions //check input's validation,return false show illegal or legalbool IsValidScore(int *arry_iScore,int iSize); //sort the score arrayvoid SortScoreArray(int *arry_iScore,int iSize); //exchange two number's valuevoid Swap(int *ix,int *iy); //tidy score arrayint TidyScoreArray(int *arry_iScore,int iSize); //get the place's score from sorted arrayint GetFinalScore(int *arry_iScore,int iSize); //--Main Function---//int main(){ int iStudentNum=0; cout<<endl<<"please input students' number:"; while( cin>>iStudentNum) { //check input's validation if(iStudentNum<1||iStudentNum>MAXNUM) { cout<<endl<<"ilegal input,procedure close!"<<endl; break; } //apply for memory int* arry_iStudScore=new int[iStudentNum]; assert(arry_iStudScore!=NULL); //initlization for(int i=0;i<iStudentNum;i++) arry_iStudScore[i]=-1; //read data from outer device cout<<endl<<"please input every student's score:"; for(int j=0;j<iStudentNum;j++) cin>>arry_iStudScore[j]; if(!IsValidScore(arry_iStudScore,iStudentNum)) { cout<<endl<<"scores inputed included illegal data,procedure close!"<<endl; break; } //sort the score array SortScoreArray(arry_iStudScore,iStudentNum); //tidy sorted array int iRealSize=TidyScoreArray(arry_iStudScore,iStudentNum); //get the place student's score int iFinalResult=GetFinalScore(arry_iStudScore,iRealSize); if(iFinalResult!=-1) cout<<iFinalResult<<endl; else cout<<endl<<"No such score !"<<endl; //free memory delete arry_iStudScore; cout<<endl<<"please input students' number:"; } return 0;} //check input's validation,return false show illegal or legalbool IsValidScore(int *arry_iScore,int iSize){ for(int i=0;i<iSize;i++) { if(arry_iScore[i]<UPSCORELIMIT ||arry_iScore[i]>DOWNSCORELIMIT) return false; } return true;} //sort the score array as boldsort methodvoid SortScoreArray(int *arry_iScore,int iSize){ for(int i=iSize-1;i>0;i--) { for(int j=0;j<=i-1;j++) if(arry_iScore[j]>arry_iScore[j+1]) Swap(&arry_iScore[j],&arry_iScore[j+1]); }} //exchange two number's valuevoid Swap(int *ix,int *iy){ int iTempt=*ix; *ix=*iy; *iy=iTempt;} //tidy score arrayint TidyScoreArray(int *arry_iScore,int iSize){ int i=0; //clear same score while(i<iSize) { while(i<iSize&&arry_iScore[i]!=arry_iScore[i+1]) { i++; } if(i>=iSize) break; else { arry_iScore[i]=-1; i++; } } int iPosition=0; int k=0; //rerow the arry while(arry_iScore[k]!=-1&&k<iSize) { k++; } if(k<iSize) { iPosition=k; while(k<iSize) { while(arry_iScore[k]==-1&&k<iSize) { k++; } if(k<iSize) { if(iPosition!=k) arry_iScore[iPosition]=arry_iScore[k]; arry_iScore[k]=-1; iPosition++; } } } return iPosition-1;} //get the place's score from sorted arrayint GetFinalScore(int *arry_iScore,int iSize){ if(THENUM>iSize) return -1; if(iSize-THENUM+1>=0) return arry_iScore[iSize-THENUM+1]; else return -1;}

评论