pat basic 1101 How many times is B

Suppose the number formed by the lowest D bits of a number A is ad​. If ad​ is cut off and moved before the highest bit of A, a new number B is formed. How many times is B the size of A? For example, if you cut off the lowest two digits of 12345, 45, and put them in front of 123, you will get 45123, which is about 3.66 times of 12345.

Input format:

The input gives a positive integer A (≤109) and the number of digits to truncate D in one line. The question ensures that D does not exceed the total number of digits in A.

Output format:

Calculate how many times B is A and output 2 decimal places.

Input example 1:

12345 2

Output sample 1:

3.66

Input example 2:

12345 5

Output sample 2:

1.00

Problem-solving ideas:

There are many methods, as long as the answer is correct

#include <stdio.h>
#include <math.h>

int main(int argc, const char *argv[]) {
	int A, D, cut, len, mul, tmp, a1, a2, B;
	if ( scanf("%d %d", &A, &D)==EOF ) printf("error\n");

	tmp = A; len = 0;
	while ( tmp ) {
		tmp /= 10;
		++len;
	}
	cut = pow(10, D);
	mul = pow(10, len - D);
	a1 = A / cut;
	a2 = A % cut;
	B = a2 * mul + a1;
	printf("%.2f\n", (double)B/A);

	return 0;
}

Guess you like

Origin blog.csdn.net/herbertyellow/article/details/126683435