题目链接: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. Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...

  2. eclipse安装androidSDK地址,Android SDK Manager简介

    eclipse安装android插件地址:https://dl-ssl.google.com/android/eclipse 这个和安装其他插件方式一样:Help—Install New Softwa ...

  3. date format 精辟讲解

    link: http://stackoverflow.com/questions/19533933/nsdateformatter-how-to-convert-wed-23-oct-2013-045 ...

  4. js生成唯一值的函数

    利用了js的闭包性质 var uniqueNumber = (( function(){ var value = 0; return function(){ return ++value; }; }) ...

  5. 3[doses] ------一种诡异的写法

    在 head first c 的第60页,有这么一道题: 一个富翁因为服药过度而死亡. 下面是自动服药器的代码: #include <stdio.h> int main(void) { , ...

  6. EditText 属性

    android:layout_gravity="center_vertical" 设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint=&quo ...

  7. android 四大组件Broadcast Receiver

    本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...

  8. 无线Ad-hoc网络的关键技术之路由(转)

    无线Ad-hoc网络的关键技术之路由http://network.51cto.com/art/201003/189719.htm

  9. MySQL5.6 Replication主从复制(读写分离) 配置完整版

    MySQL5.6 Replication主从复制(读写分离) 配置完整版 MySQL5.6主从复制(读写分离)教程 1.MySQL5.6开始主从复制有两种方式: 基于日志(binlog): 基于GTI ...

  10. lintcode:线段树的查询

    线段树的查询 对于一个有n个数的整数数组,在对应的线段树中, 根节点所代表的区间为0-n-1, 每个节点有一个额外的属性max,值为该节点所代表的数组区间start到end内的最大值. 为Segmen ...