现在的位置: 首页 > 综合 > 正文

求一个数的k次方的前三位。

2013年02月27日 ⁄ 综合 ⁄ 共 597字 ⁄ 字号 评论关闭
#include<stdio.h>
#include<string.h>
#define esp 1e20
int fun1(int n,int k)
{
    double res=1.0;
    double base=n*1.0;
    while(k)
    {
        if(k&1)
        {
            res*=base;
            while(res>=esp) res/=10;
        }
        base*=base;
        while(base>=esp) base/=10;
        k>>=1;
    }
    while(res>=1000) res/=10;
    return (int) res;
}
int main()
{
    int ncase,k,n;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%d%d",&n,&k);
        printf("%d\n",fun1(n,k));
    }
    return 0;
}

另一种方法:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    __int64 x,res;
    double n,k,y;
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%lf%lf",&n,&k);
        n=k*log10(n);
        x=(__int64)(n);
        y=n-x;
        res=(__int64)(pow(10,y)*100);
        printf("%I64d\n",res);
    }
    return 0;
}

抱歉!评论已关闭.