# 13970 - Frieren and her Rizz https://acm.cs.nthu.edu.tw/problem/13970/ ## Converting Binary to Number As the input is always 5 digit binary number, we will have the formula to convert binary to decimal number as : ![](https://hackmd.io/_uploads/BJkCLGJe6.png) Number = (1^st^ digit * 16) + (2^nd^ digit * 8) + (3^rd^ digit * 4) + (4^th^ digit * 2) + (5^th^ digit * 1) ## First Way 1. Scan the whole binary to integer 2. Take every digit from the binary by %10, the result will be either 1 or 0 3. Add the binary number into new variable 4. Divide it by 10 to get the next binary digit, and repeat until signed bit 6. For the signed bit, it's either 1 or 0. The difference between uppercase and lower case is 32. We can multiply the signed bit with 32 and add into the number. ```cpp= #include <stdio.h> int main(){ int binary; scanf("%d", &binary); int num = 0; // we will convert the binary to integer num += binary%10 * 1; // taking the 1st lastest digit and convert to integer binary = binary/10; // remove the 1st lastest digit num += binary%10 * 2; // taking the 2nd lastest digit and convert to integer binary = binary/10; // remove the 2nd lastest digit num += binary%10 * 4; // and so on... binary = binary/10; num += binary%10 * 8; binary = binary/10; num += binary%10 * 16; // taking the first digit of binary binary = binary/10; // now the binary would be either 1 or 0 in this state // fancy way : // char ch = num + 'A' - 1 char ch = num + 64; // convert integer to character ch = ch + binary * 32; // if the signed binary is 1, it will convert to uppercase printf("%c\n", ch); } // by Aurick ``` ## Second Way Binary is too confusing, we can scan each digit to 1 variable You can either scan as %c, or you can also use %1d to scan 1 digit integer. The problem with %c is you scan it as a character, so your number is based on ASCII and you need to subtract it again by 48 For every variable, the value will be either 1 or 0, 1. 1^st^ Digit : Multiply by 32 as 32 is the difference between uppercase and lowercase 2. 2^nd^ Digit : Multiply by 16 as the first digit binary 3. 3^rd^ Digit : Multiply by 8 as the second digit binary 4. 4^th^ Digit : Multiply by 4 as the third digit binary 5. 5^th^ Digit : Multiply by 2 as the fourth digit binary 6. 6^th^ Digit : Multiply by 1 as the fifth digit binary 7. Sum all of the digit and add 64 as we want to conver to ASCII character (Difference between the binary number and letter 'A' character) ```cpp= #include <stdio.h> int main(){ int a,b,c,d,e,f; scanf("%1d%1d%1d%1d%1d%1d", &a, &b, &c, &d, &e, &f); char ch = a *32 + b * 16 + c * 8 + d * 4 + e * 2 + f + 64; printf("%c\n", ch); } // by Aurick ``` ![](https://hackmd.io/_uploads/rJi9vzkga.png)