• Leftmost Digit(数学)


    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. 


    题目意思:求n^n结果的第一位。
    解题思路:我们可以将其看成幂指函数,那么我们对其处理也就顺其自然了,n^n = 10 ^ (log10(n^n)) = 10 ^ (n * log10(n)),
    然后我们可以观察到: n^n = 10 ^ (N + s) 其中,N 是一个整数 s 是一个小数。由于10的任何整数次幂首位一定为1,所以首位只和s(小数部分)有关。

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #define ll long long int
     5 using namespace std;
     6 int main()
     7 {
     8     int t,n;
     9     double m;
    10     scanf("%d",&t);
    11     while(t--)
    12     {
    13         scanf("%d",&n);
    14         m=n*log10(n);
    15         m=m-(ll)(m);
    16         m=pow(10.0,m);
    17         printf("%d
    ",(int)(m));
    18     }
    19     return 0;
    20 }
  • 相关阅读:
    2021.5.16 Android聊天功能
    2021.5.15 Android Gestures示例
    2021.5.14 程序员修炼之路:从小工到专家阅读笔记02
    KL 散度和交叉熵
    UBOOT学习
    UCOSII学习
    cortex-M3/M4体系学习
    一步步写RTOS
    38 操作系统-中断处理与特权级转移
    MDK、IAR将变量定义到指定位置
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9570013.html
Copyright © 2020-2023  润新知