1364. 序言页码

12. 整数转罗马数字

pair<int,string> mp[]={
    {1000, "M"},
    {900,  "CM"},
    {500,  "D"},
    {400,  "CD"},
    {100,  "C"},
    {90,   "XC"},
    {50,   "L"},
    {40,   "XL"},
    {10,   "X"},
    {9,    "IX"},
    {5,    "V"},
    {4,    "IV"},
    {1,    "I"},
};
unordered_map<char,int> cnt;
int n;

int main()
{
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        int num=i;
        for(auto &[value,symbol] : mp)
        {
            while(num >= value)
            {
                num -= value;
                for(auto &c : symbol)
                    cnt[c]++;
            }
            if(num == 0) break;
        }
    }

    string s="IVXLCDM";
    for(auto &c : s)
        if(cnt[c])
            cout<<c<<' '<<cnt[c]<<endl;

    //system("pause");
    return 0;
}
posted @ 2021-06-04 14:15  Dazzling!  阅读(27)  评论(0编辑  收藏  举报