Http://acm.sdibt.edu.cn/JudgeOnline/problem.php? Id = 2325
Question: (such as question)
Input and Output: (such as the question)
Solution:
1. use the table-hitting method to convert each number N (1 <= n <3500) the number of "I", "V", "X", "L", "C", "D", and "M" counts, save it with a two-dimensional array CNT [3500] [7.
2. enumeration. Start from a thousand bits and start until a single bit. Check whether the remaining number is no less than 0 after the number is removed. If the value is not less than, continue, and vice versa.
3. Reduce the code.
(1) 10-digit conversion table to the number of Roman numerals:
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) character-to-array conversion table:
char res[7]={‘I‘,‘V‘,‘X‘,‘L‘,‘C‘,‘D‘,‘M‘};
Core code:
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; } } } } } }
Bad environment ...... Awesome ......
2.2.1 preface numbering