USACO Preface Numbering 构造
一开始看到这道题目的时候,感觉好难
还要算出罗马的规则。
但是仔细一看,数据规模很小, 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 构造的更多相关文章
- USACO 2.2 Preface Numbering
Preface Numbering A certain book's prefaces are numbered in upper case Roman numerals. Traditional R ...
- 【USACO 2.2】Preface Numbering (找规律)
求 1-n 的所有罗马数字表达中,出现过的每个字母的个数. 分别对每个数的罗马表达式计算每个字母个数. 对于十进制的每一位,都是一样的规则,只是代表的字母不同. 于是我们从最后一位往前考虑,当前位由字 ...
- USACO Section2.2 Preface Numbering 解题报告 【icedream61】
preface解题报告----------------------------------------------------------------------------------------- ...
- USACO Section 2.2: Preface Numbering
搬了leetcode的代码 /* ID: yingzho1 LANG: C++ TASK: preface */ #include <iostream> #include <fstr ...
- USACO Prime Palindromes 构造回文数
这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...
- Preface Numbering
链接 分析:先打表需要用到的罗马数字,然后暴力转换,最后统计一下即可 /* PROB:preface ID:wanghan LANG:C++ */ #include "iostream&qu ...
- Preface Numbering序言页码
题面 (preface.pas/c/cpp) 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1 V 5 X 10 L 50 C 100 D 500 M ...
- USACO2.2 Preface Numbering【思维+打表】
这道题乍一看没有什么思路,细看还是没有什么思路 嗯,细看还是可以看出些什么端倪. 不能复合嵌套什么的 总结一下就只有这样3种规则: 1.IXCM最多三个同样连续 加起来2.递减:加起来 注意VLD不连 ...
- P1465 序言页码 Preface Numbering (手推)
题目描述 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1 V 5 X 10 L 50 C 100 D 500 M 1000 最多3个同样的可以表示为 ...
随机推荐
- python自学笔记(二)python基本数据类型之字符串处理
一.数据类型的组成分3部分:身份.类型.值 身份:id方法来看它的唯一标识符,内存地址靠这个查看 类型:type方法查看 值:数据项 二.常用基本数据类型 int 整型 boolean 布尔型 str ...
- https tomcat 证书搭建
首先生成证书说明 keytool -genkey -alias castest -keyalg RSA -keystore c:/keys/caskey 先让输入密码,密码必须记住,下面会用到 其中“ ...
- Windows下Python中的中文路径和中文输出问题
这几天有个项目需要写一点类似于脚本的小程序,就用Python写了,涉及到中文路径和中文输出的问题,整理一下. 有一个问题我觉得需要先强调一下,在写Python程序的时候,一定保证编码是utf-8,然后 ...
- 浅谈RFID电子标签封装技术
1RFID技术概述 1.1RFID技术概念 RFID是RadioFrequencyIdentification的缩写,即射频识别技术,俗称电子标签.RFID射频识别是一种非接触式的自动识别技术,它通过 ...
- Eclipse创建新项目时无法输入项目名的解决方法
放假耍了那么久,也是该收心忙活了. 今天打开Eclipse新建项目时,发生了一个很奇怪的情况,就是在下面这个位置的输入框无法输入. 经过百度之后,发现解决方案是(原地址点我) Eclipse图标右键 ...
- Home | WebScraping.com
Home | WebScraping.com We specialize in extracting data from websites, which is known as web scrapin ...
- GLSL中的各种变量总结
1.uint CreateShader(enum type) : 创建空的shader object; type: VERTEX_SHADER, 2.void ShaderSource(uint sh ...
- HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)
题目地址:HDU4859 做了做杭电多校,知识点会的太少了.还是将重点放在刷专题补知识点上吧,明年的多校才是重点. 这题题目求的最长周长.能够试想一下,这里的海岸线一定是在"."和 ...
- [每日一题] 11gOCP 1z0-052 :2013-09-15 Enterprise Manager Support Workbench..................B9
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11715219 正确答案:ABD EnterpriseManger Support Work ...
- 发送Email并添加附件
1. 添加命名空间 using System.Net.Mail; using System.Net; 2. The HTML MarpUp <html xmlns="http://ww ...