题目描述

高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b]

输入输出格式

输入格式:

分两行输入a,b<=10^500

输出格式:

输出只有一行,代表A+B的值

输入输出样例

输入样例#1: 复制

1
1

输出样例#1: 复制

2

思路:显而易见是通过数组来模拟,之前写过类似的,直接用了,不过以前写的太差了。

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int main()
{ //肯定不是最优的,而且还很冗长,是我太菜了
string a, b;
char c[1000]; //这里要用到char,因为我vs过不了,嘤嘤嘤
int t, i, j, lag , m, o = 1, n; //lag是进位,m是c数组的下标,用n赋值为t,下面会解释
//cin >> t;
//n = t;
//while (t--)
//{
cin >> a >> b;
m = 0; lag = 0; //下面会将两个字符串相加一直到其中一个结束,因为位数不一定相等
for (i = a.size() - 1, j = b.size() - 1; i >= 0 && j >= 0; i--, j--)
{ //size函数是计算字符串长度的
c[m] = a[i] + b[j] - 48 + lag; //根据asc码来运算,lag为进位
lag = 0; //赋值0,只有有进位时才为1
if (c[m]>57)
{
c[m] -= 10;
lag = 1;
}
m++;
}
if (a.size() == b.size()) //如果字符串长度相等,证明已经相加结束,只判断最高位是否有进位
{
if (lag) //如果有,那么最高位只会为1
c[m++] = 49;
}
if (a.size()>b.size()) //如果第一个字符串长,那么继续拿进位和第一个字符串相加,
{ //比如说123+9,3与9相加后,1,2要继续和进位相加
if (lag) //两种情况,第一种,有进位
{
for (; i >= 0; i--) //i不用赋初值,因为i即为上次相加的位置
{
c[m] = a[i] + lag;
if (c[m] > 57) //这里应该不难理解
{
c[m] -= 10;
lag = 1;
}
else
lag = 0;
m++;
}
if (lag) //这是最高位计算后,再判断最高位是否有进位
c[m++] = 49;
}
else //第二种情况,没有进位,则保持原值不用再计算
{
for (; i >= 0; i--)
c[m++] = a[i];
}
}
if (a.size()<b.size()) //当第二个字符串位数较多时同上
{
if (lag)
{
for (; j >= 0; j--)
{
c[m] = b[j] + lag;
if (c[m] > 57)
{
c[m] -= 10;
lag = 1;
}
else
lag = 0;
m++;
}
if (lag)
c[m++] = 49;
}
else
{
for (; j >= 0; j--)
c[m++] = b[j];
}
}
//cout << "Case " << o << ":" << endl; //最后要说一下输出,要严格按照题目要求,注意数字间有空格
//cout << a << " + " << b << " = ";
for (i = m - 1; i >= 0; i--)
cout << c[i];
cout << endl; //每组数据最后都要换行,endl就是换行的意思,但每两组数据间要空上一行
//if (o<n) //所以要用到n,假如有3组数据,o初值为1,所以只有为1,2时才空行,即1与2,2与3之间空行
//cout << endl;
//o++;
//}
return 0; //光A题题解就写到12点了,睡觉喽
}

洛谷P1601 A+B Problem(高精)的更多相关文章

  1. 洛谷 P1601 A+B Problem(高精)

    P1601 A+B Problem(高精) 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式 ...

  2. 【洛谷P1601 A+B Problem(高精)】

    题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...

  3. 题解-洛谷P1601 A+B Problem(高精)

    https://www.luogu.org/problemnew/show/P1601(题目传送) 显然数据范围超过了long long类型,故不能简单的用两个长整型存起来相加.这里用到大数据的高精度 ...

  4. 洛谷——P1601 A+B Problem(高精)

    https://www.luogu.org/problem/show?pid=1601#sub 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑 ...

  5. Java实现 洛谷 P1601 A+B Problem(高精)

    import java.util.*; import java.math.*; public class Main { public static void main(String args[]) { ...

  6. 洛谷1601 A+B Problem(高精) 解题报告

    洛谷1601 A+B Problem(高精) 本题地址:http://www.luogu.org/problem/show?pid=1601 题目背景 无 题目描述 高精度加法,x相当于a+b pro ...

  7. 洛谷1303 A*B Problem 解题报告

    洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...

  8. 洛谷1001 A+B Problem

    洛谷1001 A+B Problem 本题地址:http://www.luogu.org/problem/show?pid=1001 题目描述 输入两个整数a,b,输出它们的和(|a|,|b|< ...

  9. 洛谷P1865 A % B Problem

    1.洛谷P1865 A % B Problem 题目背景 题目名称是吸引你点进来的 实际上该题还是很水的 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行 ...

随机推荐

  1. find -perm命令

    http://www.2cto.com/os/201205/130125.html find -perm,根据文件的权限来查找文件,有三种形式: find -perm mode find -perm ...

  2. wait()方法写在while循环中可以在线程接到通知后再一次判断条件

    wait()方法写在while循环中可以在线程接到通知后再一次判断条件 synchronized public String pop() { String returnValue = "&q ...

  3. MySql解压版使用

    1.解压 2.配置环境变量 3.新建空目录data,修改ini配置文件,修改basedir和datadir 4.管理员运行cmd,进入bin目录 5.mysql -install,如果提示错误,先my ...

  4. CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))

    传送门 A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. Android和H5交互-基础篇

    hybrid App开发也不是什么新鲜事了,其中native和h5之间的交互则是必不可少的.Android中是如何和H5交互的? 1.webView加载页面 我们都知道在Android中是通过webV ...

  6. golang文件读写三种方式——bufio,ioutil和os.create

    package main import ( "bufio" "fmt" "io/ioutil" "os" ) func ...

  7. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. linux 编译安装TRMPdump(libRTMP)

    需要编译libRTMP,首先需要安装配置编译环境.网上能够找到的资料多是在Windows环境编译.这里介绍一下在Linux系统中编译安装libRTMP,一来给后来者一个参考,二来也给自己做一个备忘录. ...

  9. iOS手势识别

    一.手势识别与触摸事件 1.如果想监听一个view上面的触摸事件,可选的做法是: (1)自定义一个view (2)实现view的touches方法,在方法内部实现具体处理代码 2.通过touches方 ...

  10. 【NOIP2018】 游记

    All ended? [day 0] 一点感觉没有,不过翘掉了早上的课(当然还有前三周的课),然后刚想睡一会儿,就被通知要上车了/难受 在车上玩了一会儿早上下的Super Mario(主要是早上刷了一 ...