I have read all the related threads and also tried all the sample inputs given but couldn't understand why I am getting PE. My output is exactly the same as the sample outputs posted in this forum.
Below is my code for doing the output. I tried both adding and not adding a blank line at the end, but no luck. Any special case? Thanks.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int number(char n) {
if (n <= '9' and n >= '0') return n - '0';
return n - 'A' + 10;
}
char num_b(int n) {
if (n <= 9 and n >= 0) return '0' + n;
return 'A' + n - 10;
}
int base_a_to_dec(string n, int a) {
int s = n.size();
int dec = 0;
for (int i = 0; i < s; ++i) dec = dec*a + number(n[i]);
return dec;
}
void base_dec_to_b(int dec, int b, int w) {
if (dec < b) cout << setw(w) << num_b(dec%b);
else {
base_dec_to_b(dec/b, b, w - 1);
cout << num_b(dec%b);
}
}
int main() {
string num;
while (cin >> num) {
int a, b;
cin >> a >> b;
int n = base_a_to_dec(num, a);
if (b*b*b*b*b*b*b <= n) cout << setw(7) << "ERROR" << endl;
else {
base_dec_to_b(n, b, 7);
cout << endl;
}
}
}
i need more test inputs to fail my code. i have tried all the test inputs available on all the 389 posts here and get the correct result but get WA by online judge!
thanks to everyone! i got AC
the problem: i was not printing a '\n' at the end
Best Regards,
Mrunmoy.
I belong to { IDIOTS }
IDIOTS - Intelligent Dynamic & Innovative On-The-Spot!
well i got AC . but again time is very bad 1.4 s
as far as implementation goes i use a look up table. thats much faster.
should i go in for optimizations or is there any other algo available ????