ACM刷题之HDU————Leftmost Digit
来源:互联网 发布:java 所有打印写入txt 编辑:程序博客网 时间:2024/05/22 08:07
清明节假期,大家都出去玩了。我却苦逼的一个人在实验室里刷题目QAQ..
Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3215 Accepted Submission(s): 1418Problem 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 Digit 的题目。
但是这题让我们求得是最高位的数字,所以不会像之前那题一样有捷径了。
如果想暴力解此题肯定会被判超时的。
这个时候我们只要用log函数就好了。
以下是大致思路:
假设一个数为 num 根据题意,我们要求num^num 的最高位。
其实num^num 根据科学计数法,可以表示为a*10^n。
a的整数位即最高位的数。
所以有 num^num=a*10^n
两侧同时取对数:num*lg(num)=lg(a)+n
则有:lg(a)=num*lg(num)-n
即:a=10^(num*lg(num)-n)
其中,n等于int(num*lg(num)) //因为int(lg(num^num))就是它的十的幂数!
因此a=10^(num*lg(num)-int(num*lg(num)) //做题时请把int换成long long int 否则会溢出超时
——————————————————————————————————————————————————
在做题时,如果要强制转换类型,请以(int)(x) 的形式转换,否则系统会判为Compilation Error
下面是代码
#include<stdio.h>#include<math.h> int main(){double a,num,zon,b;int zu;scanf("%d",&zu);while(zu--){scanf("%lf",&num);b=log10(num); //取对数 zon=num*b;a=pow(10,(zon-(long long int)(zon)));for(int i=0;a>=10;i++){a/=10;}printf("%d\n",(long long int)(a));//必须用long long int 否则还是太大溢出 }}
2 0
- ACM刷题之HDU————Leftmost Digit
- 1060—Leftmost Digit
- hdu 1060——Leftmost Digit
- 数学——Leftmost Digit
- 杭电ACM1060——Leftmost Digit
- ACM刷题之HDU————Rightmost Digit
- 杭电acm hdu-1060Leftmost Digit
- HDU ACM 1060 Leftmost Digit [数学题]
- acm steps(Leftmost Digit)
- HDU 1060 Leftmost Digit & BIT1046 Leftmost Digit
- hdu 1060 Leftmost Digit
- hdu 1060 Leftmost Digit
- HDU 1060 Leftmost Digit
- hdu 1060 Leftmost Digit
- hdu 1060 Leftmost Digit
- HDU 1060 LeftMost Digit
- HDU 1060 Leftmost Digit
- hdu Leftmost Digit
- JSON包常用的方法
- oracle物理dg安装:方法一
- break退出循环
- Android XML数据解析
- DCOS之MarathonSSL与基本认证篇
- ACM刷题之HDU————Leftmost Digit
- 第六周上机实践项目-项目2-带武器的游戏角色
- 第六周项目2游戏类(武器)
- 关于mybatis中,批量增删改查以及参数传递的问题
- 欢迎使用CSDN-markdown编辑器
- PuTTY免输密码自动登录Linux
- 理解LSTM 网络Understanding LSTM Networks
- 设计模式分类
- 1260: 逆反的01串