【模拟】高精度练习之加法

原题传送门

思路



简单的高精度加法,无需解释~~~

Code


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
using namespace std;

string a,b;
int A[501],B[501],C[501],i,len;

int main()
{
    cin>>a>>b;
    for(i=1;i<=a.length();i++)
        A[a.length()-i+1]=a[i-1]-'0';
    for(i=1;i<=b.length();i++)
        B[b.length()-i+1]=b[i-1]-'0';
    len=max(a.length(),b.length());
    for(i=1;i<=len;i++)
    {
        C[i+1]=(A[i]+B[i]+C[i])/10;
        C[i]=(A[i]+B[i]+C[i])%10;
    }
    if(C[len+1]!=0)len++;
    for(i=len;i>=1;i--)
        cout<<C[i]; 

    return 0;
}

原文地址:https://www.cnblogs.com/gongdakai/p/11296119.html

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

【模拟】高精度练习之加法的相关文章

hdu 1316 How Many Fibs? (模拟高精度)

题目大意: 问[s,e]之间有多少个 斐波那契数. 思路分析: 直接模拟高精度字符串的加法和大小的比较. 注意wa点再 s 可以从 0 开始 那么要在判断输入结束的时候注意一下. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct node { char str[111]; int len; void

3116 高精度练习之加法

3116 高精度练习之加法 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出两个正整数A和B,计算A+B的值.保证A和B的位数不超过500位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Output Description 输出A+B的值 样例输入 Sample Input 3 12 样例输出 Sample Output 15 数据范围及提示 Data Size & Hint

高精度运算 【加法】【减法】

高精度算是我高中期间没有学明白的知识点之一,其实挺简单的东西.核心思路是[按位模拟竖式运算],说白了就是模拟题. 加法减法从低位到高位模拟,因为会进位借位 乘法也从低到高因为进位 除法要从高到低因为我们手算除法时也是从高到低保留余数的. 高精度减法:https://www.luogu.org/problemnew/show/P2142 1 #include<iostream> 2 using namespace std; 3 4 string a1,b1; 5 int a[10005],b[1

uva 424(Integer Inquiry)高精度大整数加法

这是一道很标准的大整数加法,我却wa了4次,没提交一次就查到一些细节问题,比如说我们考虑前导 0的问题,还有就是没有对输入数组处理, 使得他们每次输入时高位的置0,还有就是没考虑到最后相加后的进位, 这些问题一一改正之后,还是wa了,原来是因为,我把if语句中的==只写了一个...真坑啊,,,我就说怎么会 不过,明明写的对的,大数相加竟然还wa了四次,还有就是这道题最后不写换行也会wa...看来还是有必要从基础练起提高代码能力: 贴代码: #include<stdio.h> #include&

wiki oi 3116 高精度练习之加法

题目描述 Description 给出两个正整数A和B,计算A+B的值.保证A和B的位数不超过500位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Output Description 输出A+B的值 样例输入 Sample Input 3 12 样例输出 Sample Output 15 数据范围及提示 Data Size & Hint 两个正整数的位数不超过500位 分析:和减法一样,去掉了两个数的大小比较,发现没必要,还有发现不用判断cc大于10么

3116 高精度练习之加法——http://codevs.cn/problem/3116/

第一部分:题目 题目描述 Description 给出两个正整数A和B,计算A+B的值.保证A和B的位数不超过500位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Output Description 输出A+B的值 样例输入 Sample Input 3 12 样例输出 Sample Output 15 数据范围及提示 Data Size & Hint 两个正整数的位数不超过500位 第二部分:思路 由于数值比较大,所以用字符串形式接收.因为相加是从个

Multiply Strings(字符串乘法模拟,包含了加法模拟)

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 要求:字符串表示的数字可能无穷大,并且非负. class Solution { private: vector<string> tempStrs; public: string add

洛谷 P1604 B进制星球 高精度多进制加法

P1604 B进制星球 时空限制1s / 128MB 题目背景 进制题目,而且还是个计算器~~ 题目描述 话说有一天,小Z乘坐宇宙飞船,飞到一个美丽的星球.因为历史的原因,科技在这个美丽的星球上并不很发达,星球上人们普遍采用B(2<=B<=36)进制计数.星球上的人们用美味的食物招待了小Z,作为回报,小Z希望送一个能够完成B进制加法的计算器给他们. 现在小Z希望你可以帮助他,编写实现B进制加法的程序. 输入输出格式 输入格式: 共3行第1行:一个十进制的整数,表示进制B.第2-3行:每行一个B

高精度加法(进制)

1 /* 2 高精度进制加法 3 n为进制(n<=36) 4 */ 5 #include<bits/stdc++.h> 6 using namespace std; 7 const int maxn=10000; 8 int n; 9 struct bign{ 10 int d[maxn],len; 11 inline bign(){len=1;memset(d,0,sizeof(d));} 12 inline bign operator = (const char* num) 13 {