Facebook
From Mustard Eider, 1 Year ago, written in C.
This paste is a reply to Untitled from Bitty Peafowl - go back
Embed
Viewing differences between Untitled and Re: Untitled
#include
#include

int convertToBase10(int x, int base){
    int result = 0;
    int i = 0;
    while(x>0){
        result += (x%10)*pow(base,i);
        i++;
        x/=10;
    }
    return result;
}
int convertFromBase10(int x, int base){
    int result = 0;
    int i = 0;
    while(x>0){
        result += (x%base) * pow(10,i);
        x/=base;
        i++;
    }
    return result;
}

int main(){
    int base1,base2,x;
    scanf("%d %d %d",&base1,&base2,&x);
    x = convertFromBase10(convertToBase10(x,base1),base2);
    int n = (int)log10(x) + 1;
    x *= (int)pow(10,n%2);
    n += n%2;
    
int halfSize = (int)pow(10,ceil(((int)log10(x) + 1)/2)) + (((int)log10(x) + 1) == 0);
    
int part1 = x%((int)pow(10,n/2));
    
x%(halfSize);
    
int part2 = x/((int)pow(10,n/2));  
x/(halfSize);  
    int result = convertToBase10(part1,base2) + convertToBase10(part2,base2);
    printf("%i",convertFromBase10(result,base2));
}