📜  a ^ b中的位数

📅  最后修改于: 2021-04-24 04:04:12             🧑  作者: Mango

给定两个正整数a和b,任务是找到a ^ b中的位数(a升为幂b)。
例子:

Input: a = 2  b = 5
Output: no. of digits = 2
Explanation:
2^5 = 32 
Hence, no. of digits = 2

Input: a = 2  b = 100
Output: no. of digits = 31
Explanation:
2^100 = 1.2676506e+30
Hence, no. of digits = 31

方法:
a ^ b中的位数可以使用以下公式计算:

Number of Digits = 1 + b * (log10a)

当数字除以10时,它将减少1位数字。
例子:

554 / 10 = 55, 55 / 10 = 5

请注意,554最初有3位数字,但除后有2位数字55,再除后只有1位数字5。因此可以得出结论,要对数字进行计数,将数字除以10的次数达到1。需要计算。
数字的对数基数10是将数字除以10才能达到1的次数,但是由于对数基数10中不包含1本身,因此将1加1以得到数字位数。
注意:取底值b *(log 10 a)
以下是计算a ^ b中数字位数的实现。

CPP
// CPP Program to calculate
// no. of digits in a^b
#include
#include
using namespace std;
 
// function to calculate number
// of digits in a^b
int no_of_digit(int a, int b)
{
    return ((int)(b * log10(a)) + 1);
}
     
// driver program
int main()
{
    int a = 2, b = 100;
    cout <<"no. of digits = "<<
                  no_of_digit(a, b);
}
 
// This code is contributed by Smitha


Java
// Java Program to calculate
// no. of digits in a^b
class GFG {
     
    // function to calculate number
    // of digits in a^b
    static int no_of_digit(int a, int b)
    {
        return ((int)(b * Math.log10(a)) + 1);
    }
     
    // driver program
    public static void main(String[] args)
    {
        int a = 2, b = 100;
        System.out.print("no. of digits = " +
                          no_of_digit(a, b));
    }
}


Python3
# Python Program to calculate
# no. of digits in a^b
import math
 
# function to calculate number
# of digits in a^b
def no_of_digit(a, b):
    return ((int)(b * math.log10(a)) + 1)
 
# Driver Program
a = 2
b = 100
print("no of digits = ", no_of_digit(a, b))
 
# This code is contributed by Shrikant13


C#
// C# Program to calculate
// no. of digits in a^b
using System;
 
class GFG {
     
    // function to calculate number
    // of digits in a^b
    static int no_of_digit(int a, int b)
    {
        return ((int)(b * Math.Log10(a)) + 1);
    }
     
    // driver program
    public static void Main()
    {
        int a = 2, b = 100;
        Console.Write("no. of digits = " +
                        no_of_digit(a, b));
    }
}
 
// This code is contributed by Smitha.


PHP


Javascript


输出:

no.of digits = 31