Preface Numbering chapter 2.2
题目总是看不懂,后来跑nocow上看翻译,虽然了解意思了,却被这几个规则搞的不知道从何下手,
想着数字到底怎么变成这些字母,后来一看nocow题解第一个...直接把个十百千位枚举然后组合到一起便可...突然感觉自己智商有点捉鸡→_→
1 /* 2 3 ID: hubiao cave 4 5 PROG: preface 6 7 LANG: C++ 8 9 */ 10 11 12 13 14 #include<iostream> 15 16 #include<fstream> 17 18 #include<string> 19 20 using namespace std; 21 22 23 string buf[4][10]={{"","I","II","III","IV","V","VI","VII","VIII","IX"},//个 24 {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},//十 25 {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},//百 26 {"","M","MM","MMM","","","","","",""}}; 27 28 29 int number; 30 31 int cou[8]; 32 33 void work(string); 34 int main() 35 36 { 37 38 39 40 41 ifstream fin("preface.in"); 42 43 ofstream fout("preface.out"); 44 fin>>number; 45 for(int i=1;i<=number;i++) 46 { 47 int m=0; 48 int t=i; 49 while(t/10) 50 { 51 string str=buf[m][t%10]; 52 work(str); 53 t/=10; 54 m++; 55 } 56 string str=buf[m][t%10]; 57 work(str); 58 59 } 60 61 for(int i=1;i<=7;++i) 62 { 63 if(cou[i]==0) 64 continue; 65 switch(i) 66 { 67 case 1: 68 fout<<"I "<<cou[i]<<endl; 69 break; 70 case 2: 71 fout<<"V "<<cou[i]<<endl; 72 break; 73 case 3: 74 fout<<"X "<<cou[i]<<endl; 75 break; 76 case 4: 77 fout<<"L "<<cou[i]<<endl; 78 break; 79 case 5: 80 fout<<"C "<<cou[i]<<endl; 81 break; 82 case 6: 83 fout<<"D "<<cou[i]<<endl; 84 break; 85 case 7: 86 fout<<"M "<<cou[i]<<endl; 87 break; 88 } 89 } 90 91 92 93 return 0; 94 95 96 } 97 98 void work(string str) 99 { 100 for(int i=0;i<str.length();i++) 101 { 102 switch(str[i]) 103 { 104 case 'I': 105 cou[1]++; 106 break; 107 108 case 'V': 109 cou[2]++; 110 break; 111 112 case 'X': 113 cou[3]++; 114 break; 115 116 case 'L': 117 cou[4]++; 118 break; 119 120 case 'C': 121 cou[5]++; 122 break; 123 124 case 'D': 125 cou[6]++; 126 break; 127 128 case 'M': 129 cou[7]++; 130 break; 131 } 132 } 133 }