• hdu 1060 Leftmost Digit


    Problem Description
    Given a positive integer N, you should output the leftmost digit of N^N.
     
    Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case contains a single positive integer N(1<=N<=1,000,000,000).
     
    Output
    For each test case, you should output the leftmost digit of N^N.
     
    Sample Input
    2
    3
    4
     
    Sample Output
    2
    2
    这题是不能直接算的,无论时间还是空间都吃不消。此题用取对数的方法。例如:1234567取对数后为log10(1.234567*10^6)=6+log10(1.234567)(log10(1.234567)为小数部分),而10^(log10(1.234567))为1.234567,取整就得到了我们想要的结果。
    代码如下:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int t;
        double ans,n;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%lf",&n);
            ans=log10(n);//这里我没用n*log10(n)是因为,后面取整的时候,
    // n*log10(n)会超出int的范围,当然也可以用__int64来取整。
    ans
    -=int(ans); ans*=n; ans=ans-int(ans); ans=pow(10,ans); printf("%d ",int(ans)); } return 0; }
  • 相关阅读:
    更换glibc版本进行调试
    HDCTF 2020 Writeup
    Srop 原理与利用方法
    form使用DELETE被转换为GET的原因
    sqlite将时间转换为本地时间读取
    firewall-cmd命令
    windows创建系统服务命令
    Open_vSwitch操作
    Dublin Core
    CWM(Common warehouse metamodel)
  • 原文地址:https://www.cnblogs.com/duan-to-success/p/3486701.html
Copyright © 2020-2023  润新知