zoj2818 Root of the Problem 简单数学 开方
2017-09-27 21:06
337 查看
Root of the Problem
Time Limit:
2 Seconds Memory Limit:
65536 KB
Given positive integers B and N, find an integer A such that AN is as close as possible to B. (The result A is an approximation to the Nth root
of B.) Note that AN may be less than, equal to, or greater than B.
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.
Source: Mid-Central USA 2006
第一思路:二分找最小k,k^m<=n,然后和(k+1)^m比较。
第二思路:我们都知道n的m次方为pow(n,m)——精确值,则n开m次方为pow(n,1/m)——近似值(有时会把结果拿回去验证一遍)。当然这个函数适用于小数据,int范围就最好。
Time Limit:
2 Seconds Memory Limit:
65536 KB
Given positive integers B and N, find an integer A such that AN is as close as possible to B. (The result A is an approximation to the Nth root
of B.) Note that AN may be less than, equal to, or greater than B.
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 3 5 3 27 3 750 5 1000 5 2000 5 3000 5 1000000 5 0 0 | 1 2 3 4 4 4 5 16 |
第一思路:二分找最小k,k^m<=n,然后和(k+1)^m比较。
第二思路:我们都知道n的m次方为pow(n,m)——精确值,则n开m次方为pow(n,1/m)——近似值(有时会把结果拿回去验证一遍)。当然这个函数适用于小数据,int范围就最好。
#include<cstdio> #include<cstdlib> #include<iostream> #include<math.h> #include<algorithm> using namespace std; int main() { int n,m,k; while(cin>>n>>m){ if(!n&&!m) return 0; k=pow(n,(double)1/m); if(pow(k+1,m)-n<n-pow(k,m)) k++;//求靠近的一个 cout<<k<<endl; } return 0; }
相关文章推荐
- zoj 2818 Root of the Problem(数学思维题)
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
- zoj 2818 Root of the Problem
- zoj 2818 Root of the Problem
- zoj 2818 || poj 3100 Root of the Problem(水! = =)
- poj 3100 && zoj 2818 ( Root of the Problem ) (睡前一水)
- POJ 3100 Root of the Problem(简单题)
- ZOJ Problem Set - 2338 The Towers of Hanoi Revisited DFS+预处理
- ZOJ Problem Set - 3880||Demacia of the Ancients
- poj 3100 Root of the Problem
- poj 3100 Root of the Problem
- 杭电Problem 5053 the sum of cube 【数学公式】
- POJ 3100 Root of the Problem 可能会
- 5 Whys Quickly Getting to the Root of a Problem
- Root of the Problem
- POJ 3100 Root of the Problem (水题)
- POJ 3100 Root of the Problem
- POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!
- UVa 10499 The Land of Justice(简单数学)