UVa 355 The Bases Are Loaded

提醒自己,每次WA都要檢查的事情
1. Bounded Condition ex. 0
2. 變數有效範圍 ex. 是不是超出int的有效範圍,必須改用long long ?
不然很浪費時間 哭哭

這題的另外一個感想就是C++ string實在很難用,
從以前就這樣覺得,這次更加深我的印象。

關鍵測資
5 3 0
16 16 7FFFFFFFFFF
10 10 9999
上面這三個都是有效測資(not illegal)

#include<iostream>
#include<algorithm>
using namespace std;
inline int CharToDigit(char c)
{
if ( isdigit(c) )
return c - '0';
return c - 'A' + 10;
}
inline char DigitToChar(int n)
{
return "0123456789ABCDEF"[n];
}
bool checkIllegal(int base1, string& in )
{
for (int i = 0; i < in.length(); ++i)
if ( CharToDigit(in[i]) >= base1 )
return true;
return false;
}
/* convert string 'in' to int 'value'. */
long long parseValue( int base1, string& in )
{
if ( checkIllegal(base1, in) ) return -1;
long long value = 0;
long long curBase = 1;
for (int i = in.length() - 1; i >= 0; --i)
{
value += CharToDigit(in[i]) * curBase;
curBase *= base1;
}
return value;
}
/* convert int 'value' to string 'out' */
string toBase( int base2, long long value )
{
if (value == 0) return "0";
string out;
while ( value > 0 )
{
out += DigitToChar( value % base2 );
value /= base2;
}
reverse(out.begin(), out.end());
return out;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("355.in", "r", stdin);
#endif
int base1, base2;
string in;
while ( cin >> base1 >> base2 >> in )
{
long long value = parseValue(base1, in);
if ( value < 0 ) //wrong
{
cout << in << " is an illegal base " << base1 << " number\n";
continue;
}
cout << in << " base " << base1 << " = " << toBase(base2, value) << " base " << base2 << "\n";
}
return 0;
}
view raw 355.cpp hosted with ❤ by GitHub

留言

這個網誌中的熱門文章

UVa 10125 Sumsets

讀書心得: 撒哈拉的故事

讀書心得: 你以為你以為的就是你以為的嗎?