USACO Preface Numbering
来源:互联网 发布:移动硬盘分区隐藏软件 编辑:程序博客网 时间:2024/06/05 17:09
我很不争气的用了最简单的枚举,觉得自己在对照string数组分析字母的函数处理上还是不错滴
/*ID: wangxin12PROG: prefaceLANG: C++*/#include <iostream>#include <fstream>#include <vector>#include <string>using namespace std;int M, D, C, L, X, V, I;int N;static string s[4][10] = {{ "", "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 rome(string curr) {char c;for(int i = 0; i < curr.size(); i++) {c = curr[i];switch(c) {case 'M': M++; break;case 'D': D++; break;case 'C': C++; break;case 'L': L++; break;case 'X': X++; break;case 'V': V++; break;case 'I': I++; break;}}}void analyze(int n) {string curr;int digit;digit = n / 1000;n = n % 1000;curr = s[3][digit];rome(curr);digit = n / 100;n = n % 100;curr = s[2][digit];rome(curr);digit = n / 10;n = n % 10;curr = s[1][digit];rome(curr);digit = n / 1;curr = s[0][digit];rome(curr);}int main() {int N;//inputifstream fin("preface.in");fin>>N;fin.close();//BODYfor(int i = 1; i <= N; i++) {analyze(i);}//Outputofstream fout("preface.out");if(I > 0) fout<<"I"<<" "<<I<<endl;if(V > 0) fout<<"V"<<" "<<V<<endl;if(X > 0) fout<<"X"<<" "<<X<<endl;if(L > 0) fout<<"L"<<" "<<L<<endl;if(C > 0) fout<<"C"<<" "<<C<<endl;if(D > 0) fout<<"D"<<" "<<D<<endl;if(M > 0) fout<<"M"<<" "<<M<<endl;fout.close();return 0;}
Preface Numbering (preface)
分析一:枚举
首先大多数人都能想到的算法就是枚举,因为枚举容易易懂。虽说枚举不超时,但是我们也应该尽可能优化,使程序更快速。所以我们应该制造一个数字表:
const
shu:array[1..4,0..9] of string=((,'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',,,,,,));//千
那么之后进行枚举时就可以加快速度……
分析二:数学问题
这道题的n只有3000多,从1~n把根据题目转换成字母,然后统计出现次数也不会超时。不过这里介绍另一种更高效的算法。
0~9中IVX出现的次数和10~19,20~29,x0~x9中的相同(其它字母都未出现)。0~99中仅十位产生的字母中,字母出现的次数相同,不同的是字母成了XLC,后移了2位。
我们把n按十进制位从低到高的顺序统计每位出现的字母次数。
以n=234为例,
仅个位:23个0~9的次数,1个0~3的次数,1个4的次数。(IVX)
仅十位:2个0~9的次数*10,1个0~2的次数*10,5个3的次数。(XLC)
仅百位:0个0~9的次数*100,1个0~1的次数*100,35个2的次数。(CDM)
分析三
数学统计首先,不考虑顺序,那么任意一个数对应的罗马数字,都可以用其他几个n×10^k表示,比如999=900+90+9=CM+XC+IX;所以我们预先处理出1~9,10~90,100~900,1000~3000这几个数所对应的字符,这样再枚举效率就要高些(当然对于3000的数据看不太出来,要是10^6试试……)。
分析四
每一位对应三个字母
个位 I V X
十位 X L C
百位 C D M
千位 M
字母 A B C
然后:1对应A,2对应AA,3对应AAA,4对应AB,5对应B,6对应BA,7对应BAA,8对应BAAA,9对应C。
这样先写一个1..9,A..C的常量数组,然后对每一个数的每一位进行统计就OK了。
- USACO 2.2 Preface Numbering (preface)
- 【搜索】【USACO】Preface Numbering
- USACO Preface Numbering
- USACO:Preface Numbering
- USACO-Preface Numbering
- USACO 2.2 Preface Numbering
- USACO 2.2 Preface Numbering
- usaco Section 2.2 Preface Numbering
- USACO :Preface Numbering 解题报告
- [USACO 2.2.1] Preface Numbering
- USACO 2.2.1 Preface Numbering
- USACO 2.2.1 Preface Numbering
- USACO Section 2.2 Preface Numbering
- USACO Section 2.2 Preface Numbering
- USACO 2.2.1 Preface Numbering
- USACO 2.2 Preface Numbering (模拟)
- USACO Section 2.2 Preface Numbering
- usaco Preface Numbering 序言页码
- JTextComponent 结构
- 黑马程序员——面向对象(1)
- 黑马程序员——面向对象(2)
- IsBadReadPtr|IsBadWritePtr调试崩溃
- 黑马程序员——String类
- USACO Preface Numbering
- 黑马程序员——多线程
- Windows建立隐藏用户
- 黑马程序员——集合
- 黑马程序员——设计模式
- 黑马程序员——iO流(1)
- 黑马程序员——iO流(2)
- 黑马程序员——Css
- freemarker 数字格式化处理