The problem algorithm is not difficult, it is difficult to read test instructions, meaning that the number from 1 to n is converted to Roman numerals, and then count the number of occurrences of various letters in all numbers
For each number, the greedy method is converted to Roman numerals, and then the statistics are good.
/*id:modengd1prog:prefacelang:c++*/#include <iostream> #include <stdio.h> #include <memory.h># Include <string> #include <cstring>using namespace std;string st[15]={"M", "CM", "D", "CD", "C", "XC", "L", "XL "," X "," IX "," V "," IV "," I "};int value[15]={1000,900,500,400, 100,90,50,40, 9,5,4,1};int Counter[7];char output[10]= {' I ', ' V ', ' X ', ' L ', ' C ', ' D ', ' M '};void Count (string S) {for (int i=0;i<s.size (); i++) {switch (S[i]) { Case ' I ': counter[0]++;break; Case ' V ': counter[1]++;break; Case ' X ': counter[2]++;break; Case ' L ': counter[3]++;break; Case ' C ': counter[4]++;break; Case ' D ': counter[5]++;break; Case ' M ': counter[6]++;break; }}}string Change (int x) {string S; while (x>0) {for (int j=0;j<15;j++) {if (X>=value[j]) {X-=VALUE[J]; for (int k=0;k<st[j].size (); k++) S.push_back (St[j][k]); Break }}} return s;} int main () {freopen ("preface.in", "R", stdin); Freopen ("Preface.out", "w", stdout); int N; memset (counter,0,sizeof (counter)); scanf ("%d", &n); for (int. i=1;i<=n;i++) {Count (change (i)); } for (int i=0;i<7;i++) if (Counter[i]) cout<<output[i]<< "<<counter[i]<<en dl return 0;}
Usaco Preface numbering