Click Here to go back to the homepage.

Which Base is it Anyway? Solution:


#include <bits/stdc++.h>
using namespace std;

int main() {
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    
    int cases;
    cin >> cases;
    while(cases--){
        int n_case, result;
        size_t converted;
        string in;
        cin >> n_case >> in;
        cout << n_case << " ";
        try{
            result = stoi(in, &converted, 8);
        } catch(invalid_argument e){
            converted = 0;
        }
        if(converted == in.size()){
            cout << result << " ";
        } else {
            cout << "0 ";
        }
        try {
            result = stoi(in, &converted, 10);
        } catch(invalid_argument e){
            converted = 0;
        }
        if(converted == in.size()){
            cout << result << " ";
        } else {
            cout << "0 ";
        }
        try{
            result = stoi(in, &converted, 16);
        } catch(invalid_argument e){
            converted = 0;
        }
        if(converted == in.size()){
            cout << result << endl;
        } else {
            cout << "0 " << endl;
        }
    }
    
    return 0;
}