原题:
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;
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
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];
![](/Images/OutliningIndicators/InBlock.gif)
for (i = 0;i<set_num;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (set[i]%2!=0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<set[i]<<' '<<0<<endl;
continue;
}
flag = false;
for (j = 1;j<=set[i]/2;j+=2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
temp = 0;
k=1;
while(temp<set[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
temp = j*pow(2,k);
if (temp==set[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<j<<' '<<k<<endl;
flag = true;
break;
}
else
k++;
}
if (flag)
break;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
上面是我提交的程序
在zju提交编译错误一次 因为标准c++ pow函数为 pow(double,<type>)第一个参数必须为double,但是我再shantou 上用pow(int,int)就过了,都是编译器惹的祸...
posted on 2006-02-08 23:40
豪 阅读(894)
评论(14) 编辑 收藏 引用 所属分类:
算法&ACM