UVA Solution 389 - Basically Speaking - 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 389 - Basically Speaking - Solution in C, C++ | Volume 3

UVA Solution 389 - Basically Speaking - Solution in C, C++ | Volume 3


UVA Online Judge Solution 389 - Basically Speaking | Volume 3
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=325

Problem Name: 389 - Basically Speaking
Problem Number : UVA - 389 - Basically Speaking
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 389 - Basically Speaking - Solution in C, C++ | Volume 3


UVA Solution 389 - Basically Speaking Code in C, CPP:


#include <stdio.h>
#include <string.h>
int main() {
    char s[50];
    int n, m;
    while(scanf("%s %d %d", s, &n, &m) == 3) {
        int i, l = strlen(s);
        long long dec = 0, j = 1;
        for(i = l-1; i >= 0; i--) {
            if(s[i] <= '9')
                dec += (s[i]-'0')*j;
            else
                dec += (s[i]-'A'+10)*j;
            j *= n;
        }
        int ans[60] = {}, idx = -1;
        while(dec > 0)
            ans[++idx] = dec%m, dec /= m;
        if(idx >= 7) {
            puts("  ERROR");
            continue;
        }
        if(idx < 0) idx = 0;
        for(i = 6; i > idx; i--)
            printf(" ");
        for(i = idx; i >= 0; i--)
            printf("%c", ans[i] < 10 ? ans[i]+'0' : ans[i]+'A'-10);
        puts("");
    }
    return 0;
}

No comments:

Post a Comment