a013.
羅馬數字
--
NPSC 模擬試題
| From: [61.217.34.135] |
發表日期
:
2012-01-13 01:35
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char array[]={'I','V','X','L','C','D','M'};
int array_1[]={1,5,10,50,100,500,1000};
int array2[]={900,400,90,40,9,4};
char* array3[]={"CM","CD","XC","XL","IX","IV"};
int count(char c[]);
void analyze(int x);
int change(char x)
{
int i=0;
while(array[i]!=NULL)
{
if(x==array[i])
return array_1[i];
i++;
}
}
int main(int argc, char* argv[])
{
char a[4000]={NULL},b[4000]={NULL};
int count_a,count_b;
scanf("%s",&a);
while(a[0]!='#' && b[0]!='#')
{
scanf("%s",&b);
if(strcmp(a,b)==0)
{
printf("ZERO\n");
}
else
{
count_a=count(a);
count_b=count(b);
if(count_a>count_b)
analyze(count_a-count_b);
else
analyze(count_b-count_a);
}
scanf("%s",&a);
}
return 0;
}
int count(char c[])
{
int i=0,j,sum=0;
while(c[i+1]!=NULL)
{
if(change(c[i])>=change(c[i+1]))
{
sum+=change(c[i]);
i++;
}
else
{
sum+=change(c[i+1])-change(c[i]);
i+=2;
}
}
if(c[i]!=NULL)
{
sum+=change(c[i]);
}
return sum;
}
void analyze(int x)
{
char array4[100];
int j=0;
int i;
while(x>=1000)
{
printf("M");
x-=1000;
}
for(i=0;i<=0;i++)
{
if(x>=array2[i])
{
printf("%s",array3[i]);
x-=array2[i];
break;
}
}
while(x>=500)
{
printf("D");
x-=500;
}
for(i=1;i<=1;i++)
{
if(x>=array2[i])
{
printf("%s",array3[i]);
x-=array2[i];
break;
}
}
while(x>=100)
{
printf("C");
x-=100;
}
for(i=2;i<=2;i++)
{
if(x>=array2[i])
{
printf("%s",array3[i]);
x-=array2[i];
break;
}
}
while(x>=50)
{
printf("L");
x-=50;
}
for(i=3;i<=3;i++)
{
if(x>=array2[i])
{
printf("%s",array3[i]);
x-=array2[i];
break;
}
}
while(x>=10)
{
printf("X");
x-=10;
}
for(i=4;i<=4;i++)
{
if(x>=array2[i])
{
printf("%s",array3[i]);
x-=array2[i];
break;
}
}
while(x>=5)
{
printf("V");
x-=5;
}
for(i=5;i<=5;i++)
{
if(x>=array2[i])
{
printf("%s",array3[i]);
x-=array2[i];
break;
}
}
while(x>0)
{
printf("I");
x--;
}
putchar('\n');
}