1. 程式人生 > >Preface Numbering

Preface Numbering

continue 技術分享 stdout end cnblogs tar targe 數字 namespace

鏈接

分析:先打表需要用到的羅馬數字,然後暴力轉換,最後統計一下即可

技術分享
 1 /*
 2     PROB:preface
 3     ID:wanghan
 4     LANG:C++
 5 */
 6 #include "iostream"
 7 #include "cstdio"
 8 #include "cstring"
 9 #include "string"
10 #include "map"
11 using namespace std;
12 const int maxn=4000;
13 char s[]={I,V,X,L,C,D,M};
14 int  h[]={1
,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000}; 15 int n; 16 struct Node{ 17 int x; 18 string str; 19 }; 20 Node p[maxn]; 21 map<int,string> mp; 22 void init(){ 23 mp[1]="I",mp[2]="II",mp[3]="III",mp[4]="IV",mp[5]="V",mp[6]="VI",mp[7]="VII",mp[8]="VIII",mp[9]="IX"; 24 mp[10
]="X",mp[20]="XX",mp[30]="XXX",mp[40]="XL",mp[50]="L",mp[60]="LX",mp[70]="LXX",mp[80]="LXXX",mp[90]="XC"; 25 mp[100]="C",mp[200]="CC",mp[300]="CCC",mp[400]="CD",mp[500]="D",mp[600]="DC",mp[700]="DCC",mp[800]="DCCC",mp[900]="CM"; 26 mp[1000]="M",mp[2000]="MM",mp[3000]="MMM"; 27 } 28 string solve(int num){ 29 string res="";
30 while(num){ 31 int pos=-1; 32 for(int i=29;i>=0;i--){ 33 if(num>=h[i]){ 34 pos=i; break; 35 } 36 } 37 int mod=num/h[pos]; 38 for(int i=1;i<=mod;i++){ 39 num-=h[pos]; 40 res+=mp[h[pos]]; 41 } 42 //num-=h[pos]; 43 //res+=mp[h[pos]]; 44 } 45 return res; 46 } 47 map<char,int>mr; 48 int main() 49 { 50 freopen("preface.in","r",stdin); 51 freopen("preface.out","w",stdout); 52 cin>>n; 53 init(); 54 for(int i=1;i<=n;i++){ 55 p[i].x=i; 56 p[i].str=solve(i); 57 //cout<<i<<": "; 58 //cout<<p[i].str<<endl; 59 } 60 for(int i=1;i<=n;i++){ 61 for(int j=0;j<p[i].str.length();j++){ 62 mr[p[i].str[j]]++; 63 } 64 } 65 for(int i=0;i<7;i++){ 66 if(mr[s[i]]==0) continue; 67 else{ 68 cout<<s[i]<<" "<<mr[s[i]]<<endl; 69 } 70 } 71 return 0; 72 }
View Code

Preface Numbering