【我的漫漫考研路】有生之年·调完了BUG–冒泡排序C++版本-简书(漫漫考研路下一句)



正文之前

今天去牛客网试了试一些实战编程题,感觉贼有意思,但是也很难,挑了个成绩排序的算法题我就开始怼!

对我一个编程经验并不是很丰富的人来说,确实算是个挑战了。

所以我满满当当的搞了四个小时多,才算是把牛客的这个题目给解答了。而且还是残缺版本,因为我没用指针,所以估计时间或者是内存抄了。最后牛客给我的回答是我的不合格 ~~~~心痛!! (╯‵□′)╯︵┻━┻

正文

以后我的正文尽力简洁(好吧,我承认是我懒得打字了。所以就直接发代码吖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

输入描述:

输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开

输出描述:

按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

示例1

输入、输出

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];

【我的漫漫考研路】有生之年·调完了BUG–冒泡排序C++版本-简书(漫漫考研路下一句)插图
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");

}

运行结果:

正文之后

“没错,你已经看完了。”

“啊哈?没有代码注释?那你写个啥?”

“啊?代码注释?那是个啥?我赶时间,自行领悟呀,我赶时间!

此中有真意~~~欲辨已忘言····”


忍不住还是秀一下我的工作界面,爽歪歪~~~

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

|京ICP备18012533号-328