Advertisement
aryobarzan

USACO preface

Jun 14th, 2011
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. /*
  2. ID:goharsh1
  3. TASK:preface
  4. LANG:C++
  5. */
  6. #include <fstream>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. ifstream fin("preface.in");
  13. ofstream fout("preface.out");
  14.  
  15. #define cin fin
  16. #define cout fout
  17.  
  18.  
  19. /*
  20.  * Note: I didn't write the function "roman" , since it was a stupid repeating thing to do, I just googled it :D
  21.  */  
  22. void roman(char *s, unsigned n)
  23. /* Writes the Roman numeral representing n into the buffer s.
  24. Handles up to n = 3999. Since C doesn't have exceptions, n = 0
  25. causes the whole program to exit unsuccessfully. s should be
  26. have room for at least 16 characters, including the trailing
  27. null. */
  28.  {if (n == 0)
  29.      {puts("Roman numeral for zero requested.");
  30.       exit(EXIT_FAILURE);}
  31.  
  32.   #define digit(loop, num, c) \
  33.       loop (n >= num)         \
  34.          {*(s++) = c;         \
  35.           n -= num;}  
  36.   #define digits(loop, num, c1, c2) \
  37.       loop (n >= num)               \
  38.          {*(s++) = c1;              \
  39.           *(s++) = c2;              \
  40.           n -= num;}
  41.  
  42.   digit  ( while, 1000, 'M'      )
  43.   digits ( if,     900, 'C', 'M' )
  44.   digit  ( if,     500, 'D'      )
  45.   digits ( if,     400, 'C', 'D' )
  46.   digit  ( while,  100, 'C'      )
  47.   digits ( if,      90, 'X', 'C' )
  48.   digit  ( if,      50, 'L'      )
  49.   digits ( if,      40, 'X', 'L' )
  50.   digit  ( while,   10, 'X'      )
  51.   digits ( if,       9, 'I', 'X' )
  52.   digit  ( if,       5, 'V'      )
  53.   digits ( if,       4, 'I', 'V' )
  54.   digit  ( while,    1, 'I'      )
  55.  
  56.   #undef digit
  57.   #undef digits
  58.  
  59.   *s = 0;}
  60.  
  61. int main(void)
  62.   {
  63.     char buffer[16];
  64.     int n;
  65.     cin>>n;
  66.     int i;
  67.     int all[7]={};
  68.     for(i=1;i<=n;i++)
  69.     {
  70.         roman(buffer,i);
  71.         int j;
  72.         for(j=0;j<16&&buffer[j]!='\0';j++)
  73.         {
  74.             switch(buffer[j])
  75.             {
  76.                 case 'I':
  77.                     all[0]++;
  78.                     break;
  79.                 case 'V':
  80.                     all[1]++;
  81.                     break;
  82.                 case 'X':
  83.                     all[2]++;
  84.                     break;
  85.                 case 'L':
  86.                     all[3]++;
  87.                     break;
  88.                 case 'C':
  89.                     all[4]++;
  90.                     break;
  91.                 case 'D':
  92.                     all[5]++;
  93.                     break;
  94.                 case 'M':
  95.                     all[6]++;
  96.                     break;
  97.                
  98.             }
  99.         }
  100.     }
  101.    
  102.     if(all[0]!=0)
  103.         cout<<"I "<<all[0]<<endl;
  104.     if(all[1]!=0)
  105.         cout<<"V "<<all[1]<<endl;
  106.     if(all[2]!=0)
  107.         cout<<"X "<<all[2]<<endl;
  108.     if(all[3]!=0)
  109.         cout<<"L "<<all[3]<<endl;
  110.     if(all[4]!=0)
  111.         cout<<"C "<<all[4]<<endl;
  112.     if(all[5]!=0)
  113.         cout<<"D "<<all[5]<<endl;
  114.     if(all[6]!=0)
  115.         cout<<"M "<<all[6]<<endl;
  116.    
  117.     return 0;
  118.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement