[51NOD]大数加法(模拟)
题目链接: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]大数加法(模拟)的更多相关文章
- 51NOD 大数加法以及python写法
练练 大数加法一般为小学生式的"竖式计算"要特别注意的是借位与进位的问题(先给看c++写法,我怕先看了python写法,会看不下去c++写法)这题还有要注意的是 1.同符号的话,直 ...
- 51Nod大数加法(两个数正负都可)
很多大数的问题都运用模拟的思想,但是这个说一样也一样,但是难度较大,很麻烦,我自己谢写了100多行的代码,感觉很对,但就是WA.其实个人感觉C和C++没有大数类,是对人思想和算法的考验,但是有时候做不 ...
- POJ 2506 Tiling (递推 + 大数加法模拟 )
Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7965 Accepted: 3866 Descriptio ...
- 大数高精度加减乘除 51nod 1005 大数加法
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...
- 51nod 1005 大数加法
#include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...
- c#大数加法
在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...
- 【大数加法】POJ-1503、NYOJ-103
1503:Integer Inquiry 总时间限制: 1000ms 内存限制: 65536kB 描述 One of the first users of BIT's new supercompu ...
- 大数加法之C语言函数法(只有正数版)
由于某些原因,我于今天2017-4-19将我的博文搬到博客园了,以后我就在这里扎根了. 之前想过在博客写文章方便日后复习,但一直未能实现,所以,现在这篇是我个人人生中第一篇博 ...
- 51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 ...
随机推荐
- 推荐系统之LFM(二)
对于一个用户来说,他们可能有不同的兴趣.就以作者举的豆瓣书单的例子来说,用户A会关注数学,历史,计算机方面的书,用户B喜欢机器学习,编程语言,离散数学方面的书, 用户C喜欢大师Knuth, Jiawe ...
- poi实现Excel比较
http://stackoverflow.com/questions/866346/easiest-way-to-compare-two-excel-files-in-java http://stac ...
- NF3 里面的z cull reverse reload
nf3 siggraph2011的 分享 里面有谈对csm的优化. 1.mask white red 2. HI Z 这俩我都懂 3.reverse depth buffer这实在不明白, 为什么会有 ...
- jquery ajax/post/get 传参数给 mvc的action
jquery ajax/post/get 传参数给 mvc的action1.ActionResult Test1 2.View Test1.aspx3.ajax page4.MetaObjec ...
- BZOJ2961: 共点圆
好久没发了 CDQ分治,具体做法见XHR的论文… /************************************************************** Problem: 29 ...
- C++默认参数(转)
函数的默认参数值,即在定义参数的时候同时给它一个初始值.在调用函数的时候,我们可以省略含有默认值的参数.也就是说,如果用户指定了参数值,则使用用户指定的值,否则使用默认参数的值. void Func( ...
- Jquery---用jQuery实现的智能隐藏、滑动效果的返回顶部代码
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.2.min.js"></script& ...
- ASP 中调用函数关于Call使用注意的问题
Function TestFun(Tstr) TStr = "Fun2" End Function Sub TestSub(TStr) Tstr = "Sub2" ...
- CString,string,char*之间的转换(转)
这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...