##############以下是程式碼###############
#include<iostream>
#include<string>
using namespace std;
int ch(char $input)
{
//cout << $input << "ch+" << endl;
if ($input=='I')
{return 1;}
else if ($input=='V'){return 5;}
else if ($input=='X'){return 10;}
else if ($input=='L'){return 50;}
else if ($input=='C'){return 100;}
else if ($input=='D'){return 500;}
else if ($input=='M'){return 1000;}
else
{return 0;}
}
int num(string $input)
{
int $ssun=0;
int $k;
for ($k=($input.size()-1);$k >= 0;$k--)
{
//cout << "+" ;
if ($k >=($input.size()-1))
{
$ssun=$ssun+ch($input[$k]);
}
else
{
if (ch($input[$k+1]) > ch($input[$k]))
{
$ssun=abs($ssun-ch($input[$k]));
}
else
{
$ssun=$ssun+ch($input[$k]);
}
}
}
//cout << $input << "num+" << endl;
return $ssun;
}
string say(int $input)
{
int $i,$k,$tmp;
int $gogoa[5]={10000,1000,100,10,1};
string $hahaa[5]={"","M","C","X","I"};
int $gogob[5]={5000,500,50,5,0};
string $hahab[5]={"","","D","L","V"};
if ($input == 0)
{
cout << "ZERO" ;
}
for ($i=1;$i<=4;$i++)
{
$tmp=($input / $gogoa[$i]);
if ($tmp > 0 && $tmp < 4 )
{
for ($k=1;$k<=$tmp;$k++)
{
cout << $hahaa[$i] ;
}
$input=$input-($gogoa[$i]*$tmp);
}
else if ($tmp == 4 )
{
cout << $hahaa[$i] << $hahab[$i] ;
$input=$input-($gogoa[$i]*4);
}
else if ($tmp >= 5 && $tmp < 9 )
{
cout << $hahab[$i] ;
for ($k=1;$k<=($tmp-5);$k++)
{
cout << $hahaa[$i] ;
}
$input=$input-($gogoa[$i]*$tmp);
}
else if ($tmp == 9 )
{
cout << $hahaa[$i] << $hahaa[$i-1] ;
$input=$input-($gogoa[$i]*9);
}
else
{
}
}
cout << endl;
}
int main()
{
string $a,$b;
int $na,$nb,$nab;
while ( cin >> $a && $a != "#")
{
cin >> $b;
//羅馬數字的讀法--->從右讀到左,比自己小則是減去,例如XLIV=50- 10- 1+5=44
//先轉數字
$na=num($a);
$nb=num($b);
$nab=abs($na-$nb);
//cout << $na << "-" << $nb << "=" << $nab << " is " ;
say($nab);
//cout << $a.size() << "+" << $b.size() << endl;
}
//system("pause");
return 0;
}
#####################################
我在DEV C++跑都OK
但是貼上來測反而都是RE
嗯嗯嗯,實在看不出來哪邊的index有超過
請大家幫幫忙!感嗯!
我是C++新手