用log10()的几个用法,确定位数和取整取小数

Leftmost Digit
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11298    Accepted Submission(s): 4324

 

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.

Author

Ignatius.L

Recommend

We have carefully selected several similar problems for you:  1061 1071 1573 1066 1065 

思路:

    题意很简单:求N^N的最左边一位的数字.这道题在Math分类中难度为3.用暴力法是绝对会溢出的.

以下内容来自科学计数法-维基百科

 

科学记数法(英语:Scientific notation)是一种记数的方法。

在科学记数法中,一个数被写成一个1与10之间的实数(尾数)与一个10的次幂的积:

其中 :

(如果  是一个比1少的小数,或比 10 大,皆可改变  来表达)
 是一个整数
例子[编辑]
782300=7.823×105
0.00012=1.2×10−4
10000=1×104
回到本题,这道题很好的运用了科学计数法的特点.

假设M=N^N,

则有M=,两边同时取对数log10(M)=log10(a)+n,其中1<=|a|<10,所以log10(a)必然表示的是log10(M)的小数部分,

(因为log10(10)=1,log10(1)=0所以1<=a<10的对数肯定小于1而大于等于0),所以,当务之急是将log10(M)的小数部分找到.

这个比较容易,我们只需要log10(M)-floor(log10(M)) 即可.//floor函数表示向下取整.

 

#include<stdio.h>
#include<math.h>
#include<limits.h>
int main()
{
    int T;
    double sum,logSum,DecSum ;
 
    scanf("%d",&T) ;
    while(T--)
    {
        scanf("%lf",&sum);
        logSum = sum * log10(sum);
        DecSum=pow(10.,logSum-(long long)logSum); //用floor或者(long long),不能用(int)
        printf("%d\n",(int)DecSum);
    }
    return 0;
}
//1000000000
 

 

 

注意边界点:1和1000000000.

拓展:

log10()函数的用法

决窍一:求整数n的位数.1+(int)log10(n).(注意:其中n>0)

 

#include<stdio.h>
#include<math.h>
int main()
{
    double T,n;
    while(scanf("%lf",&n)!=EOF)
    {
        T=log10(n);
        printf("%d\n",1+(int)T); //输出n的位数
    }
    return 0;
}
 

决窍二:科学计数法中截取整数部分或者小数部分.

科学计数法表示M=a*10^m,其中1<=a<10,m为整数.两边取对数log10(M)=log10(a)+m.log10(a)表示的是小数部分,

m表示的是整数部分.要恢复a的值只需要10^log10(a).
--------------------- 
作者:Raise 
来源:CSDN 
原文:https://blog.csdn.net/linraise/article/details/14167253 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值