With a pre-driven number, it works according to a simple algorithm (for example, 4 characters)
#include <stdio.h> #include <stdlib.h> main () { int number = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a10 = 0; printf("Please input char 4 "); scanf("%d", &number); a1 = number / 1000; a2 = (number - (a1 * 1000)) / 100; a3 = (number - ((a1 * 1000) + (a2 * 100))) / 10; a4 = (number - ((a1 * 1000) + (a2 * 100) + (a3 * 10))); a10 = a1 * 8 + a2 * 4 + a3 * 2 + a4 * 1; printf("%d", a10); system("PAUSE"); }
But if the numbers are more than 4 or less, I can not figure out how to implement. I thought through the algorithm, it is necessary to count the number of characters in a given number and with a bunch of conditions to implement it, but it is very cumbersome and I can not figure out how to make it shorter. Given that I can only use WHILE and IF ELSE conditions. Who can tell the algorithm?