首页 > 代码库 > hdu1060

hdu1060

求N^N最左边的一位

1.转化为小数,快速幂

2.数学公式

************************************************

#include<cstdio>
#include<cmath>
using namespace std;

long long n;
double n0;

int cal(long long x)
{
    return (int)log10(x);
}

int ksm(double a,long long b)
{
    double tmp=a,ret=1.0;
    while(b)
    {
        if(b & 1)
        {
            ret*=tmp;
            if(ret>10)ret/=10;
        }
        tmp*=tmp;
        if(tmp>10)tmp/=10;
        b>>=1;
    }
    return (int)ret;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        n0=(double)n/pow(10.0,cal(n));
        int ans=ksm(n0,n);
        printf("%d\n",ans);
    }
    return 0;
}

************************************************

************************************************

#include<cstdio>
#include<cmath>
using namespace std;

int cal(long long x)
{
    long long tmp1=(long long)(log10(x)*x);
    double tmp2=x*log10(x);
    double tmp=tmp2-tmp1;
    int ret=(int)pow(10.0,tmp);
    return ret;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n;
        scanf("%lld",&n);
        int ans=cal(n);
        printf("%d\n",ans);
    }
    return 0;
}

************************************************