zoj 2818 Root of the Problem

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.

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdlib>
 4 using namespace std;
 5 int main(){
 6     double b, n;
 7     while(cin >> b >> n){
 8         if(b == 0 && n == 0)
 9             break;
10         //pow:double pow(double, double)
11         int a = (int)pow(b, 1/ n);//直接取整,不是四舍五入 ,pow(a, n) < b
12         if(b - pow(a, n) < pow(a + 1, n) - b)
13             cout << a << endl;
14         else
15             cout << a + 1 << endl;
16     }
17     return 0;
18 }
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

时间: 2024-10-19 22:37:37

zoj 2818 Root of the Problem的相关文章

poj 3100 &amp;&amp; zoj 2818 ( Root of the Problem ) (睡前一水)

链接:click here 题意: 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. .给你B和N,求个整数A使得A^n最接近B

POJ 3100 &amp; ZOJ 2818 &amp; HDU 2740 Root of the Problem(数学)

题目链接: POJ:http://poj.org/problem?id=3100 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1818 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2740 Description Given positive integers B and N, find an integer A such that AN is as close as

ZOJ 3686 A Simple Tree Problem(线段树)

A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0. We define this kind of operation: given a subtree, negate all its labels. An

HDU ACM 2740 Root of the Problem 简单数学题

题意:求A,使得A^N最接近B. 分析:A=B^(1/n),对其上下取整,在各取N次幂,取最接近B的. #include<iostream> #include<cmath> using namespace std; int main() { int B,N,p,q; double tmp; while(cin>>B>>N && (B||N)) { tmp=pow(1.0*B,1.0/N); p=floor(tmp); //向下取整 q=cei

zoj 3686 A Simple Tree Problem (线段树)

Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> #include <cmath>

How to address this problem?

root# cmake .. No problem. root# make [ 63%] Linking CXX shared module collisionperceptor.so/usr/bin/ld: /usr/local/lib/libode.a(ode.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC

Problem Populating Next Right Pointers in Each Node II

Problem Description: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given th

堆,二分,尺取

A - Aggressive cows POJ - 2456 Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C <= N) cows don't like

浅谈红黑树(C语言代码实现)

定义: 我们先来看看<算法导论>中的红黑树的定义:"红黑树是许多'平衡'搜索树的一种,可以保证在最坏的情况下基本动态集合操作的时间复杂度为O(lgn)." 性质: 红黑树的性质如下: 1.每个节点是红色的,或者是黑色的. 2.根节点和叶子节点是黑色的. 3.如果一个节点是红色的,那么它的父节点和子节点必须是黑色的. 4.对于每一个节点来说,从该节点到叶子节点的简单路径上,所包含的黑色节点的数量必须一致. 节点属性: 红黑树每个节点的基本属性值有五个: 1.color:表示该