我有一个任务,要用C++创建一个程序,该程序将计算极长整数的数字。
我得到两个随机数 a 和 b(1 <= a, b <= 10^16(。我需要找到一个数字 a^b(a 的 b 次方(有多少位数字。(我不需要知道数字本身,只需要知道它有多少位数(
我对如何解决这个问题几乎没有想法,我想到的一种方法是创建一个单独的结构,该结构将容纳这些非常长的整数并能够对它们进行乘法,但是进行乘法需要很长时间多次,并且程序的时间限制是 1s。也许C++中有某种功能或类似的东西可以帮助我?
提前感谢您的帮助!
基数B
中N
的数字位数为 floor(logBN( + 1。所以你需要计算地板(对数10(a b(( + 1,它等于地板(b*log10a( + 1
floor 将数字向下舍入到最接近的整数。floor 和 log10 函数都可以在 <cmath>
库中
使用对数,这可以用"小"数字来解决:日志 (a^b( = 日志 (a( * b
您正在寻找的数字将是
auto exp = math.log10(a) * b;
您需要将 thix 结果舍入到高于此值的较低整数
您将从用户那里获取数字,您将执行(int temp(在代码中使用它,您将从(cmath(库中调用pow函数,如果您想打印调用此函数的两个数字的幂,您将执行从(1(到(幂数(的for循环,在此循环中,您将等于temp和计数器,并且循环外,您将打印此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;
}