map進行字典序排序

#include<iostream>
#include<string>
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
    map<string,int>mp;
    int n;
    string s;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++)
    {
        getline(cin,s);//c++中輸入字符串(可包含空格)
        mp[s]++;//自動按字典序累加相同的字符串
    }
    for(auto it=mp.begin();it!=mp.end();it++)
    {
        cout<<it->first<<" ";
        printf("%.4lf%%\n",it->second*100.0/n);
    }
    
    return 0;
}

input:

29

Red Alder

Ash

Aspen

Basswood

Ash

Beech

Yellow Birch

Ash

Cherry

Cottonwood

Ash

Cypress

Red Elm

Gum

Hackberry

White Oak

Hickory

Pecan

Hard Maple

White Oak

Soft Maple

Red Oak

Red Oak

White Oak

Poplan

Sassafras

Sycamore

Black Walnut

Willow

output:

Ash 13.7931%

Aspen 3.4483%

Basswood 3.4483%

Beech 3.4483%

Black Walnut 3.4483%

Cherry 3.4483%

Cottonwood 3.4483%

Cypress 3.4483%

Gum 3.4483%

Hackberry 3.4483%

Hard Maple 3.4483%

Hickory 3.4483%

Pecan 3.4483%

Poplan 3.4483%

Red Alder 3.4483%

Red Elm 3.4483%

Red Oak 6.8966%

Sassafras 3.4483%

Soft Maple 3.4483%

Sycamore 3.4483%

White Oak 10.3448%

Willow 3.4483%

Yellow Birch 3.4483%

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章