Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: theem1502

Problemset: แปลงเลขฐาน

Language: c

Time: 0.002 second

Submitted On: 2025-09-08 15:16:56

#include <stdio.h>
#include <stdlib.h>

int main() {
    char num[100];
    scanf("%s", &num);
    int length = 0;
    while(num[length] != '\0') {
        length++;
    }
    int currentnum = 0;
    int multiplier = 1;
    length -= 1;
    char letters[17] = "0123456789ABCDEF";
    while (length >= 0) {
        int currentchar = num[length];
        int therealcurrentnum = 0;
        for (int i = 0; i < 27; i++) {
            if (letters[i] == currentchar) {
                therealcurrentnum = i;
            }


            
        }
            currentnum += (therealcurrentnum*multiplier);
            multiplier *= 16;
            length--;
    }
    int tempcurrentnum = currentnum;
    long long base2converted = 0;
    long long than = 1;
    
    while (currentnum > 0) {
        int remainder = currentnum % 2;
        base2converted = base2converted + (remainder * than);
        than *=10;
        currentnum /= 2;
    }

    int base8converted = 0;
    int then = 1;
    while (tempcurrentnum > 0) {
        int remainder = tempcurrentnum % 8;
        base8converted = base8converted + (remainder * then);
        then *=10;
        tempcurrentnum /= 8;
    }

    printf("%lld", base2converted);
    printf("\n%d", base8converted);

}