计算对数

#include <iostream>
#include <cmath>
#include <cstring>
const int MAXN = 300;

using namespace std;

/* 大数类,重载流运算,== < > 运算 */
class Bigint
{
public:
    friend istream& operator >> (istream& input, Bigint& b);
    friend ostream& operator << (ostream& output, Bigint& b);
    friend bool operator == (Bigint& b1, Bigint& b2);
    friend Bigint operator * (Bigint& b1, Bigint& b2);
    friend bool operator < (Bigint& b1, Bigint& b2);
    int length;
private:
    char num[MAXN + 10];
};

istream& operator >> (istream& input, Bigint& b)
{
    char buf[MAXN + 10];
    input.getline(buf, MAXN + 10);
    int m = 0;
    for(int i = strlen(buf) - 1; i >= 0; i--)
    {
        b.num[m++] = buf[i];
    }
    b.num[m] = 0;
    b.length = m;
    return input;
}

ostream& operator << (ostream& output, Bigint& b)
{
    for(int i = b.length - 1; i >= 0; i--)
    {
        cout << b.num[i];
    }
    return output;
}

bool operator < (Bigint& b1, Bigint& b2)
{
    if(b1.length < b2.length)
    {
        return true;
    }
    else if(b1.length > b2.length)
    {
        return false;
    }
    else
    {
        for(int i = b1.length - 1; i >= 0; i--)
        {
            if(b1.num[i] < b2.num[i])
            {
                return true;
            }
            else if(b1.num[i] > b2.num[i])
            {
                return false;
            }
        }
        return false;
    }
}

bool operator == (Bigint& b1, Bigint& b2)
{
    if(b1.length != b2.length)
    {
        return false;
    }
    else
    {
        for(int i = 0; i < b1.length; i++)
        {
            if(b1.num[i] != b2.num[i])
            {
                return false;
            }
        }
        return true;
    }
}

/* 高精度乘法 */
Bigint operator * (Bigint& b1, Bigint& b2)
{
    Bigint t;
    int temp[MAXN + 10] = {};
    int i, j;
    for(i = 0; i < b1.length; i++)
    {
        for(j = 0; j < b2.length; j++)
        {
            temp[i + j] += (b1.num[i] - '0') * (b2.num[j] - '0');
        }
    }
    for(i = 0; i < MAXN + 10; i++)
    {
        if(temp[i] >= 10)
        {
            int ten = temp[i] / 10;
            temp[i] %= 10;
            temp[i + 1] += ten;
        }
    }
    for(i = MAXN; i >= 0; i--)
    {
        if(temp[i] != 0)
        {
            break;
        }
    }
    t.length = i + 1;
    for(i = 0; i < t.length; i++)
    {
        t.num[i] = temp[i] + '0';
    }
    return t;
}

int main()
{
    int nTest;
    cin >> nTest;
    cin.ignore();

    for(; nTest != 0; nTest--)
    {
        Bigint num1, num2;
        cin >> num1 >> num2;
        int x = 1;
        Bigint result;
        if(num1 < num2)
        {
            result = num1;
            while(result < num2)
            {
                x++;
                result = result * num1;
            }
            cout << x - 1 << endl;
        }
        if(num1 == num2 || num2 < num1)     // 这种情况下 0 一定满足条件
        {
            cout << 0 << endl;
        }
        cin.ignore();                       // 滤除一个换行符
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值