#27615: 給以後的我


40975026H (七賢)

School : 國立臺灣師範大學
ID : 167974
IP address : [140.122.53.33]
Last Login :
2022-01-07 15:53:28
a013. 羅馬數字 -- NPSC 模擬試題 | From: [140.122.19.69] | Post Date : 2021-10-19 17:10

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int main(){

char s[1000]={},S[1000]={};

memset(s, '\0', strlen(s));

memset(S, '\0', strlen(s));

long long int i=0 , n=0 , N=0 , A=0 ;

//羅馬數字轉數字 

while(scanf("%s",s)){

if(s[0]=='#'){

break;

}

A=0;

i=0;

n=0;

N=0;

scanf("%s",S);

for(i=0;i<strlen(s);i++){

if(i<strlen(s)-1){

if(s[i]=='I'&&s[i+1]=='V'){

n=n-2;

}

if(s[i]=='X'&&s[i+1]=='C'){

n=n-20;

}

if(s[i]=='I'&&s[i+1]=='X'){

n=n-2;

}

if(s[i]=='X'&&s[i+1]=='L'){

n=n-20;

}

if(s[i]=='C'&&s[i+1]=='D'){

n=n-200;

}

if(s[i]=='C'&&s[i+1]=='M'){

n=n-200;

}

}

if(s[i]=='I'){

n=n+1;

}

else if(s[i]=='V'){

n=n+5;

}

else if(s[i]=='X'){

n=n+10;

}

else if(s[i]=='L'){

n=n+50;

}

else if(s[i]=='C'){

n=n+100;

}

else if(s[i]=='D'){

n=n+500;

}

else if(s[i]=='M'){

n=n+1000;

}

}

for(i=0;i<strlen(S);i++){

if(i<strlen(S)-1){

if(S[i]=='I'&&S[i+1]=='V'){

N=N-2;

}

if(S[i]=='X'&&S[i+1]=='C'){

N=N-20;

}

if(S[i]=='I'&&S[i+1]=='X'){

N=N-2;

}

if(S[i]=='X'&&S[i+1]=='L'){

N=N-20;

}

if(S[i]=='C'&&S[i+1]=='D'){

N=N-200;

}

if(S[i]=='C'&&S[i+1]=='M'){

N=N-200;

}

}

 

if(S[i]=='I'){

N=N+1;

}

else if(S[i]=='V'){

N=N+5;

}

else if(S[i]=='X'){

N=N+10;

}

else if(S[i]=='L'){

N=N+50;

}

else if(S[i]=='C'){

N=N+100;

}

else if(S[i]=='D'){

N=N+500;

}

else if(S[i]=='M'){

N=N+1000;

}

}

i=0;

if(N>n){

A=N-n;

}

if(N<n){

A=n-N;

}

if(N==n){

printf("ZERO");

}

//數字轉羅馬數字

while(A>=1000){

if(A/1000>0){

printf("M");

A=A-1000;

}

}

while(A>=900){

if(A/900>0){

printf("CM");

A=A-900;

}

}

while(A>=500){

if(A/500>0){

printf("D");

A=A-500;

}

}

while(A>=400){

if(A/400>0){

printf("CD");

A=A-400;

}

}

while(A>=100){

if(A/100>0){

printf("C");

A=A-100;

}

}

while(A>=90){

if(A/90>0){

printf("XC");

A=A-90;

}

}

while(A>=50){

if(A/50>0){

printf("L");

A=A-50;

}

}

while(A>=40){

if(A/40>0){

printf("XL");

A=A-40;

}

}

while(A>=10){

if(A/10>0){

printf("X");

A=A-10;

}

}

while(A>=9){

if(A/9>0){

printf("IX");

A=A-9;

}

}

while(A>=5){

if(A/5>0){

printf("V");

A=A-5;

}

}

while(A>=4){

if(A/4>0){

printf("IV");

A=A-4;

}

}

while(A>=1){

if(A/1>0){

printf("I");

A=A-1;

}

}

memset(s, '\0', strlen(s));

memset(S, '\0', strlen(s));

printf("\n");

}

return 0;

}

 

 
ZeroJudge Forum