题意:
按年龄从小到大排序.
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <fstream> #include <cmath> #include <ctime> #include <cstdlib> #include <algorithm> #include <set> #include <map> #include <list> #include <stack> #include <queue> #include <iterator> #include <vector> using namespace std; #define LL long long #define INF 0x3f3f3f3f #define MOD 1000000007 #define MAXN 10000010 #define MAXM 1000010 const int MAXN = 2000000+5; int a[MAXN]; // 方法一:快速排序 int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } void qsort(int b[], int l, int r) { int i, j, x; i = l; j = r; x = b[i]; while(i != j) { while(b[j]>x&&j>i) j--; if(i < j) { b[i] = b[j]; i++; } while(b[i]<x&&j>i) i++; if(i < j) { b[j] = b[i]; j--; } } b[i] = x; qsort(b, l, j-1); qsort(b, i+1, r); } int main() { int n; while(scanf("%d", &n)==1&&n) { int i; for(i = 0; i < n; i++ ) scanf("%d", &a[i]); qsort(a, n, sizeof(a[0]), cmp); for(i = 0; i < n; i++ ) { if(i != 0) printf(" "); printf("%d", a[i]); } printf(" "); } return 0; } //方法二:计数排序 int main() { int n; int x; int i, j; while(scanf("%d", &n)==1&&n) { int flag = 0; memset(a, 0, sizeof(a)); for(i = 1; i <= n; i++ ) { scanf("%d", &x); a[x]++; } for(i = 1; i <= 100; i++ ) //年龄:1-100岁 for(j = 1; j <= a[i]; j++ ) //每个年龄的人数(年龄从1岁开始) { if(flag) printf(" "); flag = 1; printf("%d", i); } printf(" "); } return 0; }