大数处理,带大数开根号

#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;

struct Bigint
{
    int len;
    int arg[1005];
    Bigint()
    {
        len = 1;
        memset(arg, 0, sizeof(arg));
    }
    void print()
    {
        for (int i = len - 1; i >= 0; i--){
            cout<<arg[i];
        }
        cout<<endl;
    }
    void CharToBigint(char *str)
    {
        len = strlen(str);
        for (int i = 0; i < len; i++){
            arg[i] = str[len - i - 1] - 48;
        }
        while(arg[len-1] == 0){
            len--;
        }
    }
    friend bool operator==(Bigint r, Bigint w)
    {
        if (r.len != w.len) return false;
        int i = r.len - 1;
        while (i >= 0){
            if (r.arg[i] != w.arg[i])
                return false;
            i--;
        }
        return true;
    }
    friend bool operator<=(Bigint r, Bigint w)
    {
        if (r.len < w.len) return true;
        else if (w.len < r.len) return false;
        int i = r.len - 1;
        while (i >= 0){
            if (r.arg[i]<w.arg[i])
                return true;
            else if (r.arg[i]>w.arg[i])
                return false;
            i--;
        }
        return true;
    }
    friend Bigint operator+(int r, Bigint w){
        w.arg[0] += r;
        int i = 0;
        while (w.arg[i] >= 10){
            w.arg[i + 1] += w.arg[i] / 10;
            w.arg[i] = w.arg[i] % 10;
            i++;
        }
        if(w.arg[i]) i++;
        w.len = i > w.len ? i : w.len;
        return w;
    }
    friend Bigint operator+(Bigint w, int r)
    {
        w.arg[0] += r;
        int i = 0;
        while (w.arg[i] >= 10){
            w.arg[i + 1] += w.arg[i] / 10;
            w.arg[i] = w.arg[i] % 10;
            i++;
        }
        if(w.arg[i]) i++;
        w.len = i > w.len ? i : w.len;
        return w;
    }
    friend Bigint operator-(Bigint r, Bigint w)
    {
        for (int i = 0; i < r.len; i++){
            if (r.arg[i] >= w.arg[i])
                r.arg[i] = r.arg[i] - w.arg[i];
            else{
                r.arg[i] = r.arg[i] + 10;
                r.arg[i + 1] = r.arg[i + 1] - 1;
                r.arg[i] = r.arg[i] - w.arg[i];
            }
        }
        while (r.arg[r.len - 1] == 0 && r.len > 1)
            r.len--;
        return r;
    }
    friend Bigint operator*(int x, Bigint w)
    {
        Bigint r;
        if(x == 0 || (w.len == 1 && w.arg[0] == 0)){
            return r;
        }
        for (int i = 0; i < w.len; i++){
            r.arg[i] += w.arg[i] * x;
            r.arg[i + 1] += r.arg[i] / 10;
            r.arg[i] = r.arg[i] % 10;
        }
        int i = r.arg[w.len] == 0 ? w.len-1 : w.len;
        while (r.arg[i] >= 10){
            r.arg[i + 1] = r.arg[i] / 10;
            r.arg[i] = r.arg[i] % 10;
            i++;
        }
        r.len = (i >= 0) ? i + 1 : 1;
        return r;
    }
    friend Bigint operator*(Bigint r, Bigint w)
    {
        Bigint v;
        if((r.len == 1 && r.arg[0] == 0)|| (w.len == 1 && w.arg[0] == 0)){
            return v;
        }
        for (int i = 0; i < r.len; i++){
            for (int k = 0; k < w.len; k++){
                v.arg[i + k] += w.arg[k] * r.arg[i];
                v.arg[i + k + 1] += v.arg[i + k] / 10;
                v.arg[i + k] = v.arg[i + k] % 10;
            }
        }
        int i = w.len + r.len - 1;
        i = v.arg[i] == 0 ? i-1 : i;
        while (v.arg[i] >= 10){
            v.arg[i + 1] = v.arg[i] / 10;
            v.arg[i] = v.arg[i] % 10;
            i++;
        }
        v.len = (i >= 0) ? i + 1 : 1;
        return v;
    }
    Bigint sqrt()
    {
        Bigint w, r;
        w.len = r.len = 0;
        int lens = len - 1;
        if(len == 1 && arg[0] == 1)
            return *this;
        r.arg[r.len++] = arg[lens--];
        if (len % 2 == 0)
            r = arg[lens--] + 10 * r;
        while (lens >= -1){
            int i = 0;
            while ((i*(i + 20 * w)) <= r){
                i++;
            }
            i--;
            if (i == -1 || (r.len == 1 && r.arg[0] == 1))
                i = 0;
            r = r - (i*(i + 20 * w));
            w = i + 10 * w;
            if(lens >= 0){
                r = arg[lens--] + 10 * r;
                r = arg[lens--] + 10 * r;
            }
            else
                lens -= 2;
        }
        return w;
    }
};
int main()
{
    char a[1005],b[1005];
    Bigint x,y,sum;
    while(cin>>a>>b){
        x.CharToBigint(a);
        y.CharToBigint(b);
        sum=x.sqrt()*y.sqrt();
        sum.print();

    }
}

上述模版没有除法,这里补充除法模板

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
 
const int maxn = 1000;
 
struct bign{
    int d[maxn], len;
 
	void clean() { while(len > 1 && !d[len-1]) len--; }
 
    bign() 			{ memset(d, 0, sizeof(d)); len = 1; }
    bign(int num) 	{ *this = num; } 
	bign(char* num) { *this = num; }
    bign operator = (const char* num){
        memset(d, 0, sizeof(d)); len = strlen(num);
        for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
        clean();
		return *this;
    }
    bign operator = (int num){
        char s[20]; sprintf(s, "%d", num);
        *this = s;
		return *this;
    }
 
    bign operator + (const bign& b){
        bign c = *this; int i;
        for (i = 0; i < b.len; i++){
        	c.d[i] += b.d[i];
        	if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
		}
		while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
		c.len = max(len, b.len);
		if (c.d[i] && c.len <= i) c.len = i+1;
        return c;
    }
    bign operator - (const bign& b){
        bign c = *this; int i;
        for (i = 0; i < b.len; i++){
        	c.d[i] -= b.d[i];
        	if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
		}
		while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
		c.clean();
		return c;
    }
    bign operator * (const bign& b)const{
        int i, j; bign c; c.len = len + b.len; 
        for(j = 0; j < b.len; j++) for(i = 0; i < len; i++) 
			c.d[i+j] += d[i] * b.d[j];
        for(i = 0; i < c.len-1; i++)
            c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
        c.clean();
		return c;
    }
    bign operator / (const bign& b){
    	int i, j;
		bign c = *this, a = 0;
    	for (i = len - 1; i >= 0; i--)
    	{
    		a = a*10 + d[i];
    		for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
    		c.d[i] = j;
    		a = a - b*j;
    	}
    	c.clean();
    	return c;
    }
    bign operator % (const bign& b){
    	int i, j;
		bign a = 0;
    	for (i = len - 1; i >= 0; i--)
    	{
    		a = a*10 + d[i];
    		for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
    		a = a - b*j;
    	}
    	return a;
    }
	bign operator += (const bign& b){
        *this = *this + b;
        return *this;
    }
 
    bool operator <(const bign& b) const{
        if(len != b.len) return len < b.len;
        for(int i = len-1; i >= 0; i--)
            if(d[i] != b.d[i]) return d[i] < b.d[i];
        return false;
    }
    bool operator >(const bign& b) const{return b < *this;}
    bool operator<=(const bign& b) const{return !(b < *this);}
    bool operator>=(const bign& b) const{return !(*this < b);}
    bool operator!=(const bign& b) const{return b < *this || *this < b;}
    bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}
 
    string str() const{
        char s[maxn]={};
        for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
        return s;
    }
};
 
istream& operator >> (istream& in, bign& x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
 
ostream& operator << (ostream& out, const bign& x)
{
    out << x.str();
    return out;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值