hdu 1002 A + B Problem II(大数)

题意:整数大数加法

思路:大数模板

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

#define MAXN 9999//万进制
#define DLEN 4//4位

class BigNum{
private:
    int a[500];//可以控制大数位数(500*4)
    int len;//大数长度
public:
    BigNum(){//构造函数
        len=1;
        memset(a,0,sizeof(a));
    }
    BigNum(const int);//将int转化为大数
    BigNum(const char*);//将字符串转化为大数
    BigNum(const BigNum &);//拷贝构造函数
    BigNum &operator=(const BigNum &);//重载复制运算符,大数之间赋值

    BigNum operator+(const BigNum &)const;//大数+大数
    BigNum operator-(const BigNum &)const;//大数-大数
    BigNum operator*(const BigNum &)const;//大数*大数
    BigNum operator/(const int &)const;//大数/int

    BigNum operator^(const int &)const;//幂运算
    int operator%(const int &)const;//取模
    bool operator>(const BigNum &)const;//大数与大数比较
    bool operator>(const int &)const;//大数与int比较

    void print();//输出大数
};

BigNum::BigNum(const int b){//将int转化为大数
    int c,d=b;
    len=0;
    memset(a,0,sizeof(a));
    while(d>MAXN){
        //c=d-(d/(MAXN+1))*(MAXN+1);
        c=d%(MAXN+1);//取出后四位
        d=d/(MAXN+1);//
        a[len++]=c;
    }
    a[len++]=d;
}

BigNum::BigNum(const char *s){//将字符串转化为大数
    int t,k,index,l,i,j;
    memset(a,0,sizeof(a));
    l=strlen(s);
    len=l/DLEN;
    if(l%DLEN)++len;
    index=0;
    for(i=l-1;i>=0;i-=DLEN){
        t=0;
        k=i-DLEN+1;
        if(k<0)k=0;
        for(j=k;j<=i;++j)
            t=t*10+s[j]-‘0‘;
        a[index++]=t;
    }
}

BigNum::BigNum(const BigNum &T):len(T.len){//拷贝构造函数
    int i;
    memset(a,0,sizeof(a));
    for(i=0;i<len;++i)
        a[i]=T.a[i];
}

BigNum &BigNum::operator=(const BigNum &n){//重载复制运算符,大数之间赋值
    int i;
    len=n.len;
    memset(a,0,sizeof(a));
    for(i=0;i<len;++i)
        a[i]=n.a[i];
    return *this;
}

BigNum BigNum::operator+(const BigNum &T)const{//大数+大数
    BigNum t(*this);
    int i,big;//位数
    big=T.len>len?T.len:len;
    for(i=0;i<big;++i){
        t.a[i]+=T.a[i];
        if(t.a[i]>MAXN){
            ++t.a[i+1];
            t.a[i]-=MAXN+1;
        }
    }
    if(t.a[big]!=0)t.len=big+1;
    else t.len=big;
    return t;
}

BigNum BigNum::operator-(const BigNum &T)const{//大数-大数
    int i,j,big;
    bool flag;
    BigNum t1,t2;//t1大的,t2小的
    if(*this>T){
        t1=*this;
        t2=T;
        flag=0;//前面的大
    }
    else{
        t1=T;
        t2=*this;
        flag=1;//前面的小
    }
    big=t1.len;
    for(i=0;i<big;++i){
        if(t1.a[i]<t2.a[i]){
            j=i+1;
            while(t1.a[j]==0)++j;
            --t1.a[j--];
            while(j>i)t1.a[j--]+=MAXN;
            t1.a[i]+=MAXN+1-t2.a[i];
        }
        else t1.a[i]-=t2.a[i];
    }
    while(t1.a[t1.len-1]==0&&t1.len>1){
        --t1.len;
        --big;
    }
    if(flag)t1.a[big-1]=-t1.a[big-1];//前面的小,结果为负
    return t1;
}

BigNum BigNum::operator*(const BigNum &T)const{//大数*大数
    BigNum ret;
    int i,j,up;
    int temp,temp1;
    for(i=0;i<len;++i){
        up=0;
        for(j=0;j<T.len;++j){
            temp=a[i]*T.a[j]+ret.a[i+j]+up;
            if(temp>MAXN){
                //temp1=temp-temp/(MAXN+1)*(MAXN+1);
                temp1=temp%(MAXN+1);
                up=temp/(MAXN+1);
                ret.a[i+j]=temp1;
            }
            else{
                up=0;
                ret.a[i+j]=temp;
            }
        }
        if(up!=0)ret.a[i+j]=up;
    }
    ret.len=i+j;
    while(ret.a[ret.len-1]==0&&ret.len>1)--ret.len;
    return ret;
}

BigNum BigNum::operator/(const int &b)const{//大数/int
    BigNum ret;
    int i,down=0;
    for(i=len-1;i>=0;--i){
        ret.a[i]=(a[i]+down*(MAXN+1))/b;
        down=a[i]+down*(MAXN+1)-ret.a[i]*b;
    }
    ret.len=len;
    while(ret.a[ret.len-1]==0&&ret.len>1)--ret.len;
    return ret;
}

BigNum BigNum::operator^(const int &n)const{//幂运算
    BigNum t,ret(1);
    int i;
    if(n<0)exit(-1);
    if(n==0)return 1;
    if(n==1)return *this;
    int m=n;
    while(m>1){
        t=*this;
        for(i=1;i<<1<=m;i<<=1){
            t=t*t;
        }
        m-=i;
        ret=ret*t;
        if(m==1)ret=ret*(*this);
    }
    return ret;
}

int BigNum::operator%(const int &b)const{//取模
    int i,d=0;
    for(i=len-1;i>=0;--i){
        d=((d*(MAXN+1))%b+a[i])%b;
    }
    return d;
}

bool BigNum::operator>(const BigNum &T)const{//大数与大数比较
    int ln;
    if(len>T.len)return true;
    else if(len==T.len){
        ln=len-1;
        while(a[ln]==T.a[ln]&&ln>=0)--ln;
        if(ln>=0&&a[ln]>T.a[ln])return true;
        else return false;
    }
    else return false;
}

bool BigNum::operator>(const int &t)const{//大数与int比较
    BigNum b(t);
    return *this>b;
}

void BigNum::print(){//输出大数
    int i;
    printf("%d",a[len-1]);
    for(i=len-2;i>=0;--i){
        printf("%.4d",a[i]);//%.4d代表4位,不够前面补0
    }
    printf("\n");
}

int main(){
    int t,i;
    char str1[1024],str2[1024];
    BigNum a,b,c;
    scanf("%d",&t);
    for(i=1;i<=t;++i){
        scanf("%s%s",str1,str2);
        a=BigNum(str1);
        b=BigNum(str2);
        c=a+b;
        printf("Case %d:\n",i);
        printf("%s + %s = ",str1,str2);
        c.print();
        if(i<t)printf("\n");
    }

    return 0;
}

时间: 2024-11-05 12:08:00

hdu 1002 A + B Problem II(大数)的相关文章

HDU 1002 A + B Problem II(两个大数相加)

详细题目点击:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20)

抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)

数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. 1 while (a) //将每位数字取出来,取完为止 2 { 3 num1[i]=a%10; //将每一个各位取出存在数组里面,实现了将数字反转 4 i++; //数组的变化 5 a/=10; 6 } 趁热打铁 例题:hdu 4554 叛逆的小明 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid

HDU 1002 A + B Problem II(大整数相加)

A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input c

hdu 1002 A + B Problem II(大数模拟加法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目要求:输入t 为测试组数,然后输入两个正数 a,b求和: [思路] 首先,用字符串读入a,b,再将a,b 逆序存到数组中,然后对应位相加就行了,长度小的数要注意补零 倒过来做可以让进位变得方便 ,如果对应位的和大于等于10,就把余数储存在当前位置,后一位加1: [代码] #include <iostream> #include <string> #include <cs

题解报告:hdu 1002 A + B Problem II(大数加法)

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

hdu 1002.A + B Problem II 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define max 1010 5 char a[max], b[max]; 6 7 int main() 8 { 9 int i, k, s, c, T, len1, len2; 10 scanf("%d&quo

HDU - 1002 A + B Problem II (大整数加法)

一道很基础的大整数加法. 简单的说一下思路吧. 先用字符串读取两个大数.首先需要把数组给初始化为0方便以后处理,然后对数组逆序对齐处理,接着相加转化后的两个数组并把之存进结果数组里面,最后对结果数组进行进位处理. 看代码吧. #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <queue> #include <stac

hdu 1002 A + B Problem II(大正整数相加)

代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; char s1[10000],s2[10000],s3[10000]; int main() { int t; scanf("%d",&t); int tt=0; while(t--) { ++tt; if(tt!=1) printf("\n"

杭电 1002 A + B Problem II(大数处理)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 209179    Accepted Submission(s): 40226 Problem Description I have a very simple problem for you. Given two integers A and B, yo