#7151: OLE(line:26)冗餘輸出


caferange (細菌)

學校 : 國防大學理工學院
編號 : 28362
來源 : [49.219.102.111]
最後登入時間 :
2016-03-10 20:57:41
a013. 羅馬數字 -- NPSC 模擬試題 | From: [140.132.49.17] | 發表日期 : 2012-11-04 21:37

 -------------測資出現

OLE (line:26)
冗餘輸出

請勿輸出題目未要求的文字:  MMIII
---------------
我想運算大概沒有問題(因為都到第26個測資)
但是不懂到底哪裡有多輸出...
希望各位大家能幫忙:)
--------------程式碼
#include <iostream>
#include <cstdlib>
using namespace std;
int roman2num(char x);
string num2roman(int x);
int main()
{
///*
    string x;
    int i, num1 = 0, num2 = 0, sol;
//*/
/*
    int a, b;
    string m;
    while(cin >> a >> b)
    {
        m = num2roman(abs(a-b));
        cout << m << endl;
    }
//*/
///*
    while(getline(cin, x))
    {
        for(i = x.size()-1; x[i] != ' '; i--)
        {
            if(roman2num(x[i]) < roman2num(x[i+1]))
            {
                num1 -= roman2num(x[i]);
            }
            else
            {
                num1 += roman2num(x[i]);
            }
        }
        for(i--; i >= 0; i--)
        {
            if(roman2num(x[i]) < roman2num(x[i+1]))
            {
                num2 -= roman2num(x[i]);
            }
            else
            {
                num2 += roman2num(x[i]);
            }
        }
        //cout << "num1 = " << num1 << " "<< "num2 = " << num2 << endl;
        sol = abs(num1-num2);
        //cout << "sol = " << sol << endl;
        x = num2roman(sol);
        cout << x << endl;
        num1 = num2 = 0;
    }
//*/
    return 0;
}
int roman2num(char x)
{
    int y;
    switch(x)
    {
        case 'I':
            y = 1;
            break;
        case 'V':
            y = 5;
            break;
        case 'X':
            y = 10;
            break;
        case 'L':
            y = 50;
            break;
        case 'C':
            y = 100;
            break;
        case 'D':
            y = 500;
            break;
        case 'M':
            y = 1000;
            break;
        default:
            y = 0;
            break;
    }
    return y;
}
string num2roman(int x)
{
    string y;
    if(x == 0)
    {
        y = "ZERO";
    }
    else
    {
        while(x >= 1000)
        {
            y += 'M';
            x -= 1000;
        }
        if(x >= 900)
        {
            y += "CM";
            x -= 900;
        }
        else if(x >= 500)
        {
            y += 'D';
            x -= 500;
        }
        else if(x >= 400)
        {
            y += "CD";
            x -= 400;
        }
        while(x >= 100)
        {
            y += 'C';
            x -= 100;
        }
        if(x >= 90)
        {
            y += "XC";
            x -= 90;
        }
        else if(x >= 50)
        {
            y += 'L';
            x -= 50;
        }
        else if(x >= 40)
        {
            y += "XL";
            x -= 40;
        }
        while(x >= 10)
        {
            y += 'X';
            x -= 10;
        }
        if(x >= 9)
        {
            y += "IX";
            x -= 9;
        }
        else if(x >= 5)
        {
            y += 'V';
            x -= 5;
        }
        else if(x >= 4)
        {
            y += "IV";
            x -= 4;
        }
        while(x >= 1)
        {
            y += 'I';
            x -= 1;
        }
    }
    return y;
}
 
ZeroJudge Forum