题意:
给定整数N, 采用罗马数字的方式表达,统计每个罗马字符的个数,从小到大输出
解法:
枚举,统计每一个数字的字符数,然后输出
/* ID: lsswxr1 PROG: preface LANG: C++ */ #include <iostream> #include <vector> #include <map> #include <list> #include <set> #include <deque> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <cctype> #include <cstdio> #include <iomanip> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <fstream> using namespace std; #define USACO #ifdef USACO #define cin fin #define cout fout #endif ////////////////////////////////////////////////////////////////////////// ///宏定义 const int INF = 1000000000; const int MAXN = 40001; const int maxn = MAXN; ///全局变量 和 函数 int N; int counter[5000]; inline void cnt(int num, int tot) { if (tot != 0) { if (tot == 9) { counter[num * 10] += 1; counter[num] += 1; } else if (tot >= 5) { int v = tot / 5; int c = tot % 5; counter[num * 5] += v; counter[num] += c; } else if (tot == 4) { counter[num] += 1; counter[num * 5] += 1; } else { counter[num] += tot; } } } int ans[] = {1, 5, 10, 50, 100, 500, 1000}; //一共7个 char ansChar[] = {‘I‘, ‘V‘, ‘X‘, ‘L‘, ‘C‘, ‘D‘, ‘M‘}; int main() { #ifdef USACO ofstream fout ("preface.out"); ifstream fin ("preface.in"); #endif while (cin >> N) { memset(counter, 0, sizeof(counter)); for (int i = 1; i <= N; i++) { int temp = i; int thousond = temp / 1000; if (thousond != 0) { counter[1000] += thousond; } temp -= thousond * 1000; int hundred = temp / 100; temp -= 100 * hundred; cnt(100, hundred); int ten = temp / 10; temp -= 10 * ten; cnt(10, ten); cnt(1, temp); } for (int i = 0; i < 7; i++) { if (counter[ans[i]] == 0) { continue; } cout << ansChar[i] << " "; cout << counter[ans[i]]; cout << endl; } } ///结束 return 0; }