c++简单程序错误,跪求高手解答
来源:百度知道 编辑:UC知道 时间:2024/06/03 16:28:44
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.
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
The output should consists of exactly d lines, one line for each data set.
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
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
The output should consists of exactly d lines, one line for each data set.
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
小意思,你自己粗心而已,在main函数中尼的for循环没有跳出来而且没有实际意义,改为这样就可以了,你的算法思想还是没有错的,以后要小心,做算法更是!
int main()
{
int c,n,i,x,t;
cin>>c;
while(c--)
{
cin>>n;
x=0;
i=0;
while(x<=n)
{
//for(i=0;;i++){}
x=f(i);
i++;
}
t=i-1;
for(i=t;i>=0;i--)
if(n%f(i)==0)
{cout<<n/f(i)<<' '<<i<<endl;break;}
}
return 0;
}
while(x<=n)
{
for(i=0;;i++)
x=f(i);
}
改一下
int i=0;
while(x<=n)
{
x=f(i);++i;
}
不能跳出
for(i=0;;i++)
x=f(i);循环