poj2418 map 快排水过

/*计算每个单词的百分比按字典序输出*/


#include <cstdio>
#include<cstdlib>
#include <cstring>
#include <cctype>
#include <string>
#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

struct tt
{
    char w[40];
    bool operator < (const tt a)const
    {
        int t = strcmp(w,a.w);
        if(t < 0)
            return true;
        return false;
    }
};

int n;
map<string,int>sad;
map<string,bool>sadd;
tt tre[10100];

int main()
{
    //freopen("input.in","r",stdin);
    char str[40];
    int count = 0;
    memset(tre,0,sizeof(tre));
    n = 0;
    while(fgets(str,35,stdin) != NULL)
    {
        if(str[0] == '\n')
            break;
        int len = strlen(str);
        str[len-1] = '\0';
        count++;
        if(!sadd[str])
        {
            strcpy(tre[n++].w,str);
            sadd[str] = true;
        }
        sad[str]++;
    }
    sort(tre,tre+n);
    for(int i = 0;i < n;i++)
    {
        double temp = 1.0*sad[tre[i].w] / count *100;
        printf("%s %.4lf\n",tre[i].w,temp);
    }
    return 0;
}

你可能感兴趣的:(map)