牛客练习赛15

A-吉姆的运算式

题解:寻找最后出现的数字。

 1 #pragma warning(disable:4996)
 2 #include
 3 #include<string>
 4 #include
 5 #include
 6 #include
 7 #define ll long long 
 8 using namespace std;
 9 
10 const int maxn = 10005;
11 
12 string s;
13 
14 int main()
15 {
16     while (cin >> s) {
17         int n = s.size();
18         int pos = -1;
19         for (int i = n - 1; i >= 0; i--) if (s[i] >= '0'&&s[i] <= '9') { pos = i; break; }
20         int los = 0;
21         for (int i = pos; i >= 0; i--) if (s[i] == ',' || s[i] == '(' || s[i] == ')') { los = i + 1; break; }
22         for (int i = los; i <= pos; i++) cout << s[i];
23         cout << endl;
24     }
25     return 0;
26 }

B-沃老师学生的成绩

来自官方题解:score要去掉末尾‘0’,否则可能会TLE。。。还是太菜了。。。别忘了将开始删除的末尾0在输出的时候加上。

感受:对STL提供的模板函数还是不熟啊,学习了一波代码的写法。

 1 #pragma warning(disable:4996)
 2 #include
 3 #include<string>
 4 #include
 5 #include
 6 #include
 7 #define ll long long 
 8 using namespace std;
 9 
10 const int maxn = 200005;
11 
12 int n;
13 
14 struct node {
15     string name, score;
16     int zero_cnt;
17     void init() {
18         zero_cnt = 0;
19         int len = score.size();
20         while (zero_cnt < len && score[len - 1 - zero_cnt] == '0') zero_cnt++;
21         score.resize(len - zero_cnt);
22     }
23     bool operator<(const node& i)const {
24         if (score == i.score) return name < i.name;
25         else return score > i.score;
26     }
27 }pe[maxn];
28 
29 int main()
30 {
31     while (cin >> n) {
32         for (int i = 1; i <= n; i++) {
33             cin >> pe[i].name >> pe[i].score;
34             pe[i].init();
35         }
36         sort(pe + 1, pe + n + 1);
37         for (int i = 1; i <= n; i++) cout << pe[i].name << " " << pe[i].score + string(pe[i].zero_cnt,'0') << endl;
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/zgglj-com/p/8824679.html

你可能感兴趣的:(数据结构与算法)