#28266: RE能不能幫我看一下哪裡有問題?測試都正常


u010731@tcivs.tc.edu.tw (超級不會寫程式)

學校 : 不指定學校
編號 : 162766
來源 : [111.83.106.92]
最後登入時間 :
2022-01-11 19:52:24
a013. 羅馬數字 -- NPSC 模擬試題 | From: [219.100.37.246] | 發表日期 : 2021-11-23 15:10

#include <iostream>
#include<string>
using namespace std;
string Rome,Rome_a,Rome_b;
int Jump_out = 1,number,number_a,number_b,X;

int main() {
while(Jump_out == 1)
{
Rome_a.clear();
Rome_b.clear();
Rome.clear();
X = 0 ;
while(cin >> Rome_a)
{
if(Rome_a == "#")
{
Jump_out = 0;
break;
}
else
{
number_a = 0;
for(int i = 0;i < Rome_a.length();i++)
{
if(Rome_a[i] == 'I' && Rome_a[i+1] == 'V')
{
number_a += 4;
i += 1;
}
else if(Rome_a[i] == 'I' && Rome_a[i+1] == 'X')
{
number_a += 9;
i += 1;
}
else if(Rome_a[i] == 'X' && Rome_a[i+1] == 'L')
{
number_a += 40;
i += 1;
}
else if(Rome_a[i] == 'X' && Rome_a[i+1] == 'C')
{
number_a += 90;
i += 1;
}
else if(Rome_a[i] == 'C' && Rome_a[i+1] == 'D')
{
number_a += 400;
i += 1;
}
else if(Rome_a[i] == 'C' && Rome_a[i+1] == 'M')
{
number_a += 900;
i += 1;
}
else
{
switch(Rome_a[i])
{
case 'I': number_a += 1;
break;
case 'V': number_a += 5;
break;
case 'X': number_a += 10;
break;
case 'L': number_a += 50;
break;
case 'C': number_a += 100;
break;
case 'D': number_a += 500;
break;
case 'M': number_a += 1000;
break;
}
}
}
}
break;
}
if (Jump_out == 1)
{
while(cin >> Rome_b)
{
number_b = 0;
for(int i = 0;i < Rome_b.length();i++)
{
if(Rome_b[i] == 'I' && Rome_b[i+1] == 'V')
{
number_b += 4;
i += 1;
}
else if(Rome_b[i] == 'I' && Rome_b[i+1] == 'X')
{
number_b += 9;
i += 1;
}
else if(Rome_b[i] == 'X' && Rome_b[i+1] == 'L')
{
number_b += 40;
i += 1;
}
else if(Rome_b[i] == 'X' && Rome_b[i+1] == 'C')
{
number_b += 90;
i += 1;
}
else if(Rome_b[i] == 'C' && Rome_b[i+1] == 'D')
{
number_b += 400;
i += 1;
}
else if(Rome_b[i] == 'C' && Rome_b[i+1] == 'M')
{
number_b += 900;
i += 1;
}
else
{
switch(Rome_b[i])
{
case 'I': number_b += 1;
break;
case 'V': number_b += 5;
break;
case 'X': number_b += 10;
break;
case 'L': number_b += 50;
break;
case 'C': number_b += 100;
break;
case 'D': number_b += 500;
break;
case 'M': number_b += 1000;
break;
}
}
}
break;
}
number = number_a - number_b;
if (number == 0)
{
cout << "ZERO" << endl;
}
else
{
while(number != 0)
{
if (number >= 1000)
{
Rome[X] = 'M';
number -= 1000;
}
else if (number >= 500)
{
if (number >= 900)
{
Rome[X] = 'C';
Rome[X+1] = 'M';
number -= 900;
X += 1;
}
else
{
Rome[X] = 'D';
number -= 500;
}
}
else if (number >= 100)
{
if (number >= 400)
{
Rome[X] = 'C';
Rome[X+1] = 'D';
number -= 400;
X += 1;
}
else
{
Rome[X] = 'C';
number -= 100;
}
}
else if (number >= 50)
{
if (number >= 90)
{
Rome[X] = 'X';
Rome[X+1] = 'C';
number -= 90;
X += 1;
}
else
{
Rome[X] = 'L';
number -= 50;
}
}
else if (number >= 10)
{
if (number >= 40)
{
Rome[X] = 'X';
Rome[X+1] = 'L';
number -= 40;
X += 1;
}
else
{
Rome[X] = 'X';
number -= 10;
}
}
else if (number >= 5)
{
if (number == 9)
{
Rome[X] = 'I';
Rome[X+1] = 'X';
number -= 9;
X += 1;
}
else
{
Rome[X] = 'V';
number -= 5;
}
}
else
{
if (number == 4)
{
Rome[X] = 'I';
Rome[X+1] = 'V';
number -= 4;
X += 1;
}
else
{
Rome[X] = 'I';
number -= 1;
}
}
X += 1;
}
for (int i = 0;i < X;i++)
{
cout << Rome[i];
}
cout << endl;
}
}
}
}
 
#28270: Re:RE能不能幫我看一下哪裡有問題?測試都正常


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [39.9.74.255]
最後登入時間 :
2024-10-14 22:20:08
a013. 羅馬數字 -- NPSC 模擬試題 | From: [39.9.43.177] | 發表日期 : 2021-11-23 19:35

number = number_a - number_b;


你沒有考慮到number_b大於number_a的情況,導致字串越來越長

(另外你把string當成陣列這種用法我覺得很奇怪,雖然剛好可以AC,但我會用Rome+=這樣子寫比較簡單)

 
#28859: Re:RE能不能幫我看一下哪裡有問題?測試都正常


u010731@tcivs.tc.edu.tw (超級不會寫程式)

學校 : 不指定學校
編號 : 162766
來源 : [111.83.106.92]
最後登入時間 :
2022-01-11 19:52:24
a013. 羅馬數字 -- NPSC 模擬試題 | From: [111.83.103.159] | 發表日期 : 2022-01-08 12:33

number = number_a - number_b;


你沒有考慮到number_b大於number_a的情況,導致字串越來越長

(另外你把string當成陣列這種用法我覺得很奇怪,雖然剛好可以AC,但我會用Rome+=這樣子寫比較簡單)

謝謝你,非常感謝。我把number = number_a - number_b加上abs就解決了,這個細節我還真的沒注意到!

 
ZeroJudge Forum