题目描述:
Given a positive integer n and the odd integer o and the nonnegative integer p such that n = o2^p.
Example:
For n = 24, o = 3 and p = 3.
Task:
Write a program which for each data set,reads a positive integer n,computes the odd integer o and the nonnegative integer p such that n = o2^p,writes the result.
输入要求:
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.
输出要求:
The output should consists of exactly d lines, one line for each data set.
输入样例:
1
24
输出样例:
3 3
【分析】
任何一个正整数n,都可以表示成一个奇数o与2的p次幂相乘的,如16=1*2^4,24=3*2^3,29=29*2^0。因此,p即为n中2的个数,而o即为除尽这些2后剩余的商值。参考代码如下:
#include<stdio.h>
int main(){
int d,n;
scanf("%d",&d);
while(d--){
scanf("%d",&n);
int count = 0;
while(n%2==0){
count++;
n=n/2;
}
printf("%d %d",n,count);
printf("\n");
}
return 1;
}