【HDOJ】A Math Problem(对pow()的理解)

A Math Problem

http://acm.hdu.edu.cn/showproblem.php?pid=6182

题意:输入一个n,问有多少个k的k次幂<=n(k=1,2,3...)

思路:不能用pow。因为double pow( double x, double y );x不能为负数且y为小数,或者x为0且y小于等于0,返回幂指数的结果。

   pow()是用数值逼近的方法给出的,并不保证严格精确,执行效率也不高。

   于是就用循环写了k的k次,顺手打了个表发现16的16就满足了题里数据范围。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[20]={0,1,4,27,256,3125,46656,823543,16777216,387420489,10000000000,285311670611,8916100448256,302875106592253,11112006825558016,437893890380859375}, i;
int main()
{
    ll n;
    while(cin >> n)
    {
        int sum = 0;
        for( i = 1; i < 16; i++)
        {
            if(n >= a[i])
                sum++;
            if(n < a[i])
                break;
        }
        cout << sum << endl;
    }

}
原文地址:https://www.cnblogs.com/Kohinur/p/8893174.html