#11376: 在dev C++ 跟codechef皆AC但在這裡都WA 求幫找問題


karta0910489 (unknown)

學校 : 國立臺灣科技大學
編號 : 59669
來源 : [1.170.45.27]
最後登入時間 :
2017-07-17 13:37:34
a013. 羅馬數字 -- NPSC 模擬試題 | From: [140.118.232.16] | 發表日期 : 2016-09-23 00:41

如題

 

#include<iostream>
#include <cstring>
using namespace std;

int rtn(char a[20]);
char ntr(int n);

int main(){
char a[20],b[20];
int n;
while(cin>>a){
if(a[0]=='#'){
break;
}
else{
cin>>b;
n=rtn(a)-rtn(b);
if(n==0){
cout<<"ZERO"<<endl;
}
else{
ntr(n);
}
}
}
}

int rtn(char a[20]){
int n=0;
for(int i=0;i<strlen(a);i++){
if(a[i]=='I'){
if(a[i+1]=='V'){
n+=4;
}
if(a[i+1]=='X'){
n+= 9;
}
else{
n+=1;
}
}
if(a[i]=='V'){
n+=5;
}
if(a[i]=='X'){
if(a[i+1]=='L'){
n+=40;
}
if(a[i+1]=='C'){
n+=90;
}
else{
n+=10;
}
}
if(a[i]=='L'){
n+=50;
}
if(a[i]=='C'){
if(a[i+1]=='D'){
n+=400;
}
if(a[i+1]=='M'){
n+=900;
}
else{
n+=100;
}
}
if(a[i]=='D'){
n+=500;
}
if(a[i]=='M'){
n+=1000;
}
}
return n;
}
char ntr(int n){
int m,count=0;
char a[30];
while(n>0){
if(n>=1000){
m=n/1000;
n%=1000;
for(int i=0;i<m;i++){
a[i]='M';
}
count+=m;
}
else if(n>=100){
m=n/100;
n%=100;
switch (m){
case 1:
a[count]='C';
break;
case 2:
a[count]='C';
a[count+1]='C';
break;
case 3:
a[count]='C';
a[count+1]='C';
a[count+2]='C';
break;
case 4:
a[count]='C';
a[count+1]='D';
m=2;
break;
case 5:
a[count]='D';
m=1;
break;
case 6:
a[count]='D';
a[count+1]='C';
m=2;
break;
case 7:
a[count]='D';
a[count+1]='C';
a[count+2]='C';
m=3;
break;
case 8:
a[count]='D';
a[count+1]='C';
a[count+2]='C';
a[count+3]='C';
m=4;
break;
case 9:
a[count]='C';
a[count+1]='M';
m=2;
break;
}
count+=m;
}
else if(n>=10){ //有bug
m=n/10;
n%=10;
switch (m){
case 1:
a[count]='X';
break;
case 2:
a[count]='X';
a[count+1]='X';
break;
case 3:
a[count]='X';
a[count+1]='X';
a[count+2]='X';
break;
case 4:
a[count]='X';
a[count+1]='V';
m=2;
break;
case 5:
a[count]='V';
m=1;
break;
case 6:
a[count]='V';
a[count+1]='X';
m=2;
break;
case 7:
a[count]='V';
a[count+1]='X';
a[count+2]='X';
m=3;
break;
case 8:
a[count]='V';
a[count+1]='X';
a[count+2]='X';
a[count+3]='X';
m=4;
break;
case 9:
a[count]='X';
a[count+1]='C';
m=2;
break;
}
count+=m;
}
else if(n>=1){
m=n;
n=0;
switch (m){
case 1:
a[count]='I';
break;
case 2:
a[count]='I';
a[count+1]='I';
break;
case 3:
a[count]='I';
a[count+1]='I';
a[count+2]='I';
break;
case 4:
a[count]='I';
a[count+1]='V';
m=2;
break;
case 5:
a[count]='V';
m=1;
break;
case 6:
a[count]='V';
a[count+1]='I';
m=2;
break;
case 7:
a[count]='V';
a[count+1]='I';
a[count+2]='I';
m=3;
break;
case 8:
a[count]='V';
a[count+1]='I';
a[count+2]='I';
a[count+3]='I';
m=4;
break;
case 9:
a[count]='I';
a[count+1]='C';
m=2;
break;
}
count+=m;
}
}
cout<<a<<endl;
}

 
ZeroJudge Forum