A + B problem 高精度

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;
    int i1 = a.size() - 1;
    int i2 = b.size() - 1;
    string s;
    int carry = 0;
    while (i1 >= 0 || i2 >= 0)
    {
        char ch = carry;
        if (i1 >= 0)
        {
            if (a[i1] < ‘0‘ || a[i1] > ‘9‘)
                continue;
            ch += a[i1] - ‘0‘;
        }
        if (i2 >= 0)
        {
            if (b[i2] < ‘0‘ || b[i2] > ‘9‘)
                continue;
            ch += b[i2] - ‘0‘;
        }
        if (ch >= 10)
        {
            carry = 1;
            ch -= 10;
        }
        else carry = 0;
        s.push_back(ch + ‘0‘);
        i1--;
        i2--;
    }
    if (carry)
    s.push_back(‘1‘);
    reverse(s.begin(), s.end());
    return s;
}
时间: 2024-08-06 11:54:21

A + B problem 高精度的相关文章

洛谷 P1303 A*B Problem 高精度乘法

P1303 A*B Problem 时空限制1s / 128MB 题目描述 求两数的积. 输入输出格式 输入格式: 两行,两个数. 输出格式: 积 输入输出样例 输入样例#1: 1 2 输出样例#1: 2 说明 每个数字不超过10^2000,需用高精 ------------------------------------------------------------------------------------------------ 既然有了高精度加减法,那就有高精度乘法 跟我们平时计算

hdu 1002 Sum Problem(高精度)

高精度:一位一位存 #include<stdio.h> #include<string.h> main() { int n,l1,l2,i,j,k,m,p; char a[1000],b[1000],c[1000],d[1000],s[1001]; while(scanf("%d ",&n)!=EOF) { for(p=1; p<=n; p++) { scanf("%s %s",a,b); l1=strlen(a); l2=st

Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description A group of k students from Cooking University living in the campus decided that each day of the semester one of them will p

hdoj 1002 A + B Problem II 高精度 java

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

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

洛谷 P1303 A*B Problem(高精度乘法) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接 :https://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两行,两个数. 输出格式: 积 输入输出样例 输入样例#1: 1 2 输出样例#1: 2 说明 每个数字不超过10^2000,需用高精 AC代码: 1 #include<cstdio> 2 #include<algorithm> 3 #include<iost

CodeForces 464E The Classic Problem | 呆克斯歘 主席树维护高精度

题意描述 有一个\(n\)点\(m\)边的无向图,第\(i\)条边的边权是\(2^{a_i}\).求点\(s\)到点\(t\)的最短路长度(对\(10^9 + 7\)取模). 题解 思路很简单--用主席树维护每个点的\(dis\).因为每次更新某个点\(v\)的\(dis_v\)的时候,新的\(dis_v\)都是某个点\(u\)的\(dis_u + 2^{w_{u, v}}\),相当于在原先\(u\)对应的主席树基础上修改,得到新的一棵主席树,作为\(v\)对应的主席树. 主席树(线段树)维护二

(string高精度)A + B Problem II hdu1002

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

P1303 A*B Problem(高精度乘法)

P1303 A*B Problem 模拟就好了.\(c_ {i+j} +=a_i \times b_j\).时间复杂度 \(O(n*m)\) (FFT版可以做到 \(O((n+m)\log (n+m)\)) #include<bits/stdc++.h> using namespace std; string times(string a,string b) { int aa[15000]={0},bb[15000]={0},ans[30000]={0}; string str="&