实训第一章

能调库的绝不手写
这章基本上就是在教你怎么写sort里的cmp函数的

练习题目:

EOJ 2844

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int arr[1001];
int main() {
char ch;
cin >> ch;
int temp;
while(cin >> temp)
arr[temp]++;
if(ch == 'A'){
for(int i = 1;i < 1001;i++) if(arr[i]) cout << i << " ";
}
else{
for(int i = 1001;i > 0;i--) if(arr[i]) cout << i << " ";
}
return 0;
}

EOJ 2849

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <algorithm>
using namespace std;
struct Data{
string stu;
int score;
};
bool cmp(const Data &a, const Data &b){
if(a.score != b.score)
return a.score > b.score;
return a.stu < b.stu;
}
int main(){
int n;
cin >> n;
Data arr[n];
for(int i = 0;i < n;i++)
cin >> arr[i].stu >> arr[i].score;
sort(arr, arr+n, cmp);
for(int i = 0;i < n;i++){
if(arr[i].score >= 60) cout << arr[i].stu << " " << arr[i].score << endl;
else break;
}
return 0;
}

EOJ 2975

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <algorithm>
using namespace std;
struct Data{
int cnt, num;
}arr[1001];
bool cmp(const Data &a, const Data &b){
if(a.num%10 != b.num%10)
return a.num%10 < b.num%10;
return a.num < b.num;
}
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
for(int i = 0;i < 1001;i++)
arr[i].cnt = 0, arr[i].num = i;
int N, temp;
cin >> N;
while(N--) cin >> temp, arr[temp].cnt++;
sort(arr, arr+1000, cmp);
cout << "case #" << step << ":\n";
for(int i = 0;i < 1001;i++)
if(arr[i].cnt) cout << arr[i].num << " ";
cout << endl;
}
return 0;
}

EOJ 2977

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <algorithm>
using namespace std;
struct Data{
int score;
string stu;
};
bool cmp(const Data &a, const Data &b){
if(a.score != b.score)
return a.score > b.score;
return a.stu < b.stu;
}
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
int N, M, G;
cin >> N >> M >> G;
int scr[M+1];
Data arr[N];
for(int i = 1;i <= M;i++)
cin >> scr[i];
for(int i = 0;i < N;i++){
cin >> arr[i].stu, arr[i].score = 0;
int temp, pos;
cin >> temp;
while(temp--) cin >> pos, arr[i].score += scr[pos];
}
sort(arr, arr+N, cmp);
cout << "case #" << step << ":\n";
int cnt = 0;
for(int i = 0;i < N;i++){
if(arr[i].score >= G)
cnt++;
else break;
}
cout << cnt << endl;
for(int i = 0;i < cnt;i++)
cout << arr[i].stu << " " << arr[i].score << endl;
}
return 0;
}

EOJ 2990

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
bool cmp(const string &a, const string &b){
for(int i = 0;i < min(a.length(), b.length());i++){
if(tolower(a[i]) != tolower(b[i]))
return tolower(a[i]) < tolower(b[i]);
}
}
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
int n;
cin >> n;
cin.get();
string arr[n];
for(int i = 0;i < n;i++)
getline(cin, arr[i]);
sort(arr, arr+n, cmp);
cout << "case #" << step << ":\n";
for(int i = 0;i < n;i++)
cout << arr[i] << endl;
}
return 0;
}

EOJ 2994

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
using namespace std;
struct Data{
int cnt, num;
}arr[500];
bool cmp(const Data &a, const Data &b) {
if(a.cnt == b.cnt)
return a.num < b.num;
return a.cnt > b.cnt;
}
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
for(int i = 0;i < 500;i++) arr[i].cnt = 0, arr[i].num = i;
int n, temp;
cin >> n;
while(n--) cin >> temp, arr[temp].cnt++;
sort(arr, arr+500, cmp);
cout << "case #" << step << ":\n";
for(auto i : arr){
int cnt = i.cnt;
if(!cnt) break;
for(int j = 0;j < cnt;j++)
cout << i.num << " ";
}
cout << endl;
}
return 0;
}

EOJ 3021

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;
int main(){
int T;
cin >> T;
cin.get();
for(int step = 0;step < T;step++){
vector<char*> alpha;
string line;
getline(cin, line);
for (auto &e : line)
if (isalpha(e))
alpha.push_back(&e);
for (int i = 0; i < alpha.size(); i++)
for (int j = i+1; j < alpha.size(); j++)
if (*alpha[i] > *alpha[j])
swap(*alpha[i], *alpha[j]);
cout << "case #" << step << ":\n";
cout << line << endl;
}
}

EOJ 3039

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>
using namespace std;
struct Data{
long long num;
int first;
};
bool cmp(const Data &a, const Data &b){
if(a.first == b.first)
return a.num < b.num;
return a.first > b.first;
}
int main(){
int T;
cin >> T;
for(int step = 0;step < T;step++){
int n;
cin >> n;
Data arr[n];
long long temp;
for(auto &i : arr){
cin >> temp, i.num = temp;
string str = to_string(i.num);
i.first = str[0] == '-' ? str[1] : str[0];
}
sort(arr, arr+n, cmp);
cout << "case #" << step << ":\n";
for(auto i: arr) cout << i.num << " ";
cout << endl;
}
return 0;
}

EOJ 2031

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
bool cmp(int a, int b){
return abs(a) < abs(b);
}
int main(){
int n;
while(cin >> n){
int arr[n];
for(auto &i : arr) cin >> i;
sort(arr, arr+n, cmp);
for(auto i : arr) cout << i << " ";
cout << endl;
}
return 0;
}

2019.04.28 施工完毕