don't get accustomed to thinking of problems in a complicated way.

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

#include <stdio.h>
#include <string.h>

char* table[4][9] = {
    {"I","II","III","IV","V","VI","VII","VIII","IX"},
    {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
    {"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
    {"M","MM","MMM"}
};

int cnt_i,cnt_v,cnt_x,cnt_l,cnt_c,cnt_d,cnt_m;
int p;
int N;

void count(char* s)
{
    int i;
    int len = strlen(s);
    for ( i = 0; i < len; i++)
    {
        switch ( s[i] )
        {
            case 'I' : cnt_i++; break;
            case 'V' : cnt_v++; break;
            case 'X' : cnt_x++; break;
            case 'L' : cnt_l++; break;
            case 'C' : cnt_c++; break;
            case 'D' : cnt_d++; break;
            case 'M' : cnt_m++; break;
        }
    }
}

void solve(int x)
{
    int t = x % 10;
    if ( t != 0 )
        count(table[p][t - 1]);
    if ( x / 10 > 0 )
    {
        p++;
        solve( x / 10 );
    }
}

int main()
{
    freopen("preface.in","r",stdin);
    freopen("preface.out","w",stdout);

    scanf("%d",&N);

    int i;
    for ( i = 1; i <= N; i++)
    {
        p = 0;
        solve(i);
    }

    if ( cnt_i != 0 )
        printf("I %d\n",cnt_i);
    if ( cnt_v != 0 )
        printf("V %d\n",cnt_v);
    if ( cnt_x != 0 )
        printf("X %d\n",cnt_x);
    if ( cnt_l != 0 )
        printf("L %d\n",cnt_l);
    if ( cnt_c != 0 )
        printf("C %d\n",cnt_c);
    if ( cnt_d != 0 )
        printf("D %d\n",cnt_d);
    if ( cnt_m != 0 )
        printf("M %d\n",cnt_m);

    return 0;
}
    

 

posted on 2012-11-09 13:21  Sinker  阅读(154)  评论(0编辑  收藏  举报