[ACM ] ZeroJudge a013
看板C_and_CPP (C/C++)作者darklady (Sylvanas)時間16年前 (2009/02/21 00:29)推噓0(0推 0噓 5→)留言5則, 4人參與討論串1/2 (看更多)
http://cat.nknush.kh.edu.tw/ZeroJudge/ShowProblem?problemid=a013
可以用帳號guest,pw guest測試
這個題目我解出來,自己測試都沒問題,
但是丟上去都會WA
與正確輸出不相符(line:8)
您的答案為: XXVIII
正確答案為: XVIII
可以麻煩各位幫我看一下程式碼嗎?
http://src.wtgstudio.com/?5g5p41
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000;
int sum = 0, temp = 0, result = 0;
string input;
while(getline(cin, input))
{
//cout << "debug ... input = " << input << endl;
//cout << "debug ... input.size() = " << input.size() << endl;
for(int j = input.size()-1; j != -1; --j)
{
if(input[j] == '#')
return 0;
if(input[j] == ' ')
{
temp = sum;
sum = 0;
//cout << "debug ... temp = " << temp << endl;
}
if(input[j] =='I')
{
if(input[j+1] == 'V')
;
else if (input[j+1] == 'X')
;
else
sum += 1;
}
if(input[j] =='V')
{
if(input[j-1] == 'I')
sum += 4;
else
sum += 5;
}
if(input[j] == 'X')
{
if(input[j-1] == 'I')
sum += 9;
if(input[j+1] == 'C')
;
else
sum += 10;
}
if(input[j] == 'L')
{
if(input[j-1] == 'X')
sum += 40;
else
sum += 50;
}
if(input[j] == 'C')
{
if(input[j-1] == 'X')
sum += 90;
else if(input[j+1] == 'M')
;
else if(input[j+1] == 'D')
;
else
sum += 100;
}
if(input[j] == 'D')
{
if(input[j-1] == 'C')
sum += 400;
else
sum += 500;
}
if(input[j] == 'M')
{
if(input[j-1] == 'C')
sum += 900;
else
sum += 1000;
}
}
//cout << "debug ... sum = " << sum << endl;
//cout << "debug ... sum-temp = " << sum-temp << endl;
result = abs(sum-temp);
sum = 0;
if(result == 0)
{
cout << "ZERO" << endl;
continue;
}
if(result/1000)
{
switch(result/1000)
{
case 1: cout << "M";
break;
case 2: cout << "MM";
break;
case 3: cout << "MMM";
break;
}
result = result%1000;
if(result == 0)
cout << endl;
//cout << "\ndebug ... result%1000 = " << result << endl;
}
if(result/900)
{
cout << "CM";
result = result%900;
//cout << "\ndebug ... result%900 = " << result << endl;
if(result == 0)
cout << endl;
}
if(result/500)
{
cout << "D";
result = result%500;
//cout << "\ndebug ... result%500 = " << result << endl;
if(result == 0)
cout << endl;
}
if(result/400)
{
cout << "CD";
result = result%400;
if(result == 0)
cout << endl;
}
if(result/100)
{
switch(result/100)
{
case 1: cout << "C";
break;
case 2: cout << "CC";
break;
case 3: cout << "CCC";
break;
}
result = result%100;
//cout << "\ndebug ... result%100 = " << result << endl;
if(result == 0)
cout << endl;
}
if(result/90)
{
cout << "XC";
result = result%90;
if(result == 0)
cout << endl;
}
if(result/50)
{
cout << "L";
result = result%50;
if(result == 0)
cout << endl;
}
if(result/40)
{
cout << "XL";
result = result%40;
if(result == 0)
cout << endl;
}
if(result/10)
{
switch(result/10)
{
case 1: cout << "X";
break;
case 2: cout << "XX";
break;
case 3: cout << "XXX";
break;
}
result = result%10;
//cout << "\ndebug ... result%10 = " << result << endl;
if(result == 0)
cout << endl;
}
if(result/9)
{
cout << "IX";
result = result%9;
if(result == 0)
cout << endl;
}
if(result/5)
{
cout << "V";
result = result%5;
if(result == 0)
cout << endl;
}
if(result/4)
{
cout << "IV";
result = result%4;
if(result == 0)
cout << endl;
}
if(result/1)
{
switch(result/1)
{
case 1: cout << "I";
break;
case 2: cout << "II";
break;
case 3: cout << "III";
break;
}
result = result%1;
//cout << "\ndebug ... result%1 = " << result << endl;
cout << endl;
}
}
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.161.221.10
※ 編輯: darklady 來自: 118.161.221.10 (02/21 00:31)
※ 編輯: darklady 來自: 118.161.221.10 (02/21 00:32)
→
02/21 18:15, , 1F
02/21 18:15, 1F
→
02/21 18:32, , 2F
02/21 18:32, 2F
→
02/22 01:32, , 3F
02/22 01:32, 3F
→
02/22 01:32, , 4F
02/22 01:32, 4F
→
02/22 10:48, , 5F
02/22 10:48, 5F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章