TJU Root of the Problem
来源:互联网 发布:93国际大专辩论赛网络 编辑:程序博客网 时间:2024/05/17 21:45
Input: The input consists of one or more pairs of values for B and N. Each pair appears on a single line, delimited by a single space. A line specifying the value zero for both B and N marks the end of the input. The value of B will be in the range 1 to 1,000,000 (inclusive), and the value of N will be in the range 1 to 9 (inclusive).
Output: For each pair B and N in the input, output A as defined above on a line by itself.
Example Input:Example Output:
4 35 3
27 3
750 5
1000 5
2000 5
3000 5
1000000 5
0 01
2
3
4
4
4
5
16
题目意思:给定B和N,找到一个A,使得A的N次方离B最近(可能小于,等于,大于)。
解题思路:其实本题大可不必一个一个去试,这样会浪费很多的时间,而且效果不一定很好。既然题目中已经给出了B和N,就可以求出B的N次方根,这样可以将范围缩小在两个数。例如,对B求N次方根后,将其付给一个整形变量X,这样只需要检验X和X+1,其中必然有一个是所要求的结果。
源代码:
#include<iostream>
#include<cmath>
#define esp 1e-6
using namespace std;
int main()
{
int B,N;
while(cin>>B>>N&&B&&N)
{
int j=int (pow(1.0*B,1.0/N)+esp);
double d1= B-pow(1.0*j,1.0*N);
double d2= pow(1.0*(j+1),1.0*N)-B;
if(d1<=d2)cout<<j<<endl;
else cout<<j+1<<endl;
}
return 0;
}
- TJU Root of the Problem
- Root of the Problem
- 1394. Root of the Problem
- Root of the Problem-3100
- 1394. Root of the Problem
- POJ 3100 Root of the Problem
- sicily 1394 Root of the Problem
- zoj 2818 Root of the Problem
- Sicily 1394. Root of the Problem
- poj 3100 Root of the Problem
- POJ 3100 Root of the Problem (水题)
- Sicily 1394. Root of the Problem
- POJ 3100:Root of the Problem
- POJ 3100 Root of the Problem G++
- POJ 3100 Root of the Problem 可能会
- ZOJ Problem Set - 2818 Root of the Problem
- hdu 2740 Root of the Problem (水)
- POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!
- 在MacOS和iOS系统中使用OpenCV
- 九度 题目1541:二叉树
- Documentation/CFS Scheduler
- apdu基本命令
- Android动画 Tweened Animation 之 ScaleAnimation
- TJU Root of the Problem
- SqlServer按照指定顺序对字段进行排序
- pyhton/Perl设置 sublime text2
- 电子商务网站源码下载
- PHP中的Notice错误提示
- 算法之旅——合并排序
- QT 界面制作技巧汇总(持续更新)
- 对LDA理解
- Liferay Hook开发