#28061: 求救!我一直RE


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

School : No School
ID : 162766
IP address : [111.83.106.92]
Last Login :
2022-01-11 19:52:24
a013. 羅馬數字 -- NPSC 模擬試題 | From: [219.100.37.240] | Post Date : 2021-11-12 16:07

#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)
{
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;
}
else if(Rome_a[i] == 'I' && Rome_a[i+1] == 'X')
{
number_a += 9;
}
else if(Rome_a[i] == 'X' && Rome_a[i+1] == 'L')
{
number_a += 40;
}
else if(Rome_a[i] == 'X' && Rome_a[i+1] == 'C')
{
number_a += 90;
}
else if(Rome_a[i] == 'C' && Rome_a[i+1] == 'D')
{
number_a += 400;
}
else if(Rome_a[i] == 'C' && Rome_a[i+1] == 'M')
{
number_a += 900;
}
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;
}
else if(Rome_b[i] == 'I' && Rome_b[i+1] == 'X')
{
number_b += 9;
}
else if(Rome_b[i] == 'X' && Rome_b[i+1] == 'L')
{
number_b += 40;
}
else if(Rome_b[i] == 'X' && Rome_b[i+1] == 'C')
{
number_b += 90;
}
else if(Rome_b[i] == 'C' && Rome_b[i+1] == 'D')
{
number_b += 400;
}
else if(Rome_b[i] == 'C' && Rome_b[i+1] == 'M')
{
number_b += 900;
}
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];
}
}
}
}
}
 
#28065: Re:求救!我一直RE


cges30901 (cges30901)

School : No School
ID : 30877
IP address : [27.52.11.129]
Last Login :
2022-01-26 17:32:38
a013. 羅馬數字 -- NPSC 模擬試題 | From: [27.51.64.28] | Post Date : 2021-11-12 16:44

#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)
{
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;
}
else if(Rome_a[i] == 'I' && Rome_a[i+1] == 'X')
{
number_a += 9;
}
else if(Rome_a[i] == 'X' && Rome_a[i+1] == 'L')
{
number_a += 40;
}
else if(Rome_a[i] == 'X' && Rome_a[i+1] == 'C')
{
number_a += 90;
}
else if(Rome_a[i] == 'C' && Rome_a[i+1] == 'D')
{
number_a += 400;
}
else if(Rome_a[i] == 'C' && Rome_a[i+1] == 'M')
{
number_a += 900;
}
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;
}
else if(Rome_b[i] == 'I' && Rome_b[i+1] == 'X')
{
number_b += 9;
}
else if(Rome_b[i] == 'X' && Rome_b[i+1] == 'L')
{
number_b += 40;
}
else if(Rome_b[i] == 'X' && Rome_b[i+1] == 'C')
{
number_b += 90;
}
else if(Rome_b[i] == 'C' && Rome_b[i+1] == 'D')
{
number_b += 400;
}
else if(Rome_b[i] == 'C' && Rome_b[i+1] == 'M')
{
number_b += 900;
}
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];
}
}
}
}
}


i+1可能會超過字串的長度?

 
ZeroJudge Forum