文章

1

粉丝

51

获赞

1

访问

551

头像
罗马数字 题解:
P1908 华东师范大学2022年机试
发布于2023年7月6日 16:46
阅读数 551

#include <iostream>
#include <vector>
using namespace std;

class Test0723 {
public:
vector<int> rome = {1, 5, 10, 50, 100, 500, 1000};
vector<int> decimal = {1, 1, 10, 10, 10, 100, 1000};
vector<string> romeString = {"I", "V", "X", "L", "C", "D", "M"};

string getRealValue(int num) {
int i = rome.size() - 1;
int count = 0;
string ret = "";
while (num > 0) {
count = num / rome[i];
int countD = num / decimal[i];

if (count > 0)
if (countD == 9 || countD == 4) {
if (i == 0) i++;
ret += romeString[i - 1] + (countD == 4 ? romeString[i] : romeString[i + 1]);
num = num - rome[i - 1] * countD;
} else {
for (int j = 0; j < count; j++) {
ret += romeString[i];
}
num = num - rome[i] * count;
}
i--;
}
return ret;
}
};

int main() {
int input=0;
cin>>input;

// "III" "IV" "IX" "LVIII" "MCMX...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发