Home > Old Blog Posts > USACO 2.2.1 Preface Numbering

USACO 2.2.1 Preface Numbering


认真研究可发现,罗马数字记法其实也是比较特殊的十进制记法。总结得出:
string ge[10]={””,”I”,”II”,”III”,”IV”,”V”,”VI”,”VII”,”VIII”,”IX”},
shi[10]={””,”X”,”XX”,”XXX”,”XL”,”L”,”LX”,”LXX”,”LXXX”,”XC”},
bai[10]={””,”C”,”CC”,”CCC”,”CD”,”D”,”DC”,”DCC”,”DCCC”,”CM”},
qian[4]={””,”M”,”MM”,”MMM”};
以上就是计数表,把读入的数处理一下就行了

代码;

/*
ID: dementr1
PROG: preface
LANG: C++
*/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string ge[10]={“”,”I”,”II”,”III”,”IV”,”V”,”VI”,”VII”,”VIII”,”IX”},
shi[10]={“”,”X”,”XX”,”XXX”,”XL”,”L”,”LX”,”LXX”,”LXXX”,”XC”},
bai[10]={“”,”C”,”CC”,”CCC”,”CD”,”D”,”DC”,”DCC”,”DCCC”,”CM”},
qian[4]={“”,”M”,”MM”,”MMM”};
char number[7]={‘I’,’V’,’X’,’L’,’C’,’D’,’M’};
int main()
{
ifstream fin(“preface.in”);
ofstream fout(“preface.out”);
int num,t,a[4]={},i=0,j,count[10]={},end;
string tmp;
fin>>end;
for(num=1;num<=end;num++)
{
t=0;
memset(a,0,sizeof(a));
tmp=””;
i=0;
t=num;
while(t>0)
{
a[i]=t%10;
t=t/10;
i++;
}
tmp=qian[a[3]]+bai[a[2]]+shi[a[1]]+ge[a[0]];
for(i=0;i<tmp.length();i++)
{
for(j=0;j<7;j++)
{
if(tmp[i]==number[j])
{
count[j]++;
break;
}
}
}
}
for(i=0;i<7;i++)
if(count[i]!=0) fout<<number[i]<<” “<<count[i]<<endl;
return 0;
}

Categories: Old Blog Posts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: