51node 1005 大数加法
来源:互联网 发布:现在什么软件翻墙好 编辑:程序博客网 时间:2024/06/16 21:31
1005 大数加法
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
给出2个大整数A,B,计算A+B的结果。
Input
第1行:大数A第2行:大数B(A,B的长度 <= 10000 需注意:A B有可能为负数)
Output
输出A + B
Input示例
68932147586468711654886
Output示例
537643802472
// 模拟
#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;const int maxn = 1e5 + 10;int aa[maxn],bb[maxn];char ss1[maxn],ss2[maxn];char s1[maxn], s2[maxn];int n,m;void add(int a[],int b[]){ int k = max(n,m); for(int i = 0; i <= k; i++) { if(a[i] + b[i] >= 10) { a[i] = (a[i] + b[i]) % 10; a[i+1]++; } else a[i] = a[i] + b[i]; } while(a[k--] == 0); //printf("%d",aa[k+1]); for(int i = k+1; i >= 0; i--) printf("%d",a[i]); printf("\n");}void cha(int a[], int b[]){ int k = max(n,m); for(int i = 0; i <= k; i++) { if(a[i] < b[i]) { a[i] = a[i] + 10 - b[i]; a[i+1]--; } else a[i] = a[i] - b[i]; } while(a[k--] == 0); //printf("%d",aa[k+1]); for(int i = k+1; i >= 0; i--) printf("%d",a[i]); printf("\n");}int main(){ // while(1){ scanf("%s%s",ss1,ss2); bool falg1 = false, falg2 = false; int l = 0, p = 0; int len1 = strlen(ss1); int len2 = strlen(ss2); memset(aa,0,sizeof(aa)); memset(bb,0,sizeof(bb)); if(ss1[l] == '-') falg1 = true, l++; if(ss2[p] == '-') falg2 = true, p++; for(int i = 0 , j = l; j <= len1 - 1; i++, j++) s1[i] = ss1[j]; for(int i = 0, j = p; j <= len2 - 1; i++, j++) s2[i] = ss2[j]; //cout << s1 << endl; // cout << s2 << endl; for(int i = 0, j = len1 - 1 - l; i <= len1 - 1 - l; i++, j--) aa[i] = s1[j] - '0'; for(int j = 0, i = len2 - 1 - p; j <= len2 - 1 - p; j++, i--) bb[j] = s2[i] - '0'; n = len1 - l; m = len2 - p; //cout << n << " " << m << endl; //for(int i = 0; i < n; i++) // cout << aa[i] << " "; if(falg1 == falg2) { if(falg1){ if(n == m && m == 1) { if(s1[0] == s2[0] && s2[0] == '0') { printf("0\n"); return 0; } } printf("-"); } else { if(n == m && m == 1 && s1[0] == s2[0] && s2[0] == '0') { printf("0\n"); return 0; } } add(aa,bb); } else if(falg1) { if(n > m || (n == m && strcmp(s1,s2) > 0)) { printf("-"); cha(aa,bb); } else if(n == m && strcmp(s1,s2) == 0) printf("0\n"); else cha(bb,aa); } else { if(n > m || (n == m && strcmp(s1,s2) > 0)) cha(aa,bb); else if(n == m && strcmp(s1,s2) == 0) printf("0\n"); else{ printf("-"); cha(bb,aa); } } // } return 0;}
0 0
- 51node 1005 大数加法
- 51nod 1005 大数加法 大数运算
- 51nod-1005 大数加法
- 51nod 1005《《大数加法》》
- 51nod 1005 大数加法
- 51nod 1005 大数加法
- 51nod 1005 大数加法
- 51nod 1005 大数加法
- 51nod 1005 大数加法
- 【51nod】1005 大数加法
- 51Nod 1005 大数加法
- 51Nod--1005 大数加法
- 51Nod 1005 大数加法
- 51nod 1005 大数加法
- 51nod 1005 大数加法
- 51Nod-1005-大数加法
- 51Nod-1005-大数加法
- 51Nod 1005 大数加法
- DAY2——腾讯训练营
- xftp
- 设计模式之六:代理模式
- linux网络编程(13)——服务器模型选型
- [BZOJ3786]星系探索(dfs序+splay)
- 51node 1005 大数加法
- DUBBO研究与学习一:了解
- TOMCAT启动提示NB: JAVA_HOME should point to a JDK not a JRE解决方法
- java数据结构(二)——Set(HashSet,TreeSet),泛型,Map(HashMap,TreeMap,ConcurrentHashMap)
- 数据库常用sql整理
- 网络图片缓存显示框架之universal-image-loader-1.9.1-with-sources
- VS编译后出现LINK : fatal error LNK1123: 转换到 COFF 期间失败:
- 从Quaternion.LookRotation()和Quaternion.AngleAxis()开始
- 2017gmic大会最后一波优惠~5折