题解 P1601 【A+B Problem(高精)】
P1601 A+B Problem(高精)
题目描述
高精度加法,x相当于a+b problem,b不用考虑负数。
输入输出格式
输入格式:
分两行输入a,b<=10^500
输出格式:
输出只有一行,代表A+B的值
输入输出样例
1
1
2
比较简单的高精度。
高精度。顾名思义,就是在很大的位数情况下进行运算。(炸int)
其基本思想就是用数组进行模拟加法。
模拟近位。
最后遍历数组输出。
附代码:
#include <cstdio>
#include <iostream>
using namespace std; struct diff{
int len;short x[]; void read(){
int len_read;
string read;
cin>>read;
len=;len_read=read.size();
for(int i=len_read-;i>=;i--)x[++len]=read[i]-;
} void write(){
for(int i=len;i>=;i--)printf("%d",x[i]);
printf("\n");
return ;
} struct diff operator+(struct diff tmp){
int c=;
struct diff ans;
ans.len=tmp.len;if(len>ans.len)ans.len=len;
for(int i=;i<=ans.len;i++)
{
ans.x[i]=x[i]+tmp.x[i]+c;
c=ans.x[i]/;ans.x[i]%=;
}
if(c>)ans.x[++ans.len]=c;
return ans;
}
}a,b; int main(){
a.read();b.read();a=a+b;a.write();
return ;
}
我们每个人都应该做到精益求精,所以还可以将普通高精度升华一下
那就是传说中的万进制高精度:
#include <iostream>
#include <cstdio>
using namespace std;
string s1,s2;
int c,len,len1,a[],len2,b[]; void turn()
{
int x,y,lx=s1.size(),ly=s2.size(),l,r;
int c[],d[];
l=lx/;r=ly/;
x=lx-l*;y=ly-r*;
for(int i=;i<x;i++)c[]=c[]*+s1[i]-;
for(int i=;i<y;i++)d[]=d[]*+s2[i]-;
for(int i=;i<=l+;i++)c[i-(x==)]=(s1[*(i-)+x]-)*+(s1[*(i-)+x+]-)*+(s1[*(i-)+x+]-)*+(s1[*(i-)+x+]-);
for(int i=;i<=r+;i++)d[i-(y==)]=(s2[*(i-)+y]-)*+(s2[*(i-)+y+]-)*+(s2[*(i-)+y+]-)*+(s2[*(i-)+y+]-);
len1=l+(x>);len2=r+(y>);
for(int i=;i<=len1;i++)a[i]=c[len1-i+];
for(int i=;i<=len2;i++)b[i]=d[len2-i+]; } int main()
{
cin>>s1>>s2;
turn();
len=len1;if(len2>len)len=len2;
for(int i=;i<=len;i++)
{
a[i]=a[i]+b[i]+c;
c=a[i]/;
a[i]%=;
}
if(c>)a[++len]=c;
printf("%d",a[len]);
for(int i=len-;i>=;i--)printf("%04d",a[i]);
return ;/*万进制高精加*/
}
万进制高精加要注意输出“printf("%04d",a[i]);”
点个赞吧↓
题解 P1601 【A+B Problem(高精)】的更多相关文章
- [CEOI2007]树的匹配Treasury(树形DP+高精)
题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...
- 洛谷 P1601 A+B Problem(高精)
P1601 A+B Problem(高精) 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式 ...
- 【洛谷P1601 A+B Problem(高精)】
题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式: 分两行输入a,b<=10^500 ...
- 【洛谷p1601】A+B Problem(高精)
高精度加法的思路还是很简单容易理解的 A+B Problem(高精)[传送门] 洛谷算法标签: 附上代码(最近懒得一批) #include<iostream> #include<cs ...
- 洛谷1601 A+B Problem(高精) 解题报告
洛谷1601 A+B Problem(高精) 本题地址:http://www.luogu.org/problem/show?pid=1601 题目背景 无 题目描述 高精度加法,x相当于a+b pro ...
- 【题解】洛谷P2532 [AHOI2012]树屋阶梯(卡特兰数+高精)
洛谷P2532:https://www.luogu.org/problemnew/show/P2532 思路 来自Sooke大佬的推导: https://www.luogu.org/blog/Sook ...
- 【题解】洛谷P1066 [NOIP2006TG] 2^k进制数(复杂高精+组合推导)
洛谷P1066:https://www.luogu.org/problemnew/show/P1066 思路 挺难的一道题 也很复杂 满足题目要求的种数是两类组合数之和 r的最多位数m为 w/k(当w ...
- [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)
Description 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不 ...
- BZOJ_1002_[FJOI2007]_轮状病毒_(递推+高精)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1002 )*&*(^&*^&*^**()*) 1002: [FJOI20 ...
随机推荐
- Spring笔记02_注解_IOC
目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...
- Java Socket网络编程学习笔记(一)
0.前言 其实大概半年前就已经看过网络编程Socket的知识了(传统IO),但是因为长时间的不使用导致忘的一干二净,最近正好准备校招,又重新看了网络编程这一章, 是传统IO(BIO)相关的内容,故在此 ...
- 事件绑定on与hover事件
今天项目中UI设计了一个鼠标划入和划出的效果,本来这个小效果是非常简单的!可是在实际的生产环境中就出现了一点点问题!因为在实际的环境中,数据全部是用ajax异步加载进去的,这样就造成了hover方法不 ...
- 使用 CODING 进行 Spring Boot 项目的集成
本文作者:CODING 用户 - 高文 持续集成 (Continuous integration) 是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可 ...
- kafka环境搭建
kafka环境搭建 for mac 对应qq群号:616961231 在之前的文章中, 有学习能力和兴趣爱好的同学,自己动手维护测试环境,丰衣足食是最好的办法,今天我们来讲讲kafka在mac上的安装 ...
- TreeView 节点拖拽
public Form1() { InitializeComponent(); treeView1.AllowDrop = true; treeView1.ItemDrag += new ItemDr ...
- spring boot 扫描不到自定义Controller
使用springboot启动类配置扫描的两种注解配置方式: 1.@Controller @EnableAutoConfiguration @ComponentScan 2.@SpringBoo ...
- python3 dict(字典)
clear(清空字典内容) stu = { 'num1':'Tom', 'num2':'Lucy', 'num3':'Sam', } print(stu.clear()) #输出:None copy( ...
- python文件上传
1.前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- java图片上传及图片回显1
目的:选择图片,进行图片回显之后将图片保存到服务器上(PS:没有使用任何插件,样式很丑) 实现方式: js+servlet+jsp的方式来实现 事先准备: 文件上传处理在浏览器中是以流的形式提交到服务 ...