UVA Solution 355 - The Bases Are Loaded - 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 355 - The Bases Are Loaded - Solution in C, C++ | Volume 3

UVA Solution 355 - The Bases Are Loaded - Solution in C, C++ | Volume 3


UVA Online Judge Solution 355 - The Bases Are Loaded| Volume 3
UVA Problem Link -

Problem Name: 355 - The Bases Are Loaded
Problem Number : UVA - 355 - The Bases Are Loaded
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 355 - The Bases Are Loaded Code in C, CPP:

UVA Solution 355 - The Bases Are Loaded Code in C, CPP:


#include <stdio.h>

int main() {
    int a, b;
    char s[20];
    while(scanf("%d %d %s", &a, &b, s) == 3) {
        int i, flag = 0;
        long long dec = 0, base = 1;
        for(i = 0; s[i]; i++) {
            dec *= a;
            if(s[i] >= '0' && s[i] <= '9') {
                if(s[i]-'0' >= a) {
                    flag = 1;
                } else {
                    dec += s[i]-'0';
                }
            } else {
                if(s[i]-'A'+10 >= a) {
                    flag = 1;
                } else {
                    dec += (s[i]-'A'+10);
                }
            }
        }
        if(flag) {
            printf("%s is an illegal base %d number\n", s, a);
        } else {
            long long c[50] = {}, ct = -1;
            while(dec) {
                c[++ct] = dec%b;
                dec /= b;
            }
            if(ct < 0) ct = 0;
            printf("%s base %d = ", s, a);
            while(ct >= 0) {
                printf("%c", c[ct] <= 9 ? c[ct]+'0' : c[ct]-10+'A');
                ct--;
            }
            printf(" base %d\n", b);
        }
    }
    return 0;
}

No comments:

Post a Comment