hdu 1060 Leftmost Digit

来源:互联网 发布:网络上没有找到打印机 编辑:程序博客网 时间:2024/05/29 04:42

题目:

Leftmost Digit

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 1060

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

Sample Output

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. 

题目大意:

给一个数n,求n^n的第一位

题目思路:

1、因为n*n如果用大数来计算,那必定会超时,而且题目只要求第一位,所以需要其他方法来求解

2、用数学方法就得化简转化

n^n=10^m+a     // a的int型就是要求的答案

N*lgn=m+lga

10^(n*lgn-m)=a

而m为n*lgn的整数部分

所以原式=10^fomd(n*lgn);

程序:

 

 

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<cmath>#include<algorithm>#include<cctype>#include <fstream>#include <limits>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <cassert>using namespace std;int main(){    int ci;    scanf("%d",&ci);    while(ci--)    {        double  n;        cin>>n;         int ans=pow(10,fmod(n*log10(n),1));        cout<<ans<<endl;;    }return 0;}

0 0