哈理工 2084 A+B(二进制加法)
来源:互联网 发布:思途cms源码 编辑:程序博客网 时间:2024/06/04 01:12
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2084
A+B
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 486(143 users) Total Accepted: 173(131 users) Rating: Special Judge: No
Description
给你两个二进制数,求这两个二进制数的和。
Input
有多组测试数据。
每组测试数据有两个,分别为两个二进制数,仅由0和1组成,数字可能有前导0,每个数的长度不超过1000,且长度至少为1。
处理到文件结束。
Output
对于每组测试数据,输出一行,为两个数的和。
请去掉结果的前导0。
Sample Input
00
00
11
11101
0101
1010
Sample Output
0
100000
1111
为什么要写这样的题的解题报告呢,因为这样的题是细节处理的题目,而我的细节处理能力比较差。所以要好好的锻炼一下。
下面是AC代码:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;char a[10005];char b[10005];int main(){ while(~scanf("%s %s",a,b)) { int len1=strlen(a); int len2=strlen(b); int a1[1005],b1[1005]; memset(a1,0,sizeof(a1)); memset(b1,0,sizeof(b1)); for(int i=0; i<len1; i++) { a1[i]=a[len1-i-1]-'0'; } for(int i=0; i<len2; i++) { b1[i]=b[len2-i-1]-'0'; } int c[1010]; memset(c,0,sizeof(c)); int maxn=max(len1,len2),re=0; for(int i=0; i<maxn; i++) { c[i]=a1[i]+b1[i]+re; if(c[i]>=2) { re=c[i]/2; c[i]=c[i]%2; } else { re=0; } } if(re!=0) { while(re) { c[maxn]=re%2; re/=2; maxn++; } } int flag=0; for(int i=maxn; i>=0; i--) { if(flag==0&&c[i]!=0) { flag=1; printf("%d",c[i]); continue; } if(flag==1) printf("%d",c[i]); } if(flag==0) { printf("0"); } printf("\n"); } return 0;}
0 0
- 哈理工 2084 A+B(二进制加法)
- 哈理工oj 1000 A+B problem
- hdu1753 大明A+B(高精度加法)
- A+B for Matrices(矩阵加法)
- A + B Problem II(高精度加法)
- A + B Problem II(大数加法)
- A + B Problem II (大数加法)
- 大数加法A+B
- a+b(大数加法)
- 南阳理工之A+B
- 哈理工OJ 2285 Easy as a+b(位运算问题)
- 哈理工OJ 1710 a + b = c(尺取法)
- A+B Problem(v) 南阳理工ACM 题目844
- 二进制(高精度加法)
- HDU1002-A+B加法模版
- [BZOJ3107][CQOI2013]二进制a+b(DP)
- [CQOI2013]二进制a+b
- A+B长整数的加法(算法实现)
- Unity3D-游戏序列化
- 在WinDBG中查看函数的反汇编代码的命令
- C++的程序的相关引导geekband
- blender手册快捷键和操作技巧六
- 2016"百度之星" - 初赛(Astar Round2B)解题报告
- 哈理工 2084 A+B(二进制加法)
- Astar2016-Round2B 1003(杨辉三角,求大数组合)
- Spring4 MVC Hibernate4集成 Annotation
- 高精度对数函数的实现
- Nginx源码安装
- 数据仓库之拉链算法
- Linux-用户和用户组管理
- 123
- socketClient