3 1 000007 James 85 000010 Amy 90 000001 Zoe 60 4 2 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 98 4 3 000007 James 85 000010 Amy 90 000001 Zoe 60 000002 James 90 0 0
Case 1: 000001 Zoe 60 000007 James 85 000010 Amy 90 Case 2: 000010 Amy 90 000002 James 98 000007 James 85 000001 Zoe 60 Case 3: 000001 Zoe 60 000007 James 85 000002 James 90 000010 Amy 90
写三个比较函数即可。
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stack> #define lson o<<1, l, m #define rson o<<1|1, m+1, r using namespace std; typedef long long LL; const int maxn = 100005; const int mod = 1000000007; struct C{ int num, v; char s[9]; }a[maxn]; int n, c; bool cmp1 (C x, C y) { return x.num < y.num; } bool cmp2 (C x, C y) { if(!strcmp(x.s, y.s)) return x.num < y.num; if(strcmp(x.s, y.s) < 0) return true; return false; } bool cmp3 (C x, C y) { if(x.v == y.v) return x.num < y.num; return x.v <= y.v; } int main() { int cnt = 1; while(~scanf("%d%d", &n, &c) && n+c) { for(int i = 0; i < n; i++) scanf("%d%s%d", &a[i].num, a[i].s, &a[i].v); if(c == 1) sort(a, a+n, cmp1); else if(c == 2) sort(a, a+n, cmp2); else sort(a, a+n, cmp3); printf("Case %d:\n", cnt); cnt ++; for(int i = 0; i < n; i++) printf("%06d %s %d\n", a[i].num, a[i].s, a[i].v); } return 0; }
HDU 1862 EXCEL排序 (排序水题),布布扣,bubuko.com
原文:http://blog.csdn.net/u013923947/article/details/38461153