USACO:Preface Numbering

/*
ID: Jang Lawrence
PROG: preface
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#define mp make_pair
using namespace std;
int n;
char s[][10][5]={{"M","MM","MMM"},{"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},{"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},{"I","II","III","IV","V","VI","VII","VIII","IX"}};
char str[]={"IVXLCDM"};
int num[10],id[26];
void count(int x)
{
    int cnt=3;
    while(x)
    {
        int k=x%10;
        x/=10;
        if(k==0) ;
        else
        {
            for(int i=0;s[cnt][k-1][i]!='\0';++i)
            num[id[s[cnt][k-1][i]-'A']]++;
        }
        cnt--;
    }
}
int main()
{
  #ifndef  DEBUG
  freopen("preface.in","r",stdin);
  freopen("preface.out","w",stdout);
  #endif
  for(int i=0;str[i]!='\0';++i)
  id[str[i]-'A']=i;
  scanf("%d",&n);
  for(int i=1;i<=n;++i)
  count(i);
  for(int i=0;i<7;++i)
  if(num[i]!=0)
  printf("%c %d\n",str[i],num[i]);
    return 0;
}

你可能感兴趣的:(USACO:Preface Numbering)