sort函数比较cmp写法

hihocoder1566http://hihocoder.com/problemset/problem/1566

一直WA因为cmp的写法写错了,未能正确实现排序功能。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<string>
 7 #include<cmath>
 8 #include<vector>
 9 #include<stack>
10 #include<iterator>
11 #include<queue>
12 #include<cctype>
13 #include<map>
14 #define lson l, m, rt<<1
15 #define rson m+1, r, rt<<1|1
16 #define IO ios::sync_with_stdio(false);cin.tie(0);
17 #define INF 0x3f3f3f3f
18 #define MAXN 100010
19 const int MOD=1e9+7;
20 typedef long long ll;
21 using namespace std;
22 int t;
23 typedef struct {
24     char s1[110], s2[110];
25     int sum;
26 }Node;
27 Node node[100010];
28 map<char, int> mp;
29 int cal(const char s[])
30 {
31     int len = strlen(s), ans=0, last=mp[s[len-1]];
32     for(int i = len-2; i >= 0; i--){
33         if(last-mp[s[i]] > 0){
34             last -= mp[s[i]];
35         }
36         else{
37             ans += last;
38             last = mp[s[i]];
39         }
40     }
41     ans += last;
42     return ans;
43 }
44 bool cmp(const Node a, const Node b)
45 {
46     if(strcmp(a.s1, b.s1) != 0) return strcmp(a.s1, b.s1)<0;
47     else{
48         return a.sum < b.sum;
49     }
50 }
51 int main()
52 {
53     IO;
54     mp['I']=1;mp['V']=5;mp['X']=10;mp['L']=50;mp['C']=100;mp['D']=500;mp['M']=1000;
55     cin >> t;
56     for(int i = 0; i < t; i++){
57         cin >> node[i].s1 >> node[i].s2;
58         node[i].sum = cal(node[i].s2);
59         //cout << cal(node[i].s2) << endl;
60     } 
61     sort(node, node+t, cmp);
62     for(int i = 0; i < t; i++){
63         cout << node[i].s1 << " " << node[i].s2 << endl;
64     }
65     return 0;
66 }

 

转载于:https://www.cnblogs.com/Surprisezang/p/8674964.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值