Leftmost Digit

来源:互联网 发布:java流行框架2017 编辑:程序博客网 时间:2024/06/03 08:50

Leftmost Digit

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


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
234
 

Sample Output
22
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.

一拿到这个题,很久之前做过取最后一位数《Rightmost Number》,用的快速幂来求,以为这个题也能直接用这个方法来求,但是数据太大,无法处理(可能有办法处理吧,大数据处理没学好。。。),然后上网看了人家的解题报告,才发现会用公式真的能少好多功夫。

#include<cstdio>#include<cstring>#include<cctype>#include<algorithm>#include<set>#include<cstring>#include<string>#include<iostream>#include<cmath>#include<map>#include<vector>#include<stack>using namespace std;/*要用到几个数学公式的处理,首先:我们设x=n^n,两边同取对数可得,log10(x)=n*log10(n),那么x=10^(n*log10(n)),因为log10(n)得出来的是一个浮点类型的数据,所以n*log10(n)必是一个整数加上小数的形式又因为,10的任何整数次方首位肯定是1,所以我们只要算出10的小数次幂即可。*/int main(){    int t;    scanf("%d",&t);    while(t--){        double a,k,n;        scanf("%lf",&n);        a=n*log10(n);        k=a-(__int64)a;//这一步也是要用__int64取整,不然会WA        printf("%d\n",int(pow(10,k)));    }    return 0;}




原创粉丝点击