• UVA 11462-Age sort


    题意:


      按年龄从小到大排序.

    代码如下:

    #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;
    }
  • 相关阅读:
    hdu 2065
    hdu 1999
    hdu 1562
    hdu 1728
    hdu 1180
    hdu 1088
    hdu 2133
    很好的例子。。
    putty 多标签式浏览
    df
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/5592126.html
Copyright © 2020-2023  润新知