UVA Solution 343 - What Base Is This - Solution in C, C++ | Volume 3 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Friday, May 5, 2017

UVA Solution 343 - What Base Is This - Solution in C, C++ | Volume 3

UVA Solution 343 - What Base Is This - Solution in C, C++ | Volume 3


UVA Online Judge Solution 343 - What Base Is This  | Volume 3
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=279

Problem Name: 343 - What Base Is This
Problem Number : UVA - 343 - What Base Is This
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 343 - What Base Is This - Solution in C, C++ | Volume 3

UVA Solution 343 - What Base Is This Code in C, CPP:


#include <stdio.h>
#include <string.h>
long long toDec(char s[], long long base, int len) {
    long long Dec = 0, B = 1;
    int i;
    for(i = len-1; i >= 0; i--) {
        if(s[i] >= '0' && s[i] <= '9')
            Dec += (s[i]-'0')*B;
        else
            Dec += (s[i]-'A'+10)*B;
        B *= base;
    }
    return Dec;
}
int main() {
    char s1[50], s2[50];
    while(scanf("%s %s", s1, s2) == 2) {
        int l1 = strlen(s1), l2 = strlen(s2);
        int b1 = 1, b2 = 1, i;
        for(i = 0; i < l1; i++) {
            if(s1[i] >= '0' && s1[i] <= '9')
                b1 = b1 > s1[i]-'0' ? b1 : s1[i]-'0';
            else
                b1 = b1 > s1[i]-'A'+10 ? b1 : s1[i]-'A'+10;
        }
        for(i = 0; i < l2; i++) {
            if(s2[i] >= '0' && s2[i] <='9')
                b2 = b2 > s2[i]-'0' ? b2 : s2[i]-'0';
            else
                b2 = b2 > s2[i]-'A'+10 ? b2 : s2[i]-'A'+10;
        }
        b1++, b2++;
        for(; b1 <= 36; b1++) {
            for(i = b2; i <= 36; i++) {
                if(toDec(s1, b1, l1) == toDec(s2, i, l2)) {
                    printf("%s (base %d) = %s (base %d)\n", s1, b1, s2, i);
                    goto Break;
                }
            }
        }
        printf("%s is not equal to %s in any base 2..36\n", s1, s2);
        Break:
        i == 0;
    }
    return 0;
}

No comments:

Post a Comment