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).
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.
一拿到这个题,很久之前做过取最后一位数《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;}
阅读全文
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
- 33.在旋转排序的数组中查找某值位置
- c#通用配置文件读写类(xml,ini,json)
- 【SQL】连接(五):交叉连接
- HttpUrlConnection进行跨应用请求
- docker在windows下的安装和配置
- Leftmost Digit
- opengl es 纹理
- HDU-4857逃生(反向拓扑排序)(重点是思想)
- log4j2.x架构分析与实战
- http性能测试
- hdu4857 逃生
- 封装rhel6.5的虚拟机
- Struts2(十二):国际化
- Reclyclerview 的条目的textview展开,收起