1.將羅馬數字存到陣列,區分五跟十的倍數。
2.以五為分界,前四做前輸出,後四做後輸出。
3.以迴圈做對應輸出。
注意:陣列存的方式會改變輸出時的陣列編號。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string one[4] = { "I","X","C","M" };
string five[3] = { "V","L","D" };
string s;
int n;
cin >> n;
getline(cin, s);
for (int k = 0; k < n; k++)
{
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
int subNum = s[i] - '0';
int num = 0;
if (subNum < 5)
num = subNum % 4;
else if (subNum > 5)
num = (subNum - 5) % 4;
if (subNum >= 1 && subNum <= 4)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << five[s.length() - i - 1];
}
else
{
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
else if (subNum == 5)
cout << five[s.length() - i - 1];
else if (subNum >= 6 && subNum <= 9)
{
if (num == 0)
{
cout << one[s.length() - i - 1];
cout << one[s.length() - i];
}
else
{
cout << five[s.length() - i - 1];
for (int j = 0; j < num; j++)
cout << one[s.length() - i - 1];
}
}
}
cout << endl;
}
return 0;
}
沒有留言:
張貼留言