题目大意:
给出一个整数n,记录[1,n]范围内所有整数的罗马数字表示形式下罗马字母出现的次数。
思路:
简单模拟,思路见代码。
代码:
/* ID: lujunda1 LANG: C++ PROG: preface */ #include<iostream> #include<stdio.h> #include<map> using namespace std; map<char,int> mark; int check(int n) { if(n>=1000) mark['M']+=n/1000; n%=1000; if(n>=100&&n<400) mark['C']+=n/100; else if(n>=400&&n<500) { mark['D']++; mark['C']++; } else if(n>=500&&n<900) { mark['D']++; mark['C']+=(n-500)/100; } else if(n>=900&&n<1000) { mark['M']++; mark['C']++; } n%=100; if(n>=10&&n<40) mark['X']+=n/10; else if(n>=40&&n<50) { mark['L']++; mark['X']++; } else if(n>=50&&n<90) { mark['L']++; mark['X']+=(n-50)/10; } else if(n>=90&&n<100) { mark['C']++; mark['X']++; } n%=10; if(n>=1&&n<4) mark['I']+=n; else if(n>=4&&n<5) { mark['V']++; mark['I']++; } else if(n>=5&&n<9) { mark['V']++; mark['I']+=n-5; } else if(n>=9&&n<10) { mark['X']++; mark['I']++; } } int main() { freopen("preface.in","r",stdin); freopen("preface.out","w",stdout); int n; scanf("%d",&n); for(int i=1;i<=n;i++) check(i); if(mark['I']) printf("I %d\n",mark['I']); if(mark['V']) printf("V %d\n",mark['V']); if(mark['X']) printf("X %d\n",mark['X']); if(mark['L']) printf("L %d\n",mark['L']); if(mark['C']) printf("C %d\n",mark['C']); if(mark['D']) printf("D %d\n",mark['D']); if(mark['M']) printf("M %d\n",mark['M']); }