给出2个大整数A,B,计算A+B的结果。

Input

第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数)

Output

输出A + B

Input示例

68932147586 468711654886

Output示例

537643802472

====================================================================================================

问题解法分析:

因为长度小于10000 所以可以依据模拟进位的方法进行解决

解决思路如下:

1:比较两个字符串类型:

如果为如果同为正数(负数) 则进行直接相加(负数相加负号)

若为一正一负,则对负数取消负号进行正数与负数的相减

保证此时字符串已经不存在正负号问题

2:对字符串进行转换

比较两个字符串的长度,若长短不一样则对短的字符串进行补齐 保证两个字符串长短一样

转换函数如下:

 void Trans(char *str_num1, char *str_num2, char *tempbuf1, char *tempbuf2)
{
int len_num1=;
int len_num2=;
int i=;
while(str_num1[i]!='\0')
{
len_num1++;
i++;
}
// printf("字符串1的长度: length1=%d\n",len_num1);
i=;
while(str_num2[i]!='\0')
{
len_num2++;
i++;
}
// printf("字符串2的长度: length2=%d\n\n",len_num2); tempbuf1[]='';
tempbuf2[]=''; //=======================================================================
if(len_num2>=len_num1) //补成相同长度
{
for(i=;i<=(len_num2-len_num1);i++)
{
tempbuf1[i]='';
}
for(i=len_num2-len_num1+;i<=len_num2;i++)
{
tempbuf1[i]=str_num1[i-(len_num2-len_num1+)];
}
for(i=;i<=len_num2;i++)
{
tempbuf2[i]=str_num2[i-];
}
}
//------------------------------------------
else if(len_num2<len_num1)
{
for(i=;i<=(len_num1-len_num2);i++)
{
tempbuf2[i]='';
}
for(i=len_num1-len_num2+;i<=len_num1;i++)
{
tempbuf2[i]=str_num2[i-(len_num1-len_num2+)];
}
for(i=;i<=len_num1;i++)
{
tempbuf1[i]=str_num1[i-];
}
} }

3:进行加减运算

对已经处理过的字符串进行加减计算

计算方式模仿竖式进位进行计算

加法算法:

     int i=;
int temp=;
int jinwei=;
int len=;
while(tempbuf1[i]!='\0')
{
len++;
i++;
}
for(i=len-;i>=;i--)
{
temp=(int)(tempbuf1[i]+tempbuf2[i]+jinwei-);
if(temp>=)
{
temp=temp-;
jinwei=;
}
else jinwei=;
result[i]=(char)(temp+);
}

减法算法:

     int i=;
int temp=;
int jiewei=;
int len=;
int ret=; while(tempbuf1[i]!='\0') // tempbuf1 和 tempbuf2 的长度相等
{
len++;
i++;
} ret = Compare(tempbuf1,tempbuf2);
if(ret==)
{
for(i=len-;i>=;i--)
{
temp = (int)tempbuf1[i] - (int)tempbuf2[i] - jiewei;
if (temp>=)
{
result[i]=(char)(temp+);
jiewei=;
}
else if (temp<)
{
result[i]=(char)(temp++);
jiewei=;
}
}
ThrowAway_0 (result);
}
else if(ret==)
{
memset(result,,);
result[]='';
}
else if(ret==-)
{
for(i=len-;i>=;i--)
{
temp = (int)tempbuf2[i] - (int)tempbuf1[i] - jiewei;
if (temp>=)
{
result[i]=(char)(temp+);
jiewei=;
}
else if (temp<)
{
result[i]=(char)(temp++);
jiewei=;
}
}
ThrowAway_0 (result);
memset(buf1,,);
sprintf(buf1,"-%s",result);
strcpy(result,buf1);
}

在对减法进行运算时,存在大数减小数的问题,因此需要对字符串长度进行比较:

比较函数如下:

 int Compare(char *tempbuf1,char *tempbuf2)
{
ThrowAway_0 (tempbuf1);
ThrowAway_0 (tempbuf2);
char buf1[]={};
char buf2[]={};
Trans(tempbuf1,tempbuf2,buf1,buf2);
memset(tempbuf1,,);
memset(tempbuf2,,);
strcpy(tempbuf1,buf1);
strcpy(tempbuf2,buf2); int ret=;
int count=;
while(count<)
{ int m=(int)tempbuf1[count]-;
int n=(int)tempbuf2[count]-;
if(m==n)
{
count++;
ret=;
}
else if(m>n)
{
// printf("tempbuf1>tempbuf2\n");
ret=;
break; }
else if(m<n)
{
// printf("tempbuf1<tempbuf2\n");
ret=-;
break;
}
}
return ret;
}

4:对多余项进行缩减:

将剩余0去掉

 void  ThrowAway_0 (char *tempbuf )  // 去除结果前面的 连续的无意义的 "0"
{
char buf[]={}; int n = strlen(tempbuf)-;
int i=;
while(i<n)
{
if (tempbuf[i]!='')
{
break;
}
else
{
i++;
}
}
int Throw = i;
for (i=;i<=n-Throw;i++)
{
buf[i]=tempbuf[i+Throw];
} strcpy(tempbuf,buf); }

5:对结果进行输出 完成。

测试代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h> void Add (char *tempbuf1, char *tempbuf2, char *result); // 大数相加 result = tempbuf1 + tempbuf2 void Sub(char *tempbuf1, char *tempbuf2, char *result); // 大数相减 result = tempbuf1 - tempbuf2 (默认前者大) void Trans (char *str_num1, char *str_num2, char *tempbuf1, char *tempbuf2); // 大数转化为等长,且最前面添加 “0” int Compare(char *tempbuf1,char *tempbuf2);//比较两个字符串的长度 相同返回0 buf1长返回1 buf2长返回-1 void ThrowAway_0 (char *tempbuf ); // 去除结果前面的 连续的无意义的 "0" int main()
{ char str1[100001];
char str2[100001];
char result[100001]={0};
short result_flag = 0; scanf("%s %s",str1,str2); if(str1[0] == '-' && str2[0] == '-')
{
str2[0] = str1[0] = '0';
Add(str1,str2,result);
if(result[0]!= '0')
{
printf("-");
printf("%s\n",result);
} else
printf("0\n");
}
else if(str1[0] == '-' && str2[0] != '-')
{
str1[0] ='0'; Sub(str2,str1,result);
printf("%s\n",result);
}
else if(str1[0] != '-' && str2[0] == '-')
{
str2[0] = '0';
Sub(str1,str2,result);
printf("%s\n",result);
}
else
{
Add(str1,str2,result);
printf("%s\n",result);
} return 0;
} void Trans(char *str_num1, char *str_num2, char *tempbuf1, char *tempbuf2)
{
int len_num1=0;
int len_num2=0;
int i=0;
while(str_num1[i]!='\0')
{
len_num1++;
i++;
}
i=0;
while(str_num2[i]!='\0')
{
len_num2++;
i++;
} tempbuf1[0]='0';
tempbuf2[0]='0'; //=======================================================================
if(len_num2>=len_num1) //补成相同长度
{
for(i=1;i<=(len_num2-len_num1);i++)
{
tempbuf1[i]='0';
}
for(i=len_num2-len_num1+1;i<=len_num2;i++)
{
tempbuf1[i]=str_num1[i-(len_num2-len_num1+1)];
}
for(i=1;i<=len_num2;i++)
{
tempbuf2[i]=str_num2[i-1];
}
}
//------------------------------------------
else if(len_num2<len_num1)
{
for(i=1;i<=(len_num1-len_num2);i++)
{
tempbuf2[i]='0';
}
for(i=len_num1-len_num2+1;i<=len_num1;i++)
{
tempbuf2[i]=str_num2[i-(len_num1-len_num2+1)];
}
for(i=1;i<=len_num1;i++)
{
tempbuf1[i]=str_num1[i-1];
}
} } void Add(char *tempbuf1, char *tempbuf2, char *result) // 大数相加 result = tempbuf1 + tempbuf2
{
char buf1[100001]={0};
char buf2[100001]={0};
Trans(tempbuf1,tempbuf2,buf1,buf2);
strcpy(tempbuf1,buf1);
strcpy(tempbuf2,buf2); int i=0;
int temp=0;
int jinwei=0;
int len=0;
while(tempbuf1[i]!='\0')
{
len++;
i++;
}
for(i=len-1;i>=0;i--)
{
temp=(int)(tempbuf1[i]+tempbuf2[i]+jinwei-96);
if(temp>=10)
{
temp=temp-10;
jinwei=1;
}
else jinwei=0;
result[i]=(char)(temp+48);
}
ThrowAway_0 (result); } //================================================ void ThrowAway_0 (char *tempbuf ) // 去除结果前面的 连续的无意义的 "0"
{
char buf[100000]={0}; int n = strlen(tempbuf)-1;
int i=0;
while(i<n)
{
if (tempbuf[i]!='0')
{
break;
}
else
{
i++;
}
}
int Throw = i;
for (i=0;i<=n-Throw;i++)
{
buf[i]=tempbuf[i+Throw];
} strcpy(tempbuf,buf); } //======================================================= //extern "C" __declspec(dllexport)
void Sub(char *tempbuf1, char *tempbuf2, char *result) // 大数相减 result = tempbuf1 - tempbuf2
{
ThrowAway_0 (tempbuf1);
ThrowAway_0 (tempbuf2);
memset(result,0,200);
char buf1[100001]={0};
char buf2[100001]={0};
Trans(tempbuf1,tempbuf2,buf1,buf2);
memset(tempbuf1,0,100001);
memset(tempbuf2,0,100001); strcpy(tempbuf1,buf1);
strcpy(tempbuf2,buf2); int i=0;
int temp=0;
int jiewei=0;
int len=0;
int ret=1; while(tempbuf1[i]!='\0') // tempbuf1 和 tempbuf2 的长度相等
{
len++;
i++;
} ret = Compare(tempbuf1,tempbuf2);
if(ret==1)
{
for(i=len-1;i>=0;i--)
{
temp = (int)tempbuf1[i] - (int)tempbuf2[i] - jiewei;
if (temp>=0)
{
result[i]=(char)(temp+48);
jiewei=0;
}
else if (temp<0)
{
result[i]=(char)(temp+10+48);
jiewei=1;
}
}
ThrowAway_0 (result);
}
else if(ret==0)
{
memset(result,0,100001);
result[0]='0';
}
else if(ret==-1)
{
for(i=len-1;i>=0;i--)
{
temp = (int)tempbuf2[i] - (int)tempbuf1[i] - jiewei;
if (temp>=0)
{
result[i]=(char)(temp+48);
jiewei=0;
}
else if (temp<0)
{
result[i]=(char)(temp+10+48);
jiewei=1;
}
}
ThrowAway_0 (result);
memset(buf1,0,100001);
sprintf(buf1,"-%s",result);
strcpy(result,buf1);
} } //====================================================================== //=================================================================================== int Compare(char *tempbuf1,char *tempbuf2)
{
ThrowAway_0 (tempbuf1);
ThrowAway_0 (tempbuf2);
char buf1[100001]={0};
char buf2[100001]={0};
Trans(tempbuf1,tempbuf2,buf1,buf2);
memset(tempbuf1,0,100001);
memset(tempbuf2,0,100001);
strcpy(tempbuf1,buf1);
strcpy(tempbuf2,buf2); int ret=1;
int count=0;
while(count<100001)
{ int m=(int)tempbuf1[count]-48;
int n=(int)tempbuf2[count]-48;
if(m==n)
{
count++;
ret=0;
}
else if(m>n)
{
// printf("tempbuf1>tempbuf2\n");
ret=1;
break; }
else if(m<n)
{
// printf("tempbuf1<tempbuf2\n");
ret=-1;
break;
}
}
return ret;
}

此外,对于大数运算不限于加减运算 更多运算方法可以参考大数模板:

c++大数模板

参考内容:

参考博客

【51Nod】1005 大数加法的更多相关文章

  1. 大数高精度加减乘除 51nod 1005 大数加法

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B ...

  2. 51nod 1005 大数加法

    #include<iostream> #include<string> using namespace std; #define MAXN 10001 },b[MAXN]={} ...

  3. 51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

  4. 51nod 1005 1027 1029 高精度

    Java大数用法参考:https://www.cnblogs.com/jin-nuo/p/5313205.html 1005 大数加法: import java.util.*; import java ...

  5. 51NOD 1005

    1005 大数加法  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果.   Input 第1行:大数A 第2行:大 ...

  6. 51NOD 大数加法以及python写法

    练练 大数加法一般为小学生式的"竖式计算"要特别注意的是借位与进位的问题(先给看c++写法,我怕先看了python写法,会看不下去c++写法)这题还有要注意的是 1.同符号的话,直 ...

  7. c#大数加法

    在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...

  8. 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)

    题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...

  9. 51nod 1027大数乘法

    题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...

随机推荐

  1. 1001 - Another A+B

    1001 - Another A+B Description Give you an integer a, you are to find two another integers which sum ...

  2. 如何有效的遍历django的QuerySet

    最近做了一个小的需求,在django模型中通过前台页面的表单的提交(post),后台对post的参数进行解析,通过models模型查询MySQL,将数据结构进行加工,返回到前台页面进行展示.由于对dj ...

  3. jsp连接MySQL数据库显示GIS地理数据乱码问题的解决(select AsText(the_geom))

    oh,fuck,经过我昨天下午到今天的努力,终于将这一问题成功解决了,哈哈哈 问题详细描述: 我通过jsp页面连接上MySQL数据库,取出存在表中的地理数据(类型是geometry,具体有POINT. ...

  4. String.Join的实现

    String.Join的实现 在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的 ...

  5. ODBC操作excel

    //ODBC连接Excel public static void main(String[] args) {  Connection conn = null;  Statement stm = nul ...

  6. MVC Bootstrap极速开发框架

    ASP.NET MVC Bootstrap极速开发框架 前言 每次新开发项目都要从头开始设计?有木有一个通用的快速开发框架?并且得是ASP.NET MVC  And Bootstrap?数据库不要手工 ...

  7. openGL的使用步骤

    使用openGL步骤:1.创建GLSurfaceView对象2.创建GLSurfaceView.renderer实现类.3.设置activity的contentView,以及设置view的render ...

  8. [转]Android与电脑局域网共享之:Samba Server

    大家都有这样的经历,通过我的电脑或网上邻居访问另一台计算机上的共享资源,虽然电脑和手机之间可以有多种数据传输方式,但通过Windows SMB方式进行共享估计使用的人并不是太多,下面我就简单介绍一下, ...

  9. malloc内存分配与free内存释放的原理

    malloc内存分配与free内存释放的原理 前段时间一直想看malloc的原理,在搜了好几篇malloc源码后遂放弃,晦涩难懂. 后来室友买了本深入理解计算机系统的书,原来上面有讲malloc的原理 ...

  10. PHP 5.5以后加速插件:Zend Opcache

    大家知道目前PHP的缓存插件一般有三个: APC. eAccelerator. XCache,但未来它们可能都会消失,因为PHP 5.5已经集成 Zend Opcache,功能和前三者相似但又有少许不 ...