summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2.2/preface.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/2.2/preface.c b/2.2/preface.c
new file mode 100644
index 0000000..4da1bef
--- /dev/null
+++ b/2.2/preface.c
@@ -0,0 +1,91 @@
+/*
+ID: mytbk921
+LANG: C
+TASK: preface
+*/
+
+#include <stdio.h>
+#include <string.h>
+
+struct Num
+{
+ char str[20];
+};
+
+struct Num numbers[10000];
+
+void init_numbers()
+{
+#define SETSTR(n, c) numbers[n].str[0]=c;numbers[n].str[1]=0;
+ int base, i, j, r;
+
+ for (i=0; i<10000; i++)
+ numbers[i].str[0] = 0;
+
+ SETSTR(1, 'I');
+ SETSTR(5, 'V');
+ SETSTR(10, 'X');
+ SETSTR(50, 'L');
+ SETSTR(100, 'C');
+ SETSTR(500, 'D');
+ SETSTR(1000, 'M');
+
+ for (base=1; base<1000; base*=10) {
+ for (i=2; i<4; i++) {
+ strcpy(numbers[base*i].str, numbers[base*(i-1)].str);
+ strcat(numbers[base*i].str, numbers[base].str);
+ }
+ strcpy(numbers[base*4].str, numbers[base].str);
+ strcat(numbers[base*4].str, numbers[base*5].str);
+ for (i=6; i<9; i++) {
+ strcpy(numbers[base*i].str, numbers[base*(i-1)].str);
+ strcat(numbers[base*i].str, numbers[base].str);
+ }
+ strcpy(numbers[base*9].str, numbers[base].str);
+ strcat(numbers[base*9].str, numbers[base*10].str);
+ }
+ strcpy(numbers[2000].str, "MM");
+ strcpy(numbers[3000].str, "MMM");
+
+ for (i=11; i<=3500; i++) {
+ if (numbers[i].str[0])
+ continue;
+ r = i;
+ for (base=1000; base>0; base/=10) {
+ if (r >= base) {
+ j = r/base;
+ strcat(numbers[i].str, numbers[j*base].str);
+ r = r%base;
+ }
+ }
+ }
+}
+
+
+int main()
+{
+ int n, i, j;
+ FILE *fin, *fout;
+ int nchars[26];
+ char *romans = "IVXLCDM";
+
+ init_numbers();
+
+ fin = fopen("preface.in", "r");
+ fout = fopen("preface.out", "w");
+
+ fscanf(fin, "%d", &n);
+ fclose(fin);
+
+ memset(nchars, 0, sizeof(nchars));
+ for (i=1; i<=n; i++) {
+ for (j=0; numbers[i].str[j]; j++)
+ nchars[numbers[i].str[j]-'A']++;
+ }
+ for (i=0; romans[i]; i++) {
+ if (nchars[romans[i]-'A'])
+ fprintf(fout, "%c %d\n", romans[i], nchars[romans[i]-'A']);
+ }
+ fclose(fout);
+ return 0;
+}