大数加减(51nod)
1005 大数加法
输入
第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)
输出
输出A + B
输入样例
68932147586
468711654886
输出样例
537643802472
/*
1、首先char数组输入。加法和减法的方法都是将char数组转为倒叙int型数组,再用一个新的int新数组的记录结果。 注意!!!两个新的int数组需要初始化为0 。
同号:可以认为是大数加法 , 需要注意的是进位。
异号:的话需要先比较两个字符数组的去掉“-”后的两个数的绝对值大小(函数strcmp())已确定是否需要“-”再作减法 , 注意去前置0。
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = ; int cmp(char *a , char *b)
{
int len1 = strlen(a), len2 = strlen(b);
if(len1 > len2 || (len1 == len2 && strcmp(a , b) > ))
return ;
return - ;
} void plu(char *a , char *b)
{
int len1 = strlen(a) , len2 = strlen(b) ;
int c[N] , d[N] , e[N];
memset(c , , sizeof(c)) ;
memset(d , , sizeof(d)) ;
int j = ;
for(int i = len1 - ; i >= ; i--)
c[j++] = a[i] - '';
j = ;
for(int i = len2 - ; i >= ; i--)
d[j++] = b[i] - '';
int len = max(len1 , len2);
int k = ;
for(int i = ; i < len ; i++)
{
e[i] = (c[i] + d[i] + k) % ;
k = (c[i] + d[i] + k) / ;
}
if(k)
{
len ++ ;
e[len - ] = k ;
}
for(int i = len - ; i >= ; i--)
{
cout << e[i] ;
}
cout << endl ;
}
void jian(char *a , char *b)
{
int len1 = strlen(a) , len2 = strlen(b) ;
int c[N] , d[N] , e[N];
memset(c , , sizeof(c)) ;
memset(d , , sizeof(d)) ;
int j = ;
for(int i = len1 - ; i >= ; i--)
c[j++] = a[i] - '';
j = ;
for(int i = len2 - ; i >= ; i--)
d[j++] = b[i] - '';
int len = max(len1 , len2);
for(int i = ; i < len ; i ++)
{
e[i] = c[i] - d[i] ;
if(e[i] < )
{
c[i+]--;
e[i] += ;
}
} for(int i = len - ; i >= ; i--)
{
if(e[i] == )
{
len -- ;
}
else{
break ;
}
}
if(len == )
cout << << endl ;
else
{
for(int i = len - ; i >= ; i --)
cout << e[i] ;
cout << endl ;
} } int main()
{
char a[N] , b[N] ;
while(~scanf("%s%s" , &a , &b))
{
char c[N] , d[N] ;
int len1 = strlen(a) , len2 = strlen(b);
for(int i = ; i < len1 ; i++)
c[i - ] = a[i] ;
for(int i = ; i < len2 ; i++)
d[i - ] = b[i] ;
if(a[] == '-' && b[] == '-')
{
cout << '-' ;
plu(c , d) ;
}
else if(a[] == '-')
{
if(cmp(c , b) > )
{
cout << '-' ;
jian(c , b) ;
}
else
{
jian(b , c) ;
}
}
else if(b[] == '-')
{
if(cmp(d , a) > )
{
cout << '-';
jian(d , a);
}
else
{
jian(a , d);
} }
else
{
plu(a , b);
} }
return ;
}
但是这个大数计算方法提交会超时(太low了)。。。
2、
用string函数的相关操作。
比第一份代码好在没有进行用int数组进行转换,节省了很多时间..
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int N = ; int cmp(string a , string b)
{
int len1 = a.length(), len2 = b.length();
if(len1 > len2 || (len1 == len2 && a.compare(b) > ))
return ;
return - ;
} void plu(string a , string b)
{
string c ;
int i , j , k = , x , y ;
for(i = a.length() - , j = b.length() - ; i >= && j >= ; i-- , j--)//对两个数的公共部分进行加法操作.
{
x = a[i] - '' ;
y = b[j] - '' ;
c += (x + y + k) % + '' ;
k = (x + y + k) / ;
}
while(i >= ) 对更长的数进行进位处理..
{
x = a[i] - '' ;
c += (x + k) % + '' ;
k = (x + k) / ;
i-- ;
}
while(j >= )//同上...
{
y = b[j] - '' ;
c += (y + k) % + '' ;
k = (y + k) / ;
j-- ;
}
if(k)//判断最高位是否需要进位...
{
c += k + '' ;
}
reverse(c.begin() , c.end()); // 将字符串反转...
cout << c << endl ; } void jian(string a , string b)
{
string c ;
int i , j , x , y ;
for(i = a.length() - , j = b.length() - ; i >= && j >= ; i-- , j--)//对各个部分进行减法操作...
{
x = a[i] - '';
y = b[j] - '';
if(x - y < )
{
a[i - ]--; // 如果不够减就向上一位借位..
c += (x - y + ) + '' ;
}
else
{
c += (x - y) + '';
}
}
while(i >= ) // 对更长部分的数的部分进行借位操作...
{
x = a[i] - '' ;
if(x < )
{
a[i - ]--;
c += (x + ) + '';
}
else
{
c += x + '';
}
i--;
}
while(j >= )
{
y = b[j] - '' ;
if(y < )
{
b[j - ]--;
c += (y + ) + '';
}
else
{
c += y + '';
}
j-- ;
}
int l = c.length() - ;//删除前置0 ...
while(c[l] == '')
{
c.erase(c.end() - );
l = c.length() - ;
}
if(c.length() == )//如果全为0 , 则输出0...
cout << << endl ;
else
{
reverse(c.begin() , c.end());
cout << c << endl ;
}
} int main()
{
string a , b ;
while(cin >> a >> b)
{
string c(a , ) , d(b , ); // 将a , b 字符的首位去除得到两个新的字符串c , d .. if(a[] == '-' && b[] == '-')// 两个数为"-"
{
cout << '-' ;
plu(c , d) ;
}
else if(a[] == '-') a 为负数 ,
{
if(cmp(c , b) > ) //
{
cout << '-' ; // 且a 的绝对值大于 b
jian(c , b) ;// 将a去除负号的字符串c 减去 b ...
}
else
{
jian(b , c) ; // a 绝对值小于 b 用b 减去c ..
}
}
else if(b[] == '-')
{
if(cmp(d , a) > )
{
cout << '-';
jian(d , a);
}
else
{
jian(a , d);
}
}
else
{
plu(a , b);
}
}
return ;
}
大数加减(51nod)的更多相关文章
- SPOJ VLATTICE - Visible Lattice Points 【“小”大数加减】
题目链接 一道比较简单的莫比乌斯反演,不过ans会爆long long,我是用结构体来存结果的,结构体中两个LL型变量分别存大于1e17和小于1e17的部分 #include<bits/stdc ...
- JavaScript 加减危机——为什么会出现这样的结果?
在日常工作计算中,我们如履薄冰,但是 JavaScript 总能给我们这样那样的 surprise~ 0.1 + 0.2 = ? 1 - 0.9 = ? 如果小伙伴给出内心的结果: 0.1 + 0.2 ...
- Android带加减的edittext
看了网上这样自带加减的edittext写得好复杂,还有各种监听事件,我觉得没有必有.于是我自己写了一个. 我这个edittext仅仅限制整数,每次加减1. public class TestEditT ...
- js实现输入框数量加减【转】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 自己动手丰衣足食之 jQuery 数量加减插件
引言 做一个手机端的订单相关项目中,其中下订单时需要用到数量加减的控件,可以设置默认值,也可以设置最大值和最小值.使用jQuery这么长时间了,平时很少去编写属于自己的插件,现在编写的时候对立面的一些 ...
- php 时间加减
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
- freemarker 数据做加减计算
controller的部分: @Controller@RequestMapping("/ContactsFrameIndex")public class ContactsFrame ...
- Oracle中的日期加减
加法 select sysdate,add_months(sysdate,12) from dual; --加1年 select sysdate,add_months(sysdate ...
- php如何在某个时间上加一天?一小时? 时间加减
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
随机推荐
- ZROI 19.07.28 组合计数/lb
T1 题意:\(n\)个变量,\(0 \leq x_i \leq c_i\),求\(\sum x_i = A\)方案数.\(n \leq 32\). Sol: \(n \leq 10\)的时候容斥很水 ...
- 如何用DNS+GeoIP+Nginx+Varnish做世界级的CDN
如何用DNS+GeoIP+Nginx+Varnish做世界级的CDN 如何用BIND, GeoIP, Nginx, Varnish来创建你自己的高效的CDN网络?CDN,意思是Content ...
- VS2015开发常用快捷键
以下内容均Ctrl+后面的按钮 M-O\P折叠 K-F 格式化 K-U\C注释 K-S侧外代码-(区域代码) 代码片段 ctor 自动生成默认的构造函数 prop 自动生成get set方法 cw 自 ...
- MySQL数据库4Python操作mysql、索引、慢查询日志
目录 一.Python 操作 mysql 1.1python 操作 mysql 1.2查询数据 1.3增加(添加.更新)数据 1.4修改数据 1.5删除数据 1.6SQL注入问题 1.6.1问题的引入 ...
- 【leetcode】390. Elimination Game
题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] - ...
- Sql server时间转时间long
DATEDIFF( S, '1970-01-01 00:00:00', a.endor_date ) - 8 * 60*60 ) actionTime, SELECT DATEADD(S,116070 ...
- window 连接服务器工具
Xshell xftp 下载网址 以上两个软件均免费, 只需要邮件激活即可. 其中 xshell 主要用来连接服务器,方便使用命令行.xftp 方便传输文件.
- 笨办法学Python(learn python the hard way)--练习36-37
练习37 1.Keywords(关键字) anddel fromnotwhileaselifglobal orwithassert elseifpass yield break except impo ...
- 微信小程序 form 组件
表单组件:将组件内用户输入的 <switch> <input> <checkbox> <slider> <radio> <picker ...
- 认识了一个新的手机游戏剖析工具- SnapDragon Profiler
原来这个是高通的工具,具说UNITY官方推荐了这个工具.大概看了下,可以从宏观上实时剖析手机应用的方方面面