美文网首页
简单的最高位 Leftmost Digit1060

简单的最高位 Leftmost Digit1060

作者: 碧影江白 | 来源:发表于2016-07-21 23:52 被阅读168次

    Leftmost Digit

    http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=11

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1524 Accepted Submission(s): 689 

    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
    Hint

    In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2. In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.

    解答:

    此题的解法非常简单。由于unsigned int 0~4294967295

    int -2147483648~2147483647    所以数据N的接收可以采用标准的int型。而由于 N^N便不一定在这个范围内了,所以不能用int型地址存放N*N。

    既然那个数太大无法确定大小处理,那么就不储存它好了。我们直接提取出来这个数中我们需要运用到的信息便可。

    那么我们需要提取出来的信息是什么呢?就是这个数的位数和最高位上的数值。

    1、怎样求一个数的位数呢?答案是求对数,即求以十为底该数的对数。

    2、怎样求最高位的数值呢?答案是以该对数小数部分为指数,10为底数的值取整。

    不懂得参见下述解答:

    一个数只要不是十的次幂数,对以十为底求对数的解一定是非整数。如:

    200=100*2

       ———》log10(200)=log10(100)+log10(2)   

       ———》log10(100)=2           log10(2)=0.301 

       ———》log10(200)=2.301

    故求N^N的位数:

    double k=(double)log10(N^N)=N*log10(N)   //其整数部分就是位数,之所以不取整是为了进一步计算最高位数值。

    double ans=k-floor(k)               //得到非整数数值,即N^N除以10的其位数次幂以后得到的数对以十为底取对数。

    (int )pow(10.0,ans) 则用来进行下一步的运算,把N^N换算成大于1小于10的非整数,该非整数的整数部位即是最高位的数值。

    代码如下:

        

    scanf(

    "%d"

    , &t);

        

    while

    (t--){

            

    scanf(

    "%d"

    , &n);

            doublek = n * log10(double(n));      

            double ans = k - floor(k);         printf("%d\n", (int)pow(10.0, ans));     }

    相关文章

      网友评论

          本文标题:简单的最高位 Leftmost Digit1060

          本文链接:https://www.haomeiwen.com/subject/wqtijttx.html