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;
    }
    
  • 相关阅读:
    php自动跳转中英文页面
    如何让一个层关闭之后,就算刷新页面了也不显示。除非关闭页面再次打开
    彻底杜绝warning: Cannot add header information headers already sent in......
    大三了
    X牛人關於編程語錄
    C++虚函数表解析
    epoll:Edge or Level Triggered
    LRU和LFU的区别
    会话、进程组与僵死进程
    Effective C++学习笔记:确定基类有虚析构函数
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4669357.html
Copyright © 2011-2022 走看看