Question
Reversed Binary Numbers (Kattis) - C Language I have problems when input is 1000000000 (upper limit); my code is posted after the problem. #include #include
Reversed Binary Numbers (Kattis) - C Language
I have problems when input is 1000000000 (upper limit); my code is posted after the problem.
#include
#include
int main(void) {
int remainder, i = 0;
long long decimal, binary = 0, reversed = 0, multiplier = 1;
scanf("%lld", &decimal); // decimal can reach up to 1000000000, which is 30-digit long in binary
while(decimal != 0) {
remainder = decimal % 2;
decimal /= 2;
binary += remainder * multiplier;
multiplier *= 10;
}
while(binary != 0) {
remainder = binary % 10;
reversed = (reversed * 10) + remainder;
binary /= 10;
}
while(reversed != 0) {
remainder = reversed % 10;
decimal += (remainder * pow(2, i));
reversed /= 10;
i++;
}
printf("%lld ", decimal);
return 0;
}
Reversed Binary Number:s Yi has moved to Sweden and now goes to school here. The first years of schooling she got in China, and the curricula do not match completely in the two countries. Yi likes mathematics, but now... The teacher explains the algorithm for subtraction on the board, and Yi is bored. Maybe it is possible to perform the same calculations on the numbers corresponding to the reversed binary representations of the numbers on the board? Yi dreams away and starts constructing a program that reverses the binary representation, in her mind. As soon as the lecture ends, she will go home and write it on her computer Task Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11. Input The input contains a single line with an integer N, 1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started