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).
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\n",(int)(m)); 18 } 19 return 0; 20 }
本文作者:王陸
本文链接:https://www.cnblogs.com/wkfvawl/p/9570013.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步