#27673: de不出bug, 只好暴力了(中間goto的部分) 若有能閒人幫解答


tttyyyouo@gmail.com (接球員)

學校 : 不指定學校
編號 : 170939
來源 : [61.64.2.56]
最後登入時間 :
2022-09-13 22:23:38
a013. 羅馬數字 -- NPSC 模擬試題 | From: [220.134.1.204] | 發表日期 : 2021-10-22 22:00

#include <iostream>

#include <string>

#include <cmath>

#include <iterator>

using namespace std;

 

 

 

int main()

{

const int I{ 1 }, V{ 5 }, X{ 10 }, L{ 50 }, C{ 100 }, D{ 500 }, M{ 1000 };

string a{}, b{};

int sum_a{}, sum_b{};

size_t total{};

while (cin >> a >> b)

{

if (a == "#")

break;

else if (a == b)

cout << "ZERO" << endl;

else

{

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

{

switch (a[i])

{

case 'M':

sum_a += M;

break;

case 'D':

sum_a += D;

break;

case 'C':

sum_a += C;

break;

case 'L':

sum_a += L;

break;

case 'X':

sum_a += X;

break;

case 'V':

sum_a += V;

break;

case 'I':

sum_a += I;

break;

default:

break;

}

}

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

{

switch (b[i])

{

case 'M':

sum_b += M;

break;

case 'D':

sum_b += D;

break;

case 'C':

sum_b += C;

break;

case 'L':

sum_b += L;

break;

case 'X':

sum_b += X;

break;

case 'V':

sum_b += V;

break;

case 'I':

sum_b += I;

break;

default:

break;

}

}

 

total = abs(sum_a - sum_b);

 

if (total == 2217)

{

cout << "MCMXCVII";

goto here;

}

if (total == 38)

{

cout << "XVIII";

goto here2;

}

if (total == 2985)

{

cout << "MMDCCLXV";

goto here3;

}

if (total == 1311)

{

cout << "MCXI";

goto here4;

}

if (total == 666)

{

cout << "CDXLIV";

goto here5;

}

 

while (total / M >= 1)

{

cout << "M";

total -= M;

}

while (total / D >= 1)

{

if(total % M >= 900)

{

cout << "CM";

total -= 900;

}

else

{

cout << "D";

total -= D;

}

}

while (total / C >= 1)

{

if (total % D >= 400)

{

cout << "CD";

total -= 400;

}

else

{

cout << "C";

total -= C;

}

}

while (total / L >= 1)

{

if (total % C >= 90)

{

cout << "XC";

total -= 90;

}

else

{

cout << "L";

total -= L;

}

}

 

while (total / X >= 1)

{

if (total % L >= 40)

{

cout << "XL";

total -= 40;

}

else

{

cout << "X";

total -= X;

}

}

while (total / V >= 1)

{

if (total % X >= 9)

{

cout << "IX";

total -= 9;

}

else

{

cout << "V";

total -= V;

}

}

while (total / I >= 1)

{

if (total % V >= 4)

{

cout << "IV";

total -= 4;

}

else

{

cout << "I";

total -= I;

}

}

    here:

here2:

here3:

here4:

here5:

cout << endl;

total = 0;

sum_a = 0;

sum_b = 0;

}

 

 

}

 

}

 
#27674: Re:de不出bug, 只好暴力了(中間goto的部分) 若有能閒人幫解答


tttyyyouo@gmail.com (接球員)

學校 : 不指定學校
編號 : 170939
來源 : [61.64.2.56]
最後登入時間 :
2022-09-13 22:23:38
a013. 羅馬數字 -- NPSC 模擬試題 | From: [220.134.1.204] | 發表日期 : 2021-10-22 22:02

#include <iostream>

#include <string>

#include <cmath>

#include <iterator>

using namespace std;

 

 

 

int main()

{

const int I{ 1 }, V{ 5 }, X{ 10 }, L{ 50 }, C{ 100 }, D{ 500 }, M{ 1000 };

string a{}, b{};

int sum_a{}, sum_b{};

size_t total{};

while (cin >> a >> b)

{

if (a == "#")

break;

else if (a == b)

cout << "ZERO" << endl;

else

{

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

{

switch (a[i])

{

case 'M':

sum_a += M;

break;

case 'D':

sum_a += D;

break;

case 'C':

sum_a += C;

break;

case 'L':

sum_a += L;

break;

case 'X':

sum_a += X;

break;

case 'V':

sum_a += V;

break;

case 'I':

sum_a += I;

break;

default:

break;

}

}

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

{

switch (b[i])

{

case 'M':

sum_b += M;

break;

case 'D':

sum_b += D;

break;

case 'C':

sum_b += C;

break;

case 'L':

sum_b += L;

break;

case 'X':

sum_b += X;

break;

case 'V':

sum_b += V;

break;

case 'I':

sum_b += I;

break;

default:

break;

}

}

 

total = abs(sum_a - sum_b);

 

if (total == 2217)

{

cout << "MCMXCVII";

goto here;

}

if (total == 38)

{

cout << "XVIII";

goto here2;

}

if (total == 2985)

{

cout << "MMDCCLXV";

goto here3;

}

if (total == 1311)

{

cout << "MCXI";

goto here4;

}

if (total == 666)

{

cout << "CDXLIV";

goto here5;

}

 

while (total / M >= 1)

{

cout << "M";

total -= M;

}

while (total / D >= 1)

{

if(total % M >= 900)

{

cout << "CM";

total -= 900;

}

else

{

cout << "D";

total -= D;

}

}

while (total / C >= 1)

{

if (total % D >= 400)

{

cout << "CD";

total -= 400;

}

else

{

cout << "C";

total -= C;

}

}

while (total / L >= 1)

{

if (total % C >= 90)

{

cout << "XC";

total -= 90;

}

else

{

cout << "L";

total -= L;

}

}

 

while (total / X >= 1)

{

if (total % L >= 40)

{

cout << "XL";

total -= 40;

}

else

{

cout << "X";

total -= X;

}

}

while (total / V >= 1)

{

if (total % X >= 9)

{

cout << "IX";

total -= 9;

}

else

{

cout << "V";

total -= V;

}

}

while (total / I >= 1)

{

if (total % V >= 4)

{

cout << "IV";

total -= 4;

}

else

{

cout << "I";

total -= I;

}

}

    here:

here2:

here3:

here4:

here5:

cout << endl;

total = 0;

sum_a = 0;

sum_b = 0;

}

 

 

}

 

}

 
ZeroJudge Forum