题目链接: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. 【CodeForces】【321E】Ciel and Gondolas

    DP优化/四边形不等式 这题……跟邮局那题简直一模一样吧……好水的E题…… 设dp[i][j]表示前 i 艘“gondola”坐了前 j 个人,那么方程即为$dp(i,j)=min\{ dp[i-1] ...

  2. Word中字体背景有白块咋办

      如下图,主要是从新浪博客贴过来的,先用记事本很麻烦. 1. 记事本转帖,麻烦,有公式的话需要单独处理,更麻烦 2.菜单栏中的油漆桶(段落那),有时候不行. 3.粘到QQ对话框再占回来,完美. 4. ...

  3. 日志:slf4j+log4j+maven配置

    代码的日志输出,前前后后折腾了我好几次. 本着会用,快速配置的原则,还是将配置过程记录下来,以便复用. 参考:http://blog.csdn.net/anialy/article/details/8 ...

  4. yield curve

    1. A yield curve can be built using deposit rates, swap rates, and future/forward rates 2. A par-cou ...

  5. 搜索 基础 AC 2014-01-14 15:53 170人阅读 评论(0) 收藏

    题目网址:http://haut.openjudge.cn/xiyoulianxi1/1/ 1:晶矿的个数 查看 提交 统计 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 ...

  6. lca 最近公共祖先

    http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...

  7. 【bzoj1003】[ZJOI2006]物流运输

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6331  Solved: 2610[Submit][Stat ...

  8. ID3d11asynchronous

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff476428(v=vs.85).aspx 这东西 该怎么用 ! 照这位兄弟的做就可以 ...

  9. Codeforces Round #277 (Div. 2)

    整理上次写的题目: A: For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn ...

  10. 抛弃jQuery 深入原生的JavaScript

    虽然我已经做网站建设工作10多年了,但我从最近3年才开始更多地学习如何更好的将纯JavaScript用于工作中,而不总是将jQuery考虑在第一位.现在我每天学习很多东西.这个过程让我觉得Adtile ...