• P次方数 英雄会 csdn 高校俱乐部


    题目:

        一个整数N,|N| >= 2, 如果存在整数x,使得N = x * x * x... (p个x相乘) =x^p,则称N是p次方数,给定32位内的整数N,求最大的P。例如N=5,输出1,N=36则输出2。

    分析:

        其实我是水过的。X从2到sqrt(N)便利,如果res=logN/logX在容许误差内就返回(int)(res+0.5).

    注意:

       1.输入为2147483648。

       2.输入为负数时候。得到的(int)(res+0.5)要是奇数才返回,不然返回1(很明显)。

       3.容差要足够小。

    最后:

       祝大家轻松水过。

       过了好长一段时间了,贴上代码吧。

    /*******************************************************************************/
    /* OS : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
    * Compiler : g++ (GCC) 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
    * Encoding : UTF8
    * All Rights Reserved by yaolong.
    *****************************************************************************/
    /* Description:给定N(可能为负数),求最大的p满足pow(x,p)=N,x可能为负数
    ***************************************************************
    *****************************************************************************/
    /* Analysis: 从N=2到sqrt(N)遍历,允许容差控制在0.00000000001,在32位数中
    总是能正确******
    状态:通过。
    测评网址:http://hero.csdn.net/Question/Details?ID=299&ExamID=294
    ***********************************************************/
    /*****************************************************************************/
    //*
    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    #define RC 0.00000000001
    #define MAX_INT 2147483648
    int isF(double t)
    {
    //四舍五入
        int near_res=(int) (t+0.5);
    
    //允许误差
        if(abs(near_res-t)<RC)
        {
            return near_res;
        }
    
        return -1;
    
    }
    class Test
    {
    public:
        static int give (int n)
        {
    
            bool _f=true;
            if(n<0)
            {
                _f=false; //是负数,因为负数的话,所得指数必须是奇数。
            }
            //边界2147483648
            if(n==MAX_INT)
                return 31;
    
            long long int num=abs(n);//取绝对值
            double sqrtn=sqrt(num*1.0);//避免多次的开根,取对数
            double logN=log((double)(num));
    
            for(int i=2; i<=sqrtn; i++)
            {
                double tmp=logN/log(1.0*i);
                int res=isF(tmp);
                if(-1!=res)
                {
    
                    if(!_f)  //为负数
                    {
                        if(res%2==1)
                            return res;
    
                    }
                    else
                    {
                        //为正数
                        return res;
                    }
                }
            }
            return 1;
    
        }
    };
    
    //start 提示:自动阅卷起始唯一标识,请勿删除或增加。
    int main()
    {
        int n;
        cout<<Test::give(2147483648)<<endl;
        while(cin>>n&&n) //测试数据
            cout<<Test::give(n)<<endl;
    
    
    
    
        return 0;
    }
    //end //提示:自动阅卷结束唯一标识,请勿删除或增加。
    


  • 相关阅读:
    PyDev for eclipse 插件下载地址
    Impala SQL 语言元素(翻译)[转载]
    oracle9i-11.2安装包及补丁包下载链接
    oracle link的创建过程
    Oracle 查询历史数据(转帖)
    Alpha、Beta、RC、GA版本的区别
    oracle wm_concat(column)函数的使用
    Quest.Central.for.DB2.v5.0.2.4下载地址
    Hadoop 管理工具HUE配置-初始配置
    maven编译下载源码
  • 原文地址:https://www.cnblogs.com/dengyaolong/p/3697223.html
Copyright © 2020-2023  润新知