p1465 Preface Numbering
阿新 • • 發佈:2018-11-08
用這個函式轉成羅馬數字統計就行了。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #definemkp make_pair using namespace std; const double EPS=1e-8; typedef long long lon; const lon SZ=30,INF=0x7FFFFFFF; string intToRoman(int num) { string c[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"}}; string roman; roman.append(c[3][num / 1000 % 10]); roman.append(c[2][num / 100 % 10]); roman.append(c[1][num / 10 % 10]); roman.append(c[0][num % 10]); return roman; }int main() { std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); lon casenum; //cin>>casenum; //for(lon time=1;time<=casenum;++time) { int n; cin>>n; map<char,int> mp; for(int i=1;i<=n;++i) { string tmp=intToRoman(i); for(int j=0;j<tmp.size();++j) { ++mp[tmp[j]]; } } if(mp['I'])cout<<'I'<<" "<<mp['I']<<endl; if(mp['V'])cout<<'V'<<" "<<mp['V']<<endl; if(mp['X'])cout<<'X'<<" "<<mp['X']<<endl; if(mp['L'])cout<<'L'<<" "<<mp['L']<<endl; if(mp['C'])cout<<'C'<<" "<<mp['C']<<endl; if(mp['D'])cout<<'D'<<" "<<mp['D']<<endl; if(mp['M'])cout<<'M'<<" "<<mp['M']<<endl; } return 0; }
string intToRoman(int num) {
string c[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"}};
string roman;
roman.append(c[3][num / 1000 % 10]);
roman.append(c[2][num / 100 % 10]);
roman.append(c[1][num / 10 % 10]);
roman.append(c[0][num % 10]);
return roman;
}