USACO2.2.1 Preface Numbering 序言页码 解题报告(模拟)

Description

 

一类书的序言是以罗马数字标页码的。传统罗马数字用单个字母表示特定的数值,以下是标准数字表:

I 1  L 50  M 1000
V 5  C 100
X 10 D 500

最多3个同样的可以表示为10n的数字(I,X,C,M)可以连续放在一起,表示它们的和:

III=3
CCC=300

可表示为5x10n的字符(V,L,D)从不连续出现。

除了下一个规则,一般来说,字符以递减的顺序接连出现:

CCLXVIII = 100+100+50+10+5+1+1+1 = 268

有时,一个可表示为10n的数出现在一个比它大1级或2级的数前(I在V或X前面,X在L或C前面,等等)。在这种情况下,数值等于后面的那个数减去前面的那个数:

IV = 4
IX = 9
XL = 40

This compound mark forms a unit and may not be combined to make another compound mark (e.g., IXL is wrong for 39; XXXIX is correct).

像XD, IC, 和XM这样的表达是非法的,因为前面的数比后面的数小太多。对于XD(490的错误表达),可以写成 CDXC; 对于IC(99的错误表达),可以写成XCIX; 对于XM(990的错误表达),可以写成CMXC。 90 is expressed XC and not LXL, since L followed by X connotes that successive marks are X or smaller (probably, anyway).


给定N(1 <= N < 3,500), 序言的页码数,请统计在第1页到第N页中,有几个I出现,几个V出现,等等 (从小到大的顺序)。不要输出并没有出现过的字符。

比如N = 5, 那么页码数为: I, II, III, IV, V. 总共有7个I出现,2个V出现。

 

Input

 

一个整数N。

 

Output

 

每行一个字符和一个数字k,表示这个字符出现了k次。字符必须按数字表中的递增顺序输出。

 

Sample Input

5

Sample Output

I 7
V 2

 

设一个表查询即可

能发现罗马数字每一位都是有规律的,具体百度罗马数字1-10,然后数一数I和V就知道了

 

唯一的坑点是答案是按照罗马数字大小排序的,不是按照字母的ASCII码排序的,可能就只有我这么傻吧

#include <map>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define lowbit(a) (a&(-a))
#define _mid(a,b) ((a+b)/2)
#define debu(a) cout << a <<" ";
#define debug(a) cout << a << endl;
#define _mem(a,b) memset(a,0,(b+3)<<2)
#define fori(a) for(int i=0;i<a;i++)
#define forj(a) for(int j=0;j<a;j++)
#define ifor(a) for(int i=1;i<=a;i++)
#define jfor(a) for(int j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
 
#define IO do{\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);}while(0)
#define mp(a,b) make_pair(a,b);
using namespace std;
typedef long long ll;
const int maxn =  500;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
const double EPS = 1e-7;
const double Pi = acos(-1);
const int MOD = 1e9+7;
string s = "IVXLCDM##";
int q[10];
int a[2][10]={{0,1,2,3,1,0,1,2,3,1},
              {0,0,0,0,1,1,1,1,1,0}};
int x,y;
void juge(int i){
    int j = 0;
    while(i){
        if(a[0][i%10])
            q[j] += a[0][i%10];
        if(a[1][i%10])
            q[j+1] += a[1][i%10];
        if(i%10==9)
            q[j+2]++;
        i /= 10;
        j += 2;
    }
}
int main() {
    int n;
    cin >> n;
    ifor(n)
        juge(i);
    fori(10)
        if(q[i]&&s[i]!='#')
            cout << s[i] <<" "<< q[i]<<endl;
 
    return 0;
}

 

posted @ 2018-09-14 19:45  秃头大师  阅读(254)  评论(0)    收藏  举报