A - 前m大的数
这个题思路很简单,但是写代码的时候还是有很多东西不太确定,比如数组定义多大,能不能定义那么大的数组,后来听说十的八次方后想了想应该可以;数列排序我刚想到的就是冒泡排序吧,然后想了想时间复杂度估计跑不起来,学长刚好讲了sort函数,课上没听太懂,课下自己百度了一下基本上懂了,通过这道题也对sort函数更熟悉了。
#include
#include
using namespace std;
int a[3010];
int sum[5000000];
int main()
{
int n, m, b ;
while (~scanf("%d %d", &n, &m)) {
b = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
sum[b] = a[i] + a[j];
b++;
}
}
sort(sum, sum + b);//默认从小到大排序
for (int i = b-1; i > b - m; i--) {
cout << sum[i]<<' ';
}
cout<<sum[b-m] << '\n';//细节,最后一个输出无空格
}
return 0;
}B - 稳定排序
这是我第一次写与结构体有关的题,手比较生,在论坛上看了这个同学的代码,感觉他的思路很清晰,让我把这道题看的很透彻。https://blog.csdn.net/weixin_47701814/article/details/106163326
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct stu {
char name[50];
int fenshu;
int bh;
}a[300], b[300];//第一次写的时候把s函数写在了结构体声明的前面,费了些时间才搞明白
int s(stu a, stu b) {
if (a.fenshu != b.fenshu)
return a.fenshu > b.fenshu;
else
return a.bh < b.bh;
}//规定排序,优先级分数在前,标号在后;
int main()
{
int n;
while (~scanf("%d", &n)) {
int c = 0, d = 0;
for (int i = 0; i < n; i++) {
cin >> a[i].name >> a[i].fenshu;
a[i].bh = i;
}
sort(a, a + n, s);
for (int i = 0; i < n; i++) {
cin >> b[i].name >> b[i].fenshu;
b[i].bh = i;
}
for (int i = 0; i < n; i++) {
if (a[i].fenshu != b[i].fenshu)
c = 1;
}
if (!c) {
for (int i = 0; i < n; i++) {
if ((strcmp(a[i].name, b[i].name)) != 0){
d = 1;
break;
}//这个strcmp感觉用的很巧妙
}
}
if (c == 0 && d == 1) {
cout << "Not Stable" << '\n';
for (int i = 0; i < n; i++) {
cout << a[i].name << ' ' << a[i].fenshu << '\n';
}
}else if(c) {
cout << "Error" << '\n';
for (int i = 0; i < n; i++) {
cout << a[i].name << ' ' << a[i].fenshu << '\n';
}
}else
cout << "Right"<<'\n';
}
return 0;
}
C - 开门人和关门人
这个题目的主要问题就是对时间的处理,钉钉上讲的那种方法比较简洁,另一种方法就是struct里面定义6个数字,挨个比较,过程相对繁琐。
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct jl {
string hm,jr,chu;
}a[100];
int jinru(jl a, jl b)
{
return a.jr < b.jr;
}
int chuqu(jl a, jl b)
{
return a.chu > b.chu;
}
int main()
{
int n,m;
cin >> n;
while (n--) {
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a[i].hm >> a[i].jr >> a[i].chu;
}
sort(a, a + m, jinru);
cout << a[0].hm<<" ";
sort(a, a + m, chuqu);
cout << a[0].hm << "\n";
}
return 0;
}
*D - EXCEL排序 *
做过上面两道题,这道题做的时候感觉很顺,但提交就是不过,后来才发现审错题了,题目阐述C1,C2,C3的时候没太注意标点符号导致的。C3后面的那句话我当成是只限制C3的了但实际上是限制了所有的C。这也是以后做题应该引起特别关心的一个方面。
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct stu {
string name,xh;
int cj;
}a[100010];
int c1(stu a, stu b){
return a.xh < b.xh;
}
int c2(stu a, stu b) {
if (a.name != b.name)
return a.name < b.name;
else
return a.xh < b.xh;
}
int c3(stu a, stu b) {
if (a.cj == b.cj)
return a.xh < b.xh;
else
return a.cj < b.cj;
}
int main()
{
int n, c,m=1;
while (1) {
cin >> n >> c;
if (n == 0 && c == 0)
break;
for (int i = 0; i < n; i++) {
cin >> a[i].xh >> a[i].name >> a[i].cj;
}
if (c == 1)
sort(a, a + n, c1);
if (c == 2)
sort(a, a + n, c2);
if(c==3)
sort(a, a + n, c3);
cout << "Case " << m << ':'<<'\n';
m++;
for (int i = 0; i < n; i++) {
cout << a[i].xh << " " << a[i].name << " " << a[i].cj << "\n";
}
}
return 0;
}**总结**:这四道都是跟排序相关的题目,让我真真正正地掌握了sort的用法。题可能不难,但这四道我还是做了一下午,万事开头难,以后应该会越来越好吧。Ps:后面的题我会尽快跟进

京公网安备 11010502036488号