1005 大数加法
给出2个大整数A,B,计算A+B的结果。
输入
第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 = 10000 ;
int cmp(char *a , char *b)
{
int len1 = strlen(a), len2 = strlen(b);
if(len1 > len2 || (len1 == len2 && strcmp(a , b) > 0))
return 1 ;
return -1 ;
}
void plu(char *a , char *b)
{
int len1 = strlen(a) , len2 = strlen(b) ;
int c[N] , d[N] , e[N];
memset(c , 0 , sizeof(c)) ;
memset(d , 0 , sizeof(d)) ;
int j = 0 ;
for(int i = len1 - 1 ; i >= 0 ; i--)
c[j++] = a[i] - '0';
j = 0 ;
for(int i = len2 - 1 ; i >= 0 ; i--)
d[j++] = b[i] - '0';
int len = max(len1 , len2);
int k = 0 ;
for(int i = 0 ; i < len ; i++)
{
e[i] = (c[i] + d[i] + k) % 10 ;
k = (c[i] + d[i] + k) / 10 ;
}
if(k)
{
len ++ ;
e[len - 1] = k ;
}
for(int i = len - 1 ; i >= 0 ; 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 , 0 , sizeof(c)) ;
memset(d , 0 , sizeof(d)) ;
int j = 0 ;
for(int i = len1 - 1 ; i >= 0 ; i--)
c[j++] = a[i] - '0';
j = 0 ;
for(int i = len2 - 1 ; i >= 0 ; i--)
d[j++] = b[i] - '0';
int len = max(len1 , len2);
for(int i = 0 ; i < len ; i ++)
{
e[i] = c[i] - d[i] ;
if(e[i] < 0)
{
c[i+1]--;
e[i] += 10 ;
}
}
for(int i = len - 1 ; i >= 0 ; i--)
{
if(e[i] == 0)
{
len -- ;
}
else{
break ;
}
}
if(len == 0)
cout << 0 << endl ;
else
{
for(int i = len - 1 ; i >= 0 ; 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 = 1 ; i < len1 ; i++)
c[i - 1] = a[i] ;
for(int i = 1 ; i < len2 ; i++)
d[i - 1] = b[i] ;
if(a[0] == '-' && b[0] == '-')
{
cout << '-' ;
plu(c , d) ;
}
else if(a[0] == '-')
{
if(cmp(c , b) > 0)
{
cout << '-' ;
jian(c , b) ;
}
else
{
jian(b , c) ;
}
}
else if(b[0] == '-')
{
if(cmp(d , a) > 0)
{
cout << '-';
jian(d , a);
}
else
{
jian(a , d);
}
}
else
{
plu(a , b);
}
}
return 0;
}
但是这个大数计算方法提交会超时(太low了)。。。
2、
用string函数的相关操作。
比第一份代码好在没有进行用int数组进行转换,节省了很多时间..
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int N = 10000 ;
int cmp(string a , string b)
{
int len1 = a.length(), len2 = b.length();
if(len1 > len2 || (len1 == len2 && a.compare(b) > 0))
return 1 ;
return -1 ;
}
void plu(string a , string b)
{
string c ;
int i , j , k = 0 , x , y ;
for(i = a.length() - 1 , j = b.length() - 1 ; i >= 0 && j >= 0 ; i-- , j--)//对两个数的公共部分进行加法操作.
{
x = a[i] - '0' ;
y = b[j] - '0' ;
c += (x + y + k) % 10 + '0' ;
k = (x + y + k) / 10 ;
}
while(i >= 0) 对更长的数进行进位处理..
{
x = a[i] - '0' ;
c += (x + k) % 10 + '0' ;
k = (x + k) / 10 ;
i-- ;
}
while(j >= 0)//同上...
{
y = b[j] - '0' ;
c += (y + k) % 10 + '0' ;
k = (y + k) / 10 ;
j-- ;
}
if(k)//判断最高位是否需要进位...
{
c += k + '0' ;
}
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() - 1 , j = b.length() - 1 ; i >= 0 && j >= 0; i-- , j--)//对各个部分进行减法操作...
{
x = a[i] - '0';
y = b[j] - '0';
if(x - y < 0)
{
a[i - 1]--; // 如果不够减就向上一位借位..
c += (x - y + 10) + '0' ;
}
else
{
c += (x - y) + '0';
}
}
while(i >= 0) // 对更长部分的数的部分进行借位操作...
{
x = a[i] - '0' ;
if(x < 0)
{
a[i - 1]--;
c += (x + 10) + '0';
}
else
{
c += x + '0';
}
i--;
}
while(j >= 0)
{
y = b[j] - '0' ;
if(y < 0)
{
b[j - 1]--;
c += (y + 10) + '0';
}
else
{
c += y + '0';
}
j-- ;
}
int l = c.length() - 1 ;//删除前置0 ...
while(c[l] == '0')
{
c.erase(c.end() - 1);
l = c.length() - 1;
}
if(c.length() == 0)//如果全为0 , 则输出0...
cout << 0 << endl ;
else
{
reverse(c.begin() , c.end());
cout << c << endl ;
}
}
int main()
{
string a , b ;
while(cin >> a >> b)
{
string c(a , 1) , d(b , 1); // 将a , b 字符的首位去除得到两个新的字符串c , d ..
if(a[0] == '-' && b[0] == '-')// 两个数为"-"
{
cout << '-' ;
plu(c , d) ;
}
else if(a[0] == '-') a 为负数 ,
{
if(cmp(c , b) > 0) //
{
cout << '-' ; // 且a 的绝对值大于 b
jian(c , b) ;// 将a去除负号的字符串c 减去 b ...
}
else
{
jian(b , c) ; // a 绝对值小于 b 用b 减去c ..
}
}
else if(b[0] == '-')
{
if(cmp(d , a) > 0)
{
cout << '-';
jian(d , a);
}
else
{
jian(a , d);
}
}
else
{
plu(a , b);
}
}
return 0;
}