jeudi 15 novembre 2018

Converting dec to binary, octal and hex problem

The task is on Hackerrank. Print 3 numbers in their binary, octal and hex. Numbers must be below 10^6 or end the program. There are 2 Test Cases. I've done the Test Case #0, but I don't know what else to fix for Test Case #1. I think I've done all the things that the task wants from me. Can you help me solve this?

#include <bitset>
#include <string>
using namespace std;

string decimal_to_binary(int n)
{
       string m;
       while (n >= 1) 
       {
             m = static_cast<char>((n%2) + '0') + m;
             n /=2;
       }
       return m;
}

int main()
{
unsigned long long a,b,c;

    cin >> a >> b >> c;
    if(a > 1000000 || b > 1000000 || c > 1000000) {return 0;}
    else{ 
    cout << std::dec << decimal_to_binary(a) << " " << std::oct << a << " " 
<< std::uppercase << std::hex << a << '\n'
        << std::dec << decimal_to_binary(b) << " " << std::oct << b << " " 
<< std::uppercase << std::hex << b << '\n'
        << std::dec << decimal_to_binary(c) << " " << std::oct << c << " " 
<< std::uppercase << std::hex << c << '\n';
    }
}

Aucun commentaire:

Enregistrer un commentaire