首页 技术 正文
技术 2022年11月14日
0 收藏 739 点赞 4,106 浏览 1404 个字

 

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11011 Accepted Submission(s): 4214

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).

Output

For each test case, you should output the leftmost digit of N^N.

Sample Input

2
3
4

Sample Output

2
2HintIn 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. 一开始用java做,果断超时,不过用java给我的感觉是,几天没用,就感觉有点陌生了,把这个代码记录下来,还是挺不错的: 
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int i;
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for(i=0;i<n;i++){
BigInteger b =cin.nextBigInteger();
int c = b.intValue();
b = b.pow(c);
String e = b.toString();
System.out.println(e.charAt(0));
}
}
}

题解思路,利用公式n=10^x*m=>lgn=x+lg(m);

具体步骤:

1.对M=N^N两边取对数得log10(M)=N*log10(N),即M=10^(N*log10(N))

2.要求M的最高位,则令N*log10(N)=a+b;b是小数(0<=b<1),a是整数。

3.因为10的任何整数次幂首位一定为1,所以,M的首位只和N*log10(N)的小数部分有关,

#include<iostream>
using namespace std;
int main(){
int n,i,result;
long long s;
cin>>n;
while(n--){
cin>>s;
double x1 = s*log10( 1.0*s);
double x2 = x1 - (long long)x1;
result = 0;
result = (int)pow(10.0,x2);
cout<<result<<endl;
}
return 0;
}

所以只用求10^b就可以了。(1<=10^b<10)

4.求出b也很简单,只要用double类型的(N*log10(N))去减去long long类型的(N*log10(N))。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781