#21137: 秒數沒過。哈!


z0978627167@gmail.com (Albert)

學校 : 不指定學校
編號 : 72839
來源 : [36.238.112.111]
最後登入時間 :
2020-04-23 14:55:21
a013. 羅馬數字 -- NPSC 模擬試題 | From: [220.143.87.253] | 發表日期 : 2020-04-19 00:48

#include <iostream>

 

using namespace std;

 

int XXX(string in) {

int sum = 0;

for (size_t i = 0; i < in.size(); i++) {

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

sum += 4;

i++;

}

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

sum += 9;

i++;

}

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

sum += 40;

i++;

}

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

sum += 90;

i++;

}

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

sum += 400;

i++;

}

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

sum += 900;

i++;

}

else {

switch (in[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;

}

}

}

return sum;

};

 

void IVXLCDM(int SUM) {

string Str = "";

while (SUM != 0){

if (SUM >= 1000) {

Str += "M";

SUM -= 1000;

}

else if (SUM >= 900) {

Str += "CM";

SUM -= 900;

}

else if (SUM >= 500) {

Str += "D";

SUM -= 500;

}

else if (SUM >= 400) {

Str += "CD";

SUM -= 400;

}

else if (SUM >= 100) {

Str += "C";

SUM -= 100;

}

else if (SUM >= 90) {

Str += "XC";

SUM -= 90;

}

else if (SUM >= 50) {

Str += "L";

SUM -= 50;

}

else if (SUM >= 40) {

Str += "XL";

SUM -= 40;

}

else if (SUM >= 10) {

Str += "X";

SUM -= 10;

}

else if (SUM >= 9) {

Str += "IX";

SUM -= 9;

}

else if (SUM >= 5) {

Str += "V";

SUM -= 5;

}

else if (SUM >= 4) {

Str += "IV";

SUM -= 4;

}

else {

Str += "I";

SUM -= 1;

}

}

cout << Str << endl;

}

 

int main() {

string in1, in2;

int AA, BB, SUM;

while (cin >> in1 >> in2) {

if (in1 != "#" || in2 != "#") {

AA = XXX(in1);

BB = XXX(in2);

SUM = AA - BB;

if (SUM != 0) {

IVXLCDM(SUM);

}

else cout << "ZERO" << endl;

}

else break;

}

};

 
ZeroJudge Forum