原题:
A Simple TaskGiven 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.
Input
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.
Each data set consists of exactly one line containing exactly one
integer n, 1 <= n <= 10^6.
Output
Line i, 1 <= i <= d, corresponds to the i-th input and should contain two
integers o and p separated by a single space such that n = o2^p.
Sample Input
1
24
Sample Output
3 3
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int set_num;
int *set = NULL;
int i,j,k;
int temp;
bool flag;
cin>>set_num;
set = new int[set_num];
for (i = 0;i<set_num;i++)
cin>>set[i];
for (i = 0;i<set_num;i++)
{
if (set[i]%2!=0)
{
cout<<set[i]<<' '<<0<<endl;
continue;
}
flag = false;
for (j = 1;j<=set[i]/2;j+=2)
{
temp = 0;
k=1;
while(temp<set[i])
{
temp = j*pow(2,k);
if (temp==set[i])
{
cout<<j<<' '<<k<<endl;
flag = true;
break;
}
else
k++;
}
if (flag)
break;
}
}
return 0;
}
上面是我提交的程序
在zju提交编译错误一次 因为标准c++ pow函数为 pow(double,<type>)第一个参数必须为double,但是我再shantou 上用pow(int,int)就过了,都是编译器惹的祸...
posted on 2006-02-08 23:40
豪 阅读(902)
评论(14) 编辑 收藏 引用 所属分类:
算法&ACM