C++超长整数数字计数器

C++ very long integer digit counter

我有一个任务要用 C++ 创建一个程序,计算极长整数的位数。

我有两个随机数 a 和 b (1 <= a, b <= 10^16)。我需要找出数字 a^b(a 的 b 次方)有多少位数。 (我不需要知道数字本身,只需要知道它有多少位数)

我对如何解决这个问题没有什么想法,我想到的一种方法是创建一个单独的结构来保存这些非常长的整数并能够对它们进行乘法运算,但这需要很长时间做那么多次乘法,程序的时限是1s。也许 C++ 中有某种函数或类似的东西可以帮助我?

提前感谢您的帮助!

一个数字N在基数B中的位数是floor(logBN) + 1.所以你需要计算floor (log10(ab)) + 1 等于 floor(b*log10 a) + 1

floor 将数字向下舍入到最接近的整数。 <cmath>

中提供了 floor 和 log10 函数

使用对数,这可以用 "small" 个数字来解决: 日志 (a^b) = 日志 (a) * b

您要找的号码是

auto exp = math.log10(a) * b;

您需要将结果四舍五入为高于此值的较小整数

你会从用户那里获取数字,你会做 (int temp) 在代码中使用它,你会调用 (cmath) 库中的 pow 函数,如果你想打印两个数字的幂,调用这个函数,你将做一个从 (1) 到 (power number) 的 for 循环,在这个循环中你将等于 temp 和 counter 并且在循环外你将打印这个 temp 这是代码。

#include <iostream>
#include <cmath>
using namespace std;

void power_digit();

int main()
{
    power_digit();   
}
void power_digit()
{
    int x , y ,temp ;
    string result;
    cout << "\nEnter number one :";
    cin >> x ;
    cout << "\nEnter number two :";
    cin >> y ;
    cout << "The power of x and y is : "<< pow(x,y);
      for (int i = 1 ; i < y ; i++)
    {
        temp =  i ;
    }
    cout << "\nThe digit of result is :" << temp;
}