HDU 1060 Leftmost Digit (数学/大数)
2015-09-03 17:58
429 查看
Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14954 Accepted Submission(s): 5775
[align=left]Problem Description[/align]
Given a positive integer N, you should output the leftmost digit of N^N.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, you should output the leftmost digit of N^N.
[align=left]Sample Input[/align]
2
3
4
[align=left]Sample Output[/align]
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.
[align=left]Author[/align]
Ignatius.L
[align=left]Recommend[/align]
We have carefully selected several similar problems for you: 1018 1071 1573 1066 1004
没有思路,直接百度的
m=n^n;
linag两边同时log10
log10(m)=log10(n^n)=n*log10(n);
所以,m=10^(n*log10(n));
m的最左边一位由n*log10(n)的小数部分决定。m=10^((n*log10(n))的整数部分)+10^((n*log10(n))的小数数部分);10的整数次方肯定是1000……,最左边一位由10^((n*log10(n))的整数部分)决定。
#include<queue> #include<math.h> #include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<algorithm> using namespace std; int main() { int T;cin>>T; while(T--) { double n; cin>>n; double m=n*log10(n); double p=m-(long long)m; double q=pow(10,p); cout<<(long long)q<<endl; } return 0; }
以后遇到大数,可以考虑log,考虑转换成指数形式,
另外大数数学的题,不要用int,用 long long,第一次就是用int,所以wa了一次。
相关文章推荐
- [LeetCode] Majority Element
- 虚拟机(vm)上redhat连接Xshell详解
- poj 2456 Aggressive cows(二分搜索之最大化最小值)
- python学习笔记(-)
- Java进程优雅关闭
- C++基础---字符串(string型字符串)
- CentOS 7 安装问题:dracut_initqueue[599]: Warning: Could not boot 原因,及解决办法
- 文章标题
- loadrunner安装注意事项
- CSS绘制Android Robot
- 文章标题
- maven -- 问题解决(二)解决“Could not calculate build plan”问题
- 模拟DVD管理器(及遇到的问题与解决)
- 迭代器模式(三):销售管理系统中数据的遍历的迭代器模式解决方案
- Count Complete Tree Nodes
- js正则表达式的使用与详解
- poj 2352 Stars (树状数组)
- NSLocale中常用的语言代码对照表
- 三十分钟掌握 STL
- Linux系统启动流程