今天去牛客网试了试一些实战编程题,感觉贼有意思,但是也很难,挑了个成绩排序的算法题我就开始怼! 对我一个编程经验并不是很丰富的人来说,确实算是个挑战了。 所以我满满当当的搞了四个小时多,才算是把牛客的这个题目给解答了。而且还是残缺版本,因为我没用指针,所以估计时间或者是内存抄了。最后牛客给我的回答是我的不合格 ~~~~心痛!! (╯‵□′)╯︵┻━┻
以后我的正文尽力简洁(好吧,我承认是我懒得打字了。所以就直接发代码吖a~~~~)
jack 70
peter 96
Tom 70
smith 67
peter 96
jack 70
Tom 70
smith 67
smith 67
Tom 70
jack 70
peter 96
3
0
fang 90
yang 50
ning 70
fang 90
ning 70
yang 50
代码:
#include <iostream>
#include <cstring>
using namespace std;
void sort(string a[], int b[], int n, int rule);
int main()
{
int number,rule,i;
string a[100];
int b[100];
cout<<"input the number you want to sort and the rules:(0 down,1 up)"<<"\n";
cin>>number>>rule;
cout<<"\nplease input the (name score) array: \n";
for(i=0;i<number;++i)
{
cin>>a[i]>>b[i];
cout<<"\n";
}
cout<<"Begin~"<<"\n";
sort(a,b, number, rule);
return 0;
}
void sort(string a[], int b[], int n, int rule)
{
string SortedArrayA[100];
for (int i = 0; i < n; ++i)
{
SortedArrayA[i]=a[i];
}
int SortedArrayB[100];
for (int i = 0; i < n; ++i)
{
SortedArrayB[i]=b[i];
}
string name;
int score;
int j;
int i;
for(j=n; j > 0; --j)
{
for(i=0;i<j-1;++i)
{
if(SortedArrayB[i]>SortedArrayB[i+1])
{
name=SortedArrayA[i];
score=SortedArrayB[i];
SortedArrayA[i]=SortedArrayA[i+1];
SortedArrayB[i]=SortedArrayB[i+1];
SortedArrayB[i+1]=score;
SortedArrayA[i+1]=name;
}
}
}
switch(rule)
{
case 0:
for (int i = n-1; i >=0; --i)
{
cout<<SortedArrayA[i]<<" "<<SortedArrayB[i]<<"\n";
cout<<"\n";
};
break;
case 1:
for (int k = 0; k < n; ++k)
{
cout<<SortedArrayA[k]<<" "<<SortedArrayB[k]<<"\n";
cout<<"\n";
};
break;
default:
cout<<"sorry";
break;
}
cout<<"done"<<endl;
system("pause");
}
运行结果:
“没错,你已经看完了。” “啊哈?没有代码注释?那你写个啥?” “啊?代码注释?那是个啥?我赶时间,自行领悟呀,我赶时间! 此中有真意~~~欲辨已忘言····”
忍不住还是秀一下我的工作界面,爽歪歪~~~