一开始看到这道题目的时候,感觉好难

还要算出罗马的规则。

但是仔细一看,数据规模很小, n 只给到3500

看完题目给出了几组样例之后就有感觉了

解题方法就是:

n的每个十进制数 转换成相应的罗马数字,然后统计每个罗马数字出现的次数即可

还是一道简单的构造题。

(以下摘自https://www.byvoid.com/blog/usaco-221preface-numbering/)

转化有以下规则:

1、数较大部分在前,较小部分在后

2、表示10整倍数的字母(I X C M)最多可以累加三次

3、要累加4次的数应该将比该数的字母稍大的表示5整倍数或是10的整倍数的字母在后,累加的字母在前(例如IV XL CD CM)

了解以上规则后发现并不需要实际“转化”出罗马数字,而只用统计每个字母出现的次数。

My Source Code:

/*
ID: wushuai2
PROG: preface
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; int N, ans[];
string op[][] = {
{},
{"", "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"}
}; void add(string str){
int i, j;
for(i = ; i < str.length(); ++i){
if(str[i] == 'I') ++ans[];
else if(str[i] == 'V') ++ans[];
else if(str[i] == 'X') ++ans[];
else if(str[i] == 'L') ++ans[];
else if(str[i] == 'C') ++ans[];
else if(str[i] == 'D') ++ans[];
else if(str[i] == 'M') ++ans[];
}
} int main() {
ofstream fout ("preface.out");
ifstream fin ("preface.in");
int i, j, k, t, n, s, c, w, q;
fin >> N;
for(i = ; i <= N; ++i){
int temp_g = i % ;
string temp = op[][temp_g];
add(temp);
if(i < ) continue;
int temp_s = (i / ) % ;
temp = op[][temp_s];
add(temp); if(i < ) continue;
int temp_b = (i / ) % ;
temp = op[][temp_b];
add(temp); if(i < ) continue;
int temp_q = i / ;
temp = op[][temp_q];
add(temp);
}
int flag = -;
for(i = ; i >= ; --i){
if(ans[i]){
flag = i;
break;
}
}
for(i = ; i <= flag; ++i){
if(i == ){
fout << "I" << ' ' << ans[i] << endl;
} else if(i == ){
fout << "V" << ' ' << ans[i] << endl;
} else if(i == ){
fout << "X" << ' ' << ans[i] << endl;
} else if(i == ){
fout << "L" << ' ' << ans[i] << endl;
} else if(i == ){
fout << "C" << ' ' << ans[i] << endl;
} else if(i == ){
fout << "D" << ' ' << ans[i] << endl;
} else if(i == ){
fout << "M" << ' ' << ans[i] << endl;
}
} fin.close();
fout.close();
return ;
}

USACO Preface Numbering 构造的更多相关文章

  1. USACO 2.2 Preface Numbering

    Preface Numbering A certain book's prefaces are numbered in upper case Roman numerals. Traditional R ...

  2. 【USACO 2.2】Preface Numbering (找规律)

    求 1-n 的所有罗马数字表达中,出现过的每个字母的个数. 分别对每个数的罗马表达式计算每个字母个数. 对于十进制的每一位,都是一样的规则,只是代表的字母不同. 于是我们从最后一位往前考虑,当前位由字 ...

  3. USACO Section2.2 Preface Numbering 解题报告 【icedream61】

    preface解题报告----------------------------------------------------------------------------------------- ...

  4. USACO Section 2.2: Preface Numbering

    搬了leetcode的代码 /* ID: yingzho1 LANG: C++ TASK: preface */ #include <iostream> #include <fstr ...

  5. USACO Prime Palindromes 构造回文数

    这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...

  6. Preface Numbering

    链接 分析:先打表需要用到的罗马数字,然后暴力转换,最后统计一下即可 /* PROB:preface ID:wanghan LANG:C++ */ #include "iostream&qu ...

  7. Preface Numbering序言页码

    题面 (preface.pas/c/cpp) 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1 V 5 X 10 L 50 C 100 D 500 M ...

  8. USACO2.2 Preface Numbering【思维+打表】

    这道题乍一看没有什么思路,细看还是没有什么思路 嗯,细看还是可以看出些什么端倪. 不能复合嵌套什么的 总结一下就只有这样3种规则: 1.IXCM最多三个同样连续 加起来2.递减:加起来 注意VLD不连 ...

  9. P1465 序言页码 Preface Numbering (手推)

    题目描述 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1 V 5 X 10 L 50 C 100 D 500 M 1000 最多3个同样的可以表示为 ...

随机推荐

  1. 关于CDC在非控件类中的使用

    在非CStatic的派生类中,由于进行图形的绘制的话,我们需要对该类传入一个CDC以便于绘画.这是因为非CStatic等控件类无法自己产生onPaint这类的消息,因此需要借传入的CDC进行回执,然后 ...

  2. Effective C++ 条款39

    我从本条款中学到了下面内容: 1.private继承不同于另外两种继承,派生类对象不能隐式转换为基类对象. 例如以下代码: class Bird//鸟 { }; class ostrich:priva ...

  3. js 常用的一些函数

    //设置默认焦点    var setFocus = function SetFocus(elementId) {        document.onkeydown = function (even ...

  4. 2.3.9 用NPOI操作EXCEL--通过NPOI获得公式的返回值

    前面我们学习了通过NPOI向Excel中设置公式,那么有些读者可能会问:“NPOI能不能获取公式的返回值呢?”,答案是可以!一.获取模板文件中公式的返回值如在D盘中有一个名为text.xls的Exce ...

  5. 获取图片中的文本--MODI

    http://www.aspsnippets.com/Articles/Read-Extract-Text-from-Image-OCR-in-ASPNet-using-C-and-VBNet.asp ...

  6. CodeForces 443B Kolya and Tandem Repeat

    题目:Click here 题意:给定一个字符串(只包含小写字母,并且最长200)和一个n(表示可以在给定字符串后面任意加n(<=200)个字符).问最长的一条子串长度,子串满足前半等于后半. ...

  7. UNIX/Linux进程间通信IPC---管道--全总结(实例入门)

    管道 一般,进程之间交换信息的方法只能是经由fork或exec传送打开文件,或者通过文件系统.而进程间相互通信还有其他技术——IPC(InterProcessCommunication) (因为不同的 ...

  8. [LeetCode]题解(python):008-String to Integer (atoi)

    题目来源: https://leetcode.com/problems/string-to-integer-atoi/ 题意分析: 这道题也是简单题,题目意思是要将字符串转化成int.比如‘123’转 ...

  9. NSData 数据转换

    NSData,数据,当我们需要把一些信息写入到文件里或发送到网络上,我们需要把这些数据转换下,变成纯粹的0.1字符流 数组转 NSData NSData *GLYtime = [NSKeyedArch ...

  10. Ants (POJ 1852)

    题目描述: Description An army of ants walk on a horizontal pole of length l cm, each with a constant spe ...