Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID:goharsh1
- TASK:preface
- LANG:C++
- */
- #include <fstream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- ifstream fin("preface.in");
- ofstream fout("preface.out");
- #define cin fin
- #define cout fout
- /*
- * Note: I didn't write the function "roman" , since it was a stupid repeating thing to do, I just googled it :D
- */
- void roman(char *s, unsigned n)
- /* Writes the Roman numeral representing n into the buffer s.
- Handles up to n = 3999. Since C doesn't have exceptions, n = 0
- causes the whole program to exit unsuccessfully. s should be
- have room for at least 16 characters, including the trailing
- null. */
- {if (n == 0)
- {puts("Roman numeral for zero requested.");
- exit(EXIT_FAILURE);}
- #define digit(loop, num, c) \
- loop (n >= num) \
- {*(s++) = c; \
- n -= num;}
- #define digits(loop, num, c1, c2) \
- loop (n >= num) \
- {*(s++) = c1; \
- *(s++) = c2; \
- n -= num;}
- digit ( while, 1000, 'M' )
- digits ( if, 900, 'C', 'M' )
- digit ( if, 500, 'D' )
- digits ( if, 400, 'C', 'D' )
- digit ( while, 100, 'C' )
- digits ( if, 90, 'X', 'C' )
- digit ( if, 50, 'L' )
- digits ( if, 40, 'X', 'L' )
- digit ( while, 10, 'X' )
- digits ( if, 9, 'I', 'X' )
- digit ( if, 5, 'V' )
- digits ( if, 4, 'I', 'V' )
- digit ( while, 1, 'I' )
- #undef digit
- #undef digits
- *s = 0;}
- int main(void)
- {
- char buffer[16];
- int n;
- cin>>n;
- int i;
- int all[7]={};
- for(i=1;i<=n;i++)
- {
- roman(buffer,i);
- int j;
- for(j=0;j<16&&buffer[j]!='\0';j++)
- {
- switch(buffer[j])
- {
- case 'I':
- all[0]++;
- break;
- case 'V':
- all[1]++;
- break;
- case 'X':
- all[2]++;
- break;
- case 'L':
- all[3]++;
- break;
- case 'C':
- all[4]++;
- break;
- case 'D':
- all[5]++;
- break;
- case 'M':
- all[6]++;
- break;
- }
- }
- }
- if(all[0]!=0)
- cout<<"I "<<all[0]<<endl;
- if(all[1]!=0)
- cout<<"V "<<all[1]<<endl;
- if(all[2]!=0)
- cout<<"X "<<all[2]<<endl;
- if(all[3]!=0)
- cout<<"L "<<all[3]<<endl;
- if(all[4]!=0)
- cout<<"C "<<all[4]<<endl;
- if(all[5]!=0)
- cout<<"D "<<all[5]<<endl;
- if(all[6]!=0)
- cout<<"M "<<all[6]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement