POJ1730_Perfect Pth Powers【水题】

Perfect Pth Powers

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 16699
Accepted: 3786

Description

We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such
that x is a perfect pth power.

Input

Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.

Output

For each test case, output a line giving the largest integer p such that x is a perfect pth power.

Sample Input

17

1073741824

25

0

Sample Output

1

30

2

Source

Waterloo local 2004.01.31

题目大意:对于一些整数b,n = b^p,(b为正整数)若p最大时,n为完美平方数

给你一个数n,求使n为完美平方数时,最大的p值

思路:p从31到1遍历,求n的p次开方,转为int型的t,再求t的p次方,转为int型的x

若x和n相等,则求得的p为最大值,break出循环

注意:求n的p次开方用pow()求,因为pow()函数得到的为double型,而double型数据

精度问题,比如4可表示为3.99999……或4.0000001,所以转为int型时+0.1

参考博文:http://blog.csdn.net/wangjian8006/article/details/7831478

#include<stdio.h>
#include<math.h>

int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        if(n > 0)
        {
            for(int i = 31; i >= 1; i--)
            {
                int t = (int)(pow(n*1.0,1.0/i) + 0.1);
                int x = (int)(pow(t*1.0,1.0*i) + 0.1);
                if(n == x)
                {
                    printf("%d\n",i);
                    break;
                }
            }

        }
        else
        {
            n = -n;
            for(int i = 31; i >= 1; i-=2)
            {
                int t = (int)(pow(n*1.0,1.0/i) + 0.1);
                int x = (int)(pow(t*1.0,1.0*i) + 0.1);
                if(n == x)
                {
                    printf("%d\n",i);
                    break;
                }
            }
        }
    }
    return 0;
}
时间: 2024-08-28 13:34:15

POJ1730_Perfect Pth Powers【水题】的相关文章

poj1730 - Perfect Pth Powers(完全平方数)(水题)

/* 以前做的一道水题,再做精度控制又出了错///... */ 题目大意: 求最大完全平方数,一个数b(不超过int范围),n=b^p,使得给定n,p最大: 题目给你一个数n,求p : 解题思路: 不需要遍历b,只需要从31开始遍历p就好了.这个方法涉及到我以前过分逃避的精度控制问题:本题会使用函数pow 而pow的返回值是double转化成int 会有损失,比如4的double表示可以使4.00000000或者3.99999999999:而我们使用 强制类型转换会截取整数部分结果就可能是3,因

UVA 10622 - Perfect P-th Powers(数论)

UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取所有质因子个数的gcd就是答案,但是这题有个坑啊,就是输入的可以是负数,负数的情况比较特殊,p只能为奇数,这时候是要把答案不断除2除到为奇数即可. 代码: #include <stdio.h> #include <string.h> #include <math.h> long long n; int prime[333333], vi

POJ 2239--Selecting Courses【二分图 &amp;&amp; 最大匹配数 &amp;&amp; 水题】

Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9363   Accepted: 4164 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Mi

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b

sdut 2841 Bit Problem (水题)

题目 贴这个题是因为看题解有更简单的方法, 我做的时候是直接算的, 也很简单. 贴一下题解吧: 如果一个整数不等于 0,那么该整数的二进制表示中至少有一位是 1. 这个题结果可以直接输出 x - (x&(x-1)); 因为x-1 之后二进制下,就是最右边的1变成了0, 最右边的1的 右边所有的0变成了1, 不影响最左边. 我的代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4

sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst

杭电(hdu)2053 Switch Game 水题

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13113    Accepted Submission(s): 7970 Problem Description There are many lamps in a line. All of them are off at first. A series of o

4.7-4.9补题+水题+高维前缀和

题目链接:51nod 1718 Cos的多项式  [数学] 题解: 2cosx=2cosx 2cos2x=(2cosx)^2-2 2cos3x=(2cosx)^3-3*(2cosx) 数归证明2cos(nx)能表示成关于2cosx的多项式,设为f(n) f(1)=x,f(2)=x^2-2(其中的x就是2cosx) 假设n=1~k时均成立(k>=3) 当n=k+1时 由cos((k+1)x)=cos(kx)cos(x)-sin(kx)sin(x) cos((k-1)x)=cos(kx)cos(x)

历年NOIP水题泛做

快noip了就乱做一下历年的noip题目咯.. noip2014 飞扬的小鸟 其实这道题并不是很难,但是就有点难搞 听说男神错了一个小时.. 就是$f_{i,j}$表示在第$i$个位置高度为$j$的时候最小点击次数 递推的话对于上升的情况只做一次,后面几次在后面再做.. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace st