Leftmost Digit
来源:互联网 发布:javascript json类型 编辑:程序博客网 时间:2024/06/05 12:47
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).
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
22HintIn 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.
1、题意:给你一个数,然后输出n的n次方的结果的首位数字。
2、思路:一看这个数就是非常大,用数组来算大数也是不行的,所以要用特殊的方法,然后从网上别的大佬那里学到了方法。
设M=N^N,则log10(M)=N*log10(N);
所以M=10^(N*log10(N));
10的整数次幂都为10的倍数,10的倍数首位都为1,其他位都为0,再考虑小数部分
即10^(N*log10(N))(取小数部分) 然后取整。
3、代码:
#include<stdio.h>
#include<math.h>
int main()
{
int t;
double n,k,a;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&n);
a=n*log10(n);
k=a-(long long)a;
printf("%d\n",int(pow(10,k)));
}
return 0;
}
4、总结:又学到了一些方法。
阅读全文
0 0
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- 【0015】navicat中把excel导入表
- [v9] v9站群模式下,子站点栏目和文章ur动态时l域名错误显示为主站链接的解决办法
- shiro之roles实现or关系的角色过滤
- jquery中attr和prop的区别
- 用LDA处理文本(Python)
- Leftmost Digit
- float double精度和取值范围
- App 优化(三)未完成
- java多线程之并行和并发
- sublime安装svn提交
- ztree随笔
- SqlServer2012清除日志文件
- 手机采用L形主板(譬如iPhone)比长方形主板的优点
- 浅谈 Vue 项目优化