http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2325
题目大意:(如题)
输入输出:(如题)
解题思路:
1.用打表法将每个数N(1<=N<3500)中间“I”“V”“X”“L”“C”“D”“M”的个数统计出来,用一个二维数组cnt[3500][7]保存起来。
2.枚举。从千位开始枚举,一直枚举到个位为止,每次判断减掉那个数之后剩下的数是否还不小于0。如果不小于则继续,反之结束。
3.减小代码的方法。
(1) 10进制数到罗马数字的转换表:
string rec[4][9]={"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"};
(2) 字符到数组下标的转换表:
char res[7]={'I','V','X','L','C','D','M'};
核心代码:
for(mrk=1;mrk<3500;mrk++) { dat=mrk; for(i=3;i>=0;i--) { for(j=9;j>=1;j--) { tmp=pow((double)10,(double)i)*j; while(dat-tmp>=0) { dat-=tmp; for(k=0;k<rec[i][j-1].length();k++) { switch(rec[i][j-1][k]) { case 'I': cnt[mrk][0]++; break; case 'V': cnt[mrk][1]++; break; case 'X': cnt[mrk][2]++; break; case 'L': cnt[mrk][3]++; break; case 'C': cnt[mrk][4]++; break; case 'D': cnt[mrk][5]++; break; case 'M': cnt[mrk][6]++; break; default: break; } } } } } }
环境恶劣……给力……