a013.
羅馬數字
--
NPSC 模擬試題
| From: [210.70.219.18] |
發表日期
:
2013-09-30 04:33
在偵錯模式下OK 執行下失敗
#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
int str2int(string str){
int tmp = 0;
int i = 0;
int level = 0;
for (i = 0 ; i < 40 ; i++){
switch (str[i]){
case 'M':
if (level <= 1){
tmp = tmp + 1000;
level = 1;
}else{
tmp = tmp + 800;
}
break;
case 'D':
if (level <= 2){
tmp = tmp + 500;
level = 2;
}else{
tmp = tmp + 300;
}
break;
case 'C':
if (level <= 3){
tmp = tmp + 100;
level = 3;
}else{
tmp = tmp + 80;
}
break;
case 'L':
if (level <= 4){
tmp = tmp + 50;
level = 4;
}else{
tmp = tmp + 30;
}
break;
case 'X':
if (level <= 5){
tmp = tmp + 10;
level = 5;
}else{
tmp = tmp + 8;
}
break;
case 'V':
if (level <= 6){
tmp = tmp + 5;
level = 6;
}else{
tmp = tmp + 3;
}
break;
case 'I':
tmp = tmp + 1;
level = 7;
break;
default:break;
}
}
return tmp;
}
string int2str(int tmp){
string str = "";
if(tmp == 0){
str = "ZERO";
}
while(tmp != 0){
if (tmp >= 1000){
tmp -= 1000;
str += "M";
}else if(tmp >= 900){
tmp -= 900;
str += "CM";
}else if(tmp >= 500){
tmp -= 500;
str += "D";
}else if(tmp >= 400){
tmp -= 400;
str += "CD";
}else if(tmp >= 100){
tmp -= 100;
str += "C";
}else if(tmp >= 90){
tmp -= 90;
str += "XC";
}else if(tmp >= 50){
tmp -= 50;
str += "L";
}else if(tmp >= 40){
tmp -= 40;
str += "XL";
}else if(tmp >= 10){
tmp -= 10;
str += "X";
}else if(tmp >= 9){
tmp -= 9;
str += "IX";
}else if(tmp >= 5){
tmp -= 5;
str += "V";
}else if(tmp >= 4){
tmp -= 4;
str += "IV";
}else if(tmp >= 1){
tmp -= 1;
str += "I";
}
}
return str;
}
int main() {
char strinput[40];
char strinputb[40];
int i = 0;
int result = 0;
string out = "";
for (i = 0 ; i <= 40 ; i++){
strinput[i] = 0;
strinputb[i] = 0;
}
while(cin >> strinput >> strinputb){
if (strinput != "#" ){
result = str2int(strinput) - str2int(strinputb);
if (result < 0){
result = result * -1;
}
//cout << result << endl;
out = int2str(result);
cout << out << endl;
for (i = 0 ; i <= 40 ; i++){
strinput[i] = 0;
strinputb[i] = 0;
}
}else{
break;
}
}
return 0;
}