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 }