#25834: 為啥這有問題


wwumin0137@gmail.com (齧齒)

School : 新北市立中和高級中學
ID : 157160
IP address : [1.161.50.218]
Last Login :
2021-08-29 15:52:51
a013. 羅馬數字 -- NPSC 模擬試題 | From: [1.164.37.2] | Post Date : 2021-06-26 15:18

#include<iostream>

using namespace std;

int romanTont(string s) {

int sum = 0;

for (int i = 0; i < s.size(); i++)

{

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

{

sum += 4;

i++;

}

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

{

sum += 9;

i++;

}

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

{

sum += 40;

i++;

}

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

{

sum += 90;

i++;

}

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

{

sum += 400;

i++;

}

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

{

sum += 900;

i++;

}

else

{

    switch (s[i])

{

case 'I':sum += 1; break;

case 'V':sum += 5; break;

case 'X':sum += 10; break;

case 'L':sum += 50; break;

case 'C':sum += 100; break;

case 'D':sum += 500; break;

case 'M':sum += 1000; break;

default:

break;

}

}

}

return sum;

}

int main(void)

{

string roma1 , roma2 ;

int n ;

while(roma1 !="#"){

cin >> roma1 >> roma2;

n = romanTont(roma1) - romanTont(roma2);

if(n==0)

cout << "ZERO"<<endl;

while(n>0)

{

if(n>=1000){

n = n - 1000;

cout << 'M';

}

else if(n>=900 &&n<1000){

n = n - 900;

cout << "CM";

}

else if(n>=500&&n<900){

n = n - 500;

cout << 'D';

}

else if(n>=400&&n<500){

n = n - 400;

cout << "CD";

}

else if(n>=100&&n<400){

n = n - 100;

cout << 'C';

}

else if(n>=90&&n<100){

n = n - 90;

cout << "XC";

}

else if(n>=50&&n<90){

n = n - 50;

cout << 'L';

}

else if(n>=40&&n<50){

n = n - 40;

cout << "XL";

}

else if(n>=10&&n<40){

n = n - 10;

cout << 'X';

}

else if(n==9){

n = n - 9;

cout << "IX";

}

else if(n>=5&&n<9){

n = n - 5;

cout << 'V';

}

else if(n==4){

n = n - 4;

cout << "IV";

}

else {

n--;

cout << 'I';

}

}  

cout <<endl;

}  

}

求幫助,我承認這方法挺複雜的~~~

 
ZeroJudge Forum