zoukankan      html  css  js  c++  java
  • UVA-11029 Leading and Trailing

    Apart from the novice programmers, all others know that you can’t exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double datatype format, but you won’t get all the digits of the result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.

    Input

    The first line of input will be an intege.T<1001, where T represents the number of test cases. Each of the next T lines contains two positive integers,n and k.n will fit in

    32 bit integer and k will be less than 10000001.

    Output

    For each line of input there will be one line of output. It will be of the formt.LLL:::TTT, where LLL represents the first three digits of n,k and TTT represents the last three digits of n,k. You are assured that n k will contain at least 6 digits.

    Sample Input

    2

    123456 1

    123456 2

    Sample Output

    123...456

    152...936

    题目大意:两个数n、k,求n^k的前三位数字与后三位数字。

    题目解析:后三位数字很容易求,通过幂取模即可。先考虑n不做幂运算时如何取前三位,令d=log10(n),则d=a(整数部分)+i(小数部分),n=10^d=10^(a+i)=10^a*10^i。

    则10^i*100即为前三位。以123456为例,d=log10(123456)=5+i,则123456=10^i*10^5。又因为123456=1.23456*10^5,所以10^i=1.23456,所以前三位为10^i*100=123。

    再来考虑n做幂运算时,将n^k变换为10^(klog(n)),则d=k*log10(n),接下来就不必细说了。

    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<cstring>
    # include<string>
    # include<cmath>
    # include<algorithm>
    using namespace std;
    int mypow(long long a,long long b)
    {
        if(b==0)
            return 1;
        if(b==1){
            a%=1000;
            return a;
        }
        long long t=mypow(a,b/2);
        t*=t;
        t%=1000;
        if(b&1)
            t*=a;
        t%=1000;
        return t;
    }
    int main()
    {
        int T,k,i;
        long long n;
        scanf("%d",&T);
        while(T--)
        {
            cin>>n>>k;
            int ans2=mypow(n,k);
            double u=k*log10(n);
            double ans1=pow(10,u-(int)u)*100;
            printf("%d...%03d
    ",(int)ans1,ans2);
        }
        return 0;
    }
    
  • 相关阅读:
    比较.NET程序集(DLL或EXE)是否相同
    [转] JavaScript数组去重(12种方法)
    [转] js网络请求跨域问题汇总(携带cookie)
    [转] JS中arr.forEach()如何跳出循环
    [转] vue前端异常监控sentry实践
    [转] vue父组件触发子组件事件
    [转] vue 自定义组件使用v-model
    [转] Nginx配置性能优化
    [转] linux 查找文本过滤grep正则表达式命令详解用法
    [转] Nginx配置中的location、root、alias
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4669357.html
Copyright © 2011-2022 走看看