码迷,mamicode.com
首页 > 其他好文 > 详细

usaco Preface Numbering

时间:2015-09-03 20:20:09      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

 题目算法不难,难的是读懂题意,意思是从1到N的数字转换成罗马数字,然后统计所有数字中的各种字母出现的次数

 对于每个数,用贪心的方法转换为罗马数字,然后统计就好了

/*
ID: modengd1
PROG: preface
LANG: C++
*/

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <string>
#include <cstring>
using namespace std;
string St[15]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[15]={1000,900,500,400, 100,90,50,40, 10, 9,5,4,1};
int counter[7];
char output[10]={‘I‘,‘V‘,‘X‘,‘L‘,‘C‘,‘D‘,‘M‘};
void Count(string S)
{
    for(int i=0;i<S.size();i++)
    {
        switch(S[i])
        {
            case ‘I‘:
                counter[0]++;break;
            case ‘V‘:
                counter[1]++;break;
            case‘X‘:
                counter[2]++;break;
            case‘L‘:
                counter[3]++;break;
            case‘C‘:
                counter[4]++;break;
            case‘D‘:
                counter[5]++;break;
            case‘M‘:
                counter[6]++;break;
        }
    }
}
string change(int x)
{
    string s;
    while(x>0)
    {
        for(int j=0;j<15;j++)
        {
            if(x>=value[j])
            {
                x-=value[j];
                for(int k=0;k<St[j].size();k++)
                    s.push_back(St[j][k]);
                break;
            }
        }
    }
    return s;
}
int main()
{
    freopen("preface.in","r",stdin);
    freopen("preface.out","w",stdout);
    int N;
    memset(counter,0,sizeof(counter));
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
    {
        Count(change(i));
    }
    for(int i=0;i<7;i++)
        if(counter[i])
            cout<<output[i]<<‘ ‘<<counter[i]<<endl;
    return 0;
}

  

usaco Preface Numbering

标签:

原文地址:http://www.cnblogs.com/modengdubai/p/4780599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!