模板:http://www.cnblogs.com/TQCAI/p/8410799.html


1.高精度加法训练

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>


#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 10000
#define MAX 1<<30
#define V vector<int>
#define ll long long

using namespace std;

typedef struct hp{
    int len;
    char s[LEN];
    hp(){
        len=0;
        memset(s,0,LEN);
    }
    hp(const char *ch){
        memset(s,0,LEN);
        len=strlen(ch);
        for(int i=0;i<len;i++){
            s[len-i]=ch[i]-48;
        }
    }
    void print(){
        for(int i=len;i>=1;i--){
            putchar(s[i]+48);
        }
    }
}hp;

hp add(const hp&a,const hp&b){
    int i,len=max(a.len,b.len);
    hp c;
    for(i=1;i<=len;i++){
        c.s[i]+=a.s[i]+b.s[i];
        if(c.s[i]>9){
            c.s[i+1]++;
            c.s[i]%=10;
        }
    }
    len++;
    while(len>1 && c.s[len]==0) len--;
    c.len=len;
    return c;
}

int main(){
//    freopen("A+B Problem.txt","r",stdin);
    char a[LEN];
    char b[LEN];
    scanf("%s",a);
    scanf("%s",b);
    add(hp(a),hp(b)).print();
    puts("");
    return 0; 
}
View Code

相关文章:

  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
猜你喜欢
  • 2022-12-23
  • 2021-10-10
  • 2022-01-03
相关资源
相似解决方案