您的位置:首页 > 其它

usaco Preface Numbering

2015-09-03 19:51 183 查看
题目算法不难,难的是读懂题意,意思是从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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: