随笔-141  评论-9  文章-3  trackbacks-0

/*
ID: lorelei3
TASK: preface
LANG: C
*/


#include 
<stdio.h>
#include 
<stdlib.h>
#include 
<string.h>
#include 
<assert.h>
 
static char *encode[] = {
    
"""I""II""III""IV",
    
"V""VI""VII""VIII""IX",
}
;
 
char*
romandigit(
int d, char *ivx)
{
    
char *s, *p;
    
static char str[10];
 
    
for(s=encode[d%10], p=str; *s; s++, p++{
        
switch(*s){
        
case 'I':
            
*= ivx[0];
            
break;
        
case 'V':
            
*= ivx[1];
            
break;
        
case 'X':
            
*= ivx[2];
            
break;
        }

    }

    
*= '\0';
    
return str;
}

 
char*
roman(
int n)
{
    
static char buf[20];
 
    strcpy(buf, 
"");
    strcat(buf, romandigit(n
/1000"M"));
    strcat(buf, romandigit(n
/100,  "CDM"));
    strcat(buf, romandigit(n
/10,   "XLC"));
    strcat(buf, romandigit(n,      
"IVX"));
    
return buf;
}

 
int 
main(
void)
{
    FILE 
*fin, *fout;
    
int i, n;
    
char *s;
    
int count[256];
 
    fin 
= fopen("preface.in""r");
    fout 
= fopen("preface.out""w");
    assert(fin 
!= NULL && fout != NULL);
 
    fscanf(fin, 
"%d"&n);
 
    
for(s="IVXLCDM"*s; s++)
        count[
*s] = 0;
 
    
for(i=1; i<=n; i++)
        
for(s=roman(i); *s; s++)
            count[
*s]++;
 
    
for(s="IVXLCDM"*s; s++)
        
if(count[*s])
            fprintf(fout, 
"%c %d\n"*s, count[*s]);
 
    
return 0;
}
posted on 2010-11-21 00:47 小阮 阅读(144) 评论(0)  编辑 收藏 引用 所属分类: USACO

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理