Base conversion (function topic)

1113: binary conversion (Special Function)
time limit: 1 Sec memory limit: 128 MB
submitted: 72 Resolution: 55
Title Description
enter a decimal integer n, outputs the corresponding binary integer. Commonly used methods to convert "In addition modulo 2, in reverse order." A decimal number by 2 to obtain the remainder and quotient, the quotient obtained by dividing by 2, and so on, up until the quotient equals 0, to take down the remainder of the divided binary number that is asked for. For example, the calculation process 52 in terms of binary number `
52 is divided by 2 a remainder obtained were 0,0,1,0,1,1, in reverse order, to obtain the corresponding 52 binary number 110100.

Thought a recursive calculation process described above is such that: the output of n / 2 corresponding to the binary number, then enter the 2%. Implementation of a recursive function as follows:

void convert(int n)

{

if(n > 0)

{

  调用自身,输出n/2对应的二进制数;

   输出n%2;

}

}

Try it!
Input
enter a positive integer n.
Output
number n corresponding to the binary output.

# include<stdio.h>
void convert(int x);
int main()
{	
	int x;
	scanf("%d",&x);
	convert(x);
	return 0;
}
void convert(int x)
{
	if(x>0)
	{
		convert(x/2);
		printf("%d",x%2);
	}
}

Here with a recursive function, with no array, the array simpler

Published 123 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/Du798566/article/details/104705898