题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1005

要处理符号,还要特别注意0和连续进位等情况。偷懒使用strcmp,但是前提必须是长度相等,否则是字典序。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
char ca[maxn], cb[maxn], cs[maxn]; void add(char* ca, char* cb) {
int a[maxn];
int b[maxn];
memset(a, , sizeof(a));
memset(b, , sizeof(b));
int la = strlen(ca);
int lb = strlen(cb);
for(int i = ; i < la; i++) a[i] = ca[la-i-] - '';
for(int i = ; i < lb; i++) b[i] = cb[lb-i-] - '';
for(int i = ; i < maxn; i++) {
a[i] += b[i];
a[i+] += a[i] / ;
a[i] %= ;
}
int p = maxn;
while(p--) if(a[p] != ) break;
for(int i = p; i >= ; i--) printf("%d", a[i]);
printf("\n");
} void sub(char* ca, char* cb) {
int a[maxn];
int b[maxn];
memset(a, , sizeof(a));
memset(b, , sizeof(b));
int la = strlen(ca);
int lb = strlen(cb);
for(int i = ; i < la; i++) a[i] = ca[la-i-] - '';
for(int i = ; i < lb; i++) b[i] = cb[lb-i-] - '';
for(int i = ; i < la; i++) {
if(a[i] >= b[i]) a[i] -= b[i];
else {
a[i] = a[i] - b[i] + ;
a[i+]--;
}
}
int p = maxn;
while(p--) if(a[p] != ) break;
for(int i = p; i >= ; i--) printf("%d", a[i]);
printf("\n");
} int main() {
// freopen("in", "r", stdin);
memset(ca, , sizeof(ca));
memset(cb, , sizeof(cb));
scanf("%s %s", ca, cb);
int la = strlen(ca);
int lb = strlen(cb);
if(ca[] == '' && cb[] == '') printf("0\n");
else if(ca[] == '-' && cb[] == '-') {
printf("-");
add(ca+, cb+);
}
else if(ca[] != '-' && cb[] != '-') add(ca, cb);
else if(ca[] == '-' && cb[] != '-') {
if(la - < lb) sub(cb, ca+);
else if(strcmp(ca+, cb) > ) {
printf("-");
sub(ca+, cb);
}
else if(strcmp(ca+, cb) == ) printf("0\n");
else sub(cb, ca+);
}
else if(ca[] != '-' && cb[] == '-') {
if(la - < lb) sub(ca, cb+);
else if(strcmp(ca, cb+) > ) sub(ca, cb+);
else if(strcmp(ca, cb+) == ) printf("0\n");
else {
printf("-");
sub(cb+, ca);
}
}
return ;
}

[51NOD]大数加法(模拟)的更多相关文章

  1. 51NOD 大数加法以及python写法

    练练 大数加法一般为小学生式的"竖式计算"要特别注意的是借位与进位的问题(先给看c++写法,我怕先看了python写法,会看不下去c++写法)这题还有要注意的是 1.同符号的话,直 ...

  2. 51Nod大数加法(两个数正负都可)

    很多大数的问题都运用模拟的思想,但是这个说一样也一样,但是难度较大,很麻烦,我自己谢写了100多行的代码,感觉很对,但就是WA.其实个人感觉C和C++没有大数类,是对人思想和算法的考验,但是有时候做不 ...

  3. POJ 2506 Tiling (递推 + 大数加法模拟 )

    Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7965   Accepted: 3866 Descriptio ...

  4. 大数高精度加减乘除 51nod 1005 大数加法

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...

  5. 51nod 1005 大数加法

    #include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...

  6. c#大数加法

    在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...

  7. 【大数加法】POJ-1503、NYOJ-103

    1503:Integer Inquiry 总时间限制:  1000ms 内存限制:  65536kB 描述 One of the first users of BIT's new supercompu ...

  8. 大数加法之C语言函数法(只有正数版)

         由于某些原因,我于今天2017-4-19将我的博文搬到博客园了,以后我就在这里扎根了.         之前想过在博客写文章方便日后复习,但一直未能实现,所以,现在这篇是我个人人生中第一篇博 ...

  9. 51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

随机推荐

  1. 剑指offer--面试题20

    题目:从外向里顺时针打印矩阵 做题心得:该题本质上并未考查复杂的数据结构及算法,而是考查了快速找规律的能力!!! 要想作出此题,必须先有绝对清晰的思路,否则越写越乱(因为涉及到很多的循环打印) 自己当 ...

  2. [百度空间] [转]程序员趣味读物:谈谈Unicode编码

    出处:CSDN [ 2005-05-13 10:05:53 ] 作者:fmddlmyy 这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG ...

  3. function的prototype

    prototype只有function才有的属性. var a = function() { this.age = 12; this.name = "haha"; }; a.pro ...

  4. android 关于Location of the Android SDK has not been setup in the preferences的解决方法

    今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...

  5. jquery json遍历和动态绑定事件

    <div id='tmpselectorList' style='border: 1px solid grey;max-height: 150px;position:absolute;text- ...

  6. Sqli-labs less 34

    Less-34 本关是post型的注入漏洞,同样的也是将post过来的内容进行了 ' \ 的处理.由上面的例子可以看到我们的方法就是将过滤函数添加的 \ 给吃掉.而get型的方式我们是以url形式提交 ...

  7. c# 获取mac地址的2种方法

    和大家分享下,互相学习一下吧.第一个获取方法好像获取不到mac地址,我用了第二种方法可以获取到.希望知道的可以说下为什么. 1,首先要添加引用:using System.Management; 2,代 ...

  8. 【一】php 操作符

    1.php单引号和双引号的区别 单引号和双引号都能表示字符串,但是单引号不能识别里面带有转义字符'/'和变量的字符串,所以需要""去表示这种字符串.或者使用<<< ...

  9. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  10. 传说中的WCF(5):数据协定(a)

    在第4篇中,咱们了解了发送/接收SOAP头,从本篇开头,我们不妨更深入地去探求一下有关WCF中的消息到底是啥玩意儿.WCF庞大而复杂,而从 MSDN文档中,你会看到许多很专业很抽象的东西,你不禁会问, ...