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

还要算出罗马的规则。

但是仔细一看,数据规模很小, 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. CentOS5.4下安装codeblocks 12.11

    centos6.3下安装codeblock简单多了,这些开源的软件也都在不断进步.原来装过codeblocks10.05,忘了,这次安装又花了我半天时间,最后总算搞定. 先是安装了wxGTK-2.8. ...

  2. Linux C网络编程学习笔记

    Linux C网络编程总结报告 一.Linux C 网络编程知识介绍: 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户端:(client) 在网络程序中, ...

  3. vs2012-vs2013编译出来的程序不能在xp上运行解决方法

    在链接标志中加入参数: /SUBSYSTEM:WINDOWS,"5.01" 在ide环境下: 项目属性-常规-平台工具集-Visual Studio 2013 - Windows ...

  4. JAVA GUI学习 - JTable表格组件学习_A ***

    public class JTableKnow_A extends JFrame { public JTableKnow_A() { this.setBounds(300, 100, 400, 300 ...

  5. golang实现tcp接入服务器

    接入服务器和后端业务服务其维持tcp连接,多个前端请求通过接入服务器访问后端业务服务器,接入服务器可以方便增加路由功能,维护多个业务服务器,根据消息ID路由到具体的业务服务器. 项目目录如下 simp ...

  6. MySQL 存储过程创建表

    创建 CREATE PROCEDURE  Pro_IsExistTable(ableName varchar(100),out outputParam int)BEGINset @csql=conca ...

  7. MD5算法【计算文件和字符串的MD5值】

    1. MD5算法是一种散列(hash)算法(摘要算法,指纹算法),不是一种加密算法(易错).任何长度的任意内容都可以用MD5计算出散列值.MD5的前身:MD2.MD3.MD4.介绍工具:CalcMD5 ...

  8. MVC+ADO模式

    MVC+DAO设计模式 博客分类: Java Java WEB开发   MVC+DAO设计模式 本文摘自:http://www.paper.edu.cn    基于MVC+DAO设计模式的Struts ...

  9. Yii2归档安装法

    打开dos 操作命令  1.先把init.bat  拖到dos命令窗口 打开 (如果拖过去没打开 可以回车Enter一下) 这里需要注意一下  下图红圈中是两种环境  0->开发环境  1-&g ...

  10. thinkphp小技巧

    if(IS_POST) _404("页面不存在",U("index"))               //如果不是POST提交,则跳转到index.debug模 ...