之前做过的一道题,现在还是拿出来谢谢自己的心得,不是为了叠加博客的内容,而是为了这样每做一道题会一种方法,坚持下去(多吃饭,多运动,多敲代码,多写博客,多陪女朋友,)为了自己的健康,自己开心快乐
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 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.代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,t;
double a1,a2;
cin>>t;
while(t--){
cin>>n;
a1=n*(log10(n*1.0));
//cout<<a1<<endl;
a2=a1-(__int64)a1;
//cout<<a2<<endl;
cout<<(int)pow(10.0,a2)<<endl;
}
return 0;
}
#include<cmath>
using namespace std;
int main()
{
int n,t;
double a1,a2;
cin>>t;
while(t--){
cin>>n;
a1=n*(log10(n*1.0));
//cout<<a1<<endl;
a2=a1-(__int64)a1;
//cout<<a2<<endl;
cout<<(int)pow(10.0,a2)<<endl;
}
return 0;
}
m=n^n,两边分别对10取对数得 log10(m)=n*log10(n),
得m=10^(n*log10(n)),由于10的任何整数次幂首位一定为1,
所以m的首位只和n*log10(n)的小数部分有关
得m=10^(n*log10(n)),由于10的任何整数次幂首位一定为1,
所以m的首位只和n*log10(n)的小数部分有关