I encountered this problem : https://www.urionlinejudge.com.br/judge/en/problems/view/1193
/*input
3
101 bin
101 dec
8f hex
*/
/*************************************************************
* Purpose : https://www.urionlinejudge.com.br/judge/en/problems/view/1193
* Author: Sahil Arora
* Version: 1.0
* Date: 22/10/15
* Bugs : None
*************************************************************/
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
/* code */
std::ios_base::sync_with_stdio(false);
int test;
cin>>test;
for(int i=1;i<=test;++i){
cout<<"Case "<<i<<":\n";
string str,base;
long long num,j;
cin>>str>>base;
if(base=="dec"){
num = stoll(str,nullptr);
cout<<hex<<num<<" hex\n";
bitset<32> bin(num);
for(j=31;bin[j]==0 && j>=0;--j)
;
while(j>=0)
cout<<bin[j--];
cout<<" bin\n\n";
}
else if(base=="hex"){
str = "0x" + str;
num = stoll(str,nullptr,16);
cout<<dec<<num<<" dec\n"; // <--focus on this line
bitset<32> bin(num);
for(j=31;bin[j]==0 && j>=0;--j)
;
while(j>=0)
cout<<bin[j--];
cout<<" bin\n\n";
}
else{
num = stoll(str,nullptr,2);
cout<<dec<<num<<" dec\n"<<hex<<num<<" hex\n\n";
}
}
return 0;
}
Now on changing line 45 to :
cout<<num<<" dec\n";
My output for hex
changes. It gives the same output as input for hex
to dec
.I fail to understand why it gives such an error. Also, if I enter only 1 test case, it gives correct output for a hex
, but still gives 20% Wrong answer on submission!
Input :
3
101 bin
101 dec
8f hex
Expected output :
Case 1:
5 dec
5 hex
Case 2:
65 hex
1100101 bin
Case 3:
143 dec
10001111 bin
My Output without using dec
in cout
:
Case 1:
5 dec
5 hex
Case 2:
65 hex
1100101 bin
Case 3:
8f dec
10001111 bin