tantan的博客

Notes, ideas, and observations

水题

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
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
int n;
cin >> n;
int mod = 0;
ostringstream out;
for (int i = 0; i < n; i++) {
string name, pass;
int flag = 0;
cin >> name >> pass;
for (auto &&ch : pass) {
switch (ch) {
case 'l': ch = 'L'; flag = 1; break;
case '1': ch = '@'; flag = 1; break;
case '0': ch = '%'; flag = 1; break;
case 'O': ch = 'o'; flag = 1; break;
default: break;
}
}
if (flag) {
mod++;
out << name << " " << pass << endl;
}
}
if (mod) {
cout << mod << endl << out.str();
} else {
cout << "There " << (n == 1 ? "is" : "are") << " " << n << " account"
<< (n == 1 ? "" : "s") << " and no account is modified" << endl;
}
return 0;
}

#c++11(新?)特性之右值引用

#右值

送分题

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
43
44
45
46
47
48
#include <iostream>
using namespace std;

struct student {
string name;
string major;
};

int main()
{
int n;
cin >> n;
student s, male_min, female_max;
int score, male_score_min = 101, female_score_max = -1;
string gender;
for (int i = 0; i < n; i++) {
cin >> s.name >> gender >> s.major >> score;
if (gender == "M") {
if (score < male_score_min) {
male_min = s;
male_score_min = score;
}
} else {
if (score > female_score_max) {
female_max = s;
female_score_max = score;
}
}
}

if (female_score_max != -1) {
cout << female_max.name << " " << female_max.major << endl;
} else {
cout << "Absent" << endl;
}
if (male_score_min != 101) {
cout << male_min.name << " " << male_min.major << endl;
} else {
cout << "Absent" << endl;
}
if (female_score_max != -1 && male_score_min != 101) {
cout << female_score_max - male_score_min << endl;
} else {
cout << "NA" << endl;
}

return 0;
}

水题, 注意边界情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n = nums.size();
int last = (n>0?nums[0]:0);
vector<string> res;
for(int i=1;i<=n;i++){
if(i==n || nums[i]!=nums[i-1]+1){
if (last==nums[i-1]){
res.push_back(to_string(last));
}else{
res.push_back(to_string(last)+"->"+to_string(nums[i-1]));
}
if(i<n) last=nums[i];
}
}
return res;
}
};
0%