XDOJ 1046 - 高精度模板综合测试 - [高精度模板]

题目链接:http://acm.xidian.edu.cn/problem.php?id=1046

题目描述

请输出两个数的和,差,积,商,取余。注意不要有前导零。

输入

多组数据,每组数据是两个整数A,B(0<=A<=10^100,0<B<=10^100).

输出

对于每组数据,输出五个整数,分别代表A+B,A-B,A*B,A/B,A%B.

样例输入
5 2
11 3

样例输出
7 3 10 2 1
14 8 33 3 2

模板注释:

本模板只能处理十进制非负大整数。

AC代码:

#include<bits/stdc++.h>
using namespace std;

struct BigInt
{
    int len,d[205];

    void clean(){while(len>1 && !d[len-1]) len--;}
    string str()const
    {
        string s;
        for(int i=0;i<len;i++) s+=d[len-1-i]+‘0‘;
        return s;
    }

    BigInt(){memset(d,0,sizeof(d));len=1;}
    BigInt(int num){*this=num;}
    BigInt(char* num){*this=num;}

    bool operator<(const BigInt& oth)const
    {
        if(len!=oth.len) return len<oth.len;
        for(int i=len-1;i>=0;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
        return false;
    }
    bool operator>(const BigInt& oth)const{return oth<*this;}
    bool operator<=(const BigInt& oth)const{return !(oth<*this);}
    bool operator>=(const BigInt& oth)const{return !(*this<oth);}
    bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
    bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);}

    BigInt 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;
    }
    BigInt operator=(int num)
    {
        char s[20];
        sprintf(s,"%d",num);
        return *this=s;
    }
    BigInt operator+(const BigInt& oth)const
    {
        BigInt c;
        c.len=max(len,oth.len);
        for(int i=0;i<=c.len;i++) c.d[i]=0;
        for(int i=0;i<c.len;i++)
        {
            c.d[i]+=(i<len?d[i]:0)+(i<oth.len?oth.d[i]:0);
            c.d[i+1]+=c.d[i]/10;
            c.d[i]%=10;
        }
        c.len+=(c.d[c.len]>0);
        c.clean();
        return c;
    }
    BigInt operator-(const BigInt& oth)const
    {
        BigInt c=*this;
        if(c<oth) printf("Produce negative number!\n");
        int i;
        for(i=0;i<oth.len;i++)
        {
            c.d[i]-=oth.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;
    }
    BigInt operator*(const BigInt& oth)const
    {
        BigInt c;
        for(int i=0;i<len;i++) for(int j=0;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
        for(int i=0;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+1]+=c.d[i]/10, c.d[i]%=10;
        c.clean();
        return c;
    }
    BigInt operator/(const BigInt& oth)const
    {
        BigInt c=*this, r=0;
        for(int i=0;i<len;i++)
        {
            r=r*10+c.d[len-1-i];
            int j;
            for(j=0;j<10;j++) if(r<oth*(j+1)) break;
            c.d[len-1-i]=j;
            r=r-oth*j;
        }
        c.clean();
        return c;
    }
    BigInt operator%(const BigInt& oth)
    {
        BigInt r=0;
        for(int i=0;i<len;i++)
        {
            r=r*10+d[len-1-i];
            int j;
            for(j=0;j<10;j++) if(r<oth*(j+1)) break;
            r=r-oth*j;
        }
        return r;
    }
    BigInt operator+=(const BigInt& oth)
    {
        *this=*this+oth;
        return *this;
    }
    BigInt operator*=(const BigInt& oth)
    {
        *this=*this*oth;
        return *this;
    }
    BigInt operator-=(const BigInt& oth)
    {
        *this=*this-oth;
        return *this;
    }
    BigInt operator/=(const BigInt& oth)
    {
        *this=*this/oth;
        return *this;
    }
};
istream& operator>>(istream& in, BigInt& x)
{
    string s;
    in>>s;
    x=s.c_str();
    return in;
}
ostream& operator<<(ostream& out,const BigInt& x)
{
    out<<x.str();
    return out;
}

int main()
{
    BigInt a,b;
    while(cin>>a>>b)
    {
        cout<<a+b<<" ";
        if(a<b) cout<<‘-‘<<b-a<<" ";
        else cout<<a-b<<" ";
        cout<<a*b<<" ";
        cout<<a/b<<" ";
        cout<<a%b<<endl;
    }
}

原文地址:https://www.cnblogs.com/dilthey/p/9824826.html

时间: 2024-08-10 10:20:15

XDOJ 1046 - 高精度模板综合测试 - [高精度模板]的相关文章

问题 B: 【高精度】简单高精度加法

问题 B: [高精度]简单高精度加法 时间限制: 1 Sec  内存限制: 64 MB提交: 94  解决: 27[提交][状态][讨论版] 题目描述 修罗王解决了计算机的内存限制问题,终于可以使用电脑进行大型的魔法运算了,他交给邪狼的第一个任务是计算两个非负整数A.B的和,其中A和B的位数在5000位以内. 输入 共两行数据,第一行为一个非负整数A,第二行为一个非负整数B,A.B的位数均在5000以内. 输出 输出一个非负数,即两数之和. 样例输入 1111111111 2222222222

Cacti 模板(图形模板、数据模板、主机模板)与自定义监控脚本

Cacti定义了三种类型的模板,分别是 主机模板 数据模板 图形模板     主机模板(Host templates),它是图像模板和数据查询的一个集合,描述了监控某一类型的机器需要生成那些图像. 数据模板(Data templates),它描述了 Cacti 存储哪些数据到指定类型的 RRD 文件.该模板与 RRDTool 工具的 create 命令相关. 图形模板(Graph templates),描述了生成的一张图像应该是什么样子的.包括使用哪些数据模板.展示哪些元素.是否使用 CDEF

函数模板与类模板

函数模板,顾名思义,是在生成函数时依照的模板. 有时,我们需要对不同的数据类型做同样的函数操作. 比如:分别对一个int类型数 和 一个double类型数求平方. 这时,虽然都是同样的求平方操作(函数体内代码一样),但是我们必须要编写两个不同的函数,因为处理int类型的函数的参数和返回值类型都应该是int,而处理double类型的函数的参数和返回值都应该是double. 如下:函数体内操作代码一样,设计为重载函数,用相同的函数名,但是参数类型和返回值类型却都不一样,函数需要定义两次. int S

中石油-【高精度】简单高精度加法

问题 B: [高精度]简单高精度加法 时间限制: 1 Sec  内存限制: 64 MB提交: 63  解决: 20[提交][状态][讨论版] 题目描述 修罗王解决了计算机的内存限制问题,终于可以使用电脑进行大型的魔法运算了,他交给邪狼的第一个任务是计算两个非负整数A.B的和,其中A和B的位数在5000位以内. 输入 共两行数据,第一行为一个非负整数A,第二行为一个非负整数B,A.B的位数均在5000以内. 输出 输出一个非负数,即两数之和. 样例输入 1111111111 2222222222

[自制模板引擎] 为模板引擎增加解析表达式功能

通过上一片已经能把占位符替换成对应的变量的值,实现了一个简单的迷你模板引擎,模板引擎一个不错的功能就是能够计算一些简单的表达式,计算表达式的核心其实就是执行字符串,执行字符串的方法多种多样eval,Function,setTimeout等等,都能执行,至于用哪种方法,就仁者见仁智者见智了. 下面整理一下在模板引擎中会遇到的表达式: { a + b } //普通的计算表达式 { a == false } //普通的比较 { a === false } //全等比较 { a !== false} {

模板类继承模板类

今天写的代码提交到OJ上就出现这样的错误,但是vs并不会出错. '_elem' was not declared in this scope 原因在于模板类继承模板类,子类看不见父类的成员. 但是VC++做了一些小拓展,可以不适用this->就调用父类成员. gcc在扫描到模板类时就要求确定每一个成员在哪里声明的,VC++在类实例化之后再检测,就不会有这个问题. 可以使用以下方式解决: 方法1: 使用this template<typename T> class A { protecte

类模板和函数模板

函数模板: 函数模板全特化:所谓特化,是指相对普通模板的特化,是另外一个模板.但不是实例,只是模板 template <class T>                                      //普通函数模板,泛型T mymax(const T t1, const T t2){   return t1 < t2 ? t2 : t1;} template <>const char* mymax(const char* t1,const char* t2)  

函数模板,函数模板重载,可变参数模板,函数模板覆盖,通过引用交换数据

 1.函数模板初级,如果想使用模板,需要实例化,实例化的方式是加上<数据类型> #include <iostream> //函数模板可以对类型进行优化重载,根据类型会覆盖 //如果仍然要使用模板函数,需要实例化 template<class T> T add(T a, T b) { std::cout << "T add " << std::endl; return a + b; } int add(int a, int

广度优先(BFS) ------- 模板1:-----模板2:--------模板3:

//使用数组queue[ ]存放结点队列 void BFS( ) { head=0; tail=1; queue[head]=首结点的值; while (head<tail) //队列不空 { temp=tail; for (k=head; k<=tail; k++) //对当前层扩展 { if ( 到达目的状态 ) { 输出结果; return; } for (i=1; i<=m; i++) //每个结点的m种扩展可能 if (可以扩展) { 处理每种可能情况: queue[temp+