題目是先輸入學生人數n,再輸入排序模式flag(0代表降序,1代表升序),接著輸入n個學生的姓名和成績。然后,按照排序模式排序并輸出,要求穩定的排序演算法。
問題:我的代碼在本地編譯器上可以正常編譯運行,但在牛客網clang++3.3編譯后總是不能通過測驗用例(下面附測驗用例),只能輸出排序后的前11個資料,那后面的資料跑哪兒去了?
看到別人的插入演算法,時間復雜度跟我的沒差,但是可以正常通過,求解答,謝謝!
我的代碼:
#include <iostream>
#include<string>
using namespace std;
struct Student{
public:
string name;
int score;
};
void changestu(Student &a,Student &b){
Student tmp=a;
a=b;
b=tmp;
}
int main()
{
Student stu[1000];
int n=0;
int flag=0;
while(cin>>n>>flag){
for(int i=0;i<n;i++)
cin>>stu[i].name>>stu[i].score;
if(flag==0){
for(int p=0;p<n;p++){
for(int q=p+1;q<n;q++){
if(stu[p].score<stu[q].score){
changestu(stu[p],stu[q]);
}
}
}
}else{
for(int p=0;p<n;p++){
for(int q=p+1;q<n;q++){
if(stu[p].score>stu[q].score){
changestu(stu[p],stu[q]);
}
}
}
}
for(int r=0;r<n;r++)
cout<<stu[r].name<<' '<<stu[r].score<<endl;
}
}
測驗用例:
28
1
qhsq 15
ozslg 79
ncttmtsphb 71
a 39
eeiuyzsj 34
nmlrokx 21
pjizylo 90
ec 45
f 12
sh 31
fm 25
ptprphubqk 29
wxdiwv 0
uhlcpjtxad 60
w 20
zwktbpun 70
efzfkf 69
v 31
rsnrgtl 73
lhdo 76
wt 56
mcdwd 14
ydrnoyd 37
gmlfds 76
zx 1
dqx 98
gz 90
kvbzrwrrjj 13
牛客網上我的測驗用例運行結果:
wxdiwv 0
zx 1
f 12
kvbzrwrrjj 13
mcdwd 14
qhsq 15
w 20
nmlrokx 21
fm 25
ptprphubqk 29
v 31
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67648.html
標籤:基礎類
上一篇:C++builder6與新版OPENCV能不能一起用
下一篇:C++拓撲排序
