POJ_3100_Root of the Problem(暴力)

来源:互联网 发布:高性能网络编程2 编辑:程序博客网 时间:2024/06/06 00:53

题型:数论


题意:给出B和N,求出一个A,使得A^N最接近B


分析:

       由于B是int型,所以A的范围不会很大,所以暴力可以接受,从1找起,到刚好大于B的时候break,比较A与A-1哪一个是更加接近的答案。

       因为pow()函数中的参数均为double型,而时间复杂度与手写一样,所以手写一个比较保险


代码:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;int fun(int a,int n){    int mul=1;    for(int i=1;i<=n;i++){        mul*=a;    }    return mul;}int main(){    int a,b,n;    while(1){        scanf("%d%d",&b,&n);        if(b==0 && n==0) break;        int a=1;        int ans;        while(1){            ans=fun(a,n);            if(ans>=b) break;            a++;        }        if(ans-b<b-fun(a-1,n)){            printf("%d\n",a);        }        else printf("%d\n",a-1);    }    return 0;}


原创粉丝点击