加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数运算

发布时间:2020-12-14 04:41:24 所属栏目:大数据 来源:网络整理
导读:B题 HDU1060 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)??? Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22158??? Accepted Submission(s): 8577 Problem Description Given a positive integer N,you should output the

B题 HDU1060

Leftmost Digit

Time Limit: 2000/1000 MS (Java/Others)??? Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22158??? Accepted Submission(s): 8577

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

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.

?

解题思路:

?

1、m=n^n,要取m的最高位;

2、log10(m)=log10(n^n);

3、m=10^(n*log10(n));

4、设n*log10(n)=a+b; (a为整数部分,b为小数部分)

5、则m=10^(a+b)=(10^a)*(10^b);

6、由于10^a的最高位是1,不影响结果,所以m的最高位只需考虑10^b;

7、最后对10^b的结果取整;(由于0<b<10,所以1<10^b<10,即为所求结果)

?

题目代码:

#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
int main(){
    int n,t;
    double x,y;
    LL result;
    cin>>t;
    while(t--){
        cin>>n;
        x=n*log10(n);
        y=LL(n*log10(n));
        x=x-y;
        result=(LL)pow(10.0,x);
        cout<<result<<endl; 
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读