大数相加

本文介绍了一种使用字符串模拟大数加法的方法,解决了输入数据超出32位整数限制的问题,并提供了完整的C语言实现代码。


基础练习(一)
12:00:00
               
D - A + B Problem II
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

I have a VERY SIMPLE problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
正整数高精度1000位加法

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

	
		2
1 2
112233445566778899 998877665544332211 

Sample Output

	
		Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110 

题意:求大数之和

思路:用字符串保存数据,模拟加法,从第位向高位循环操作。

失误:主要表现为思路不太清晰”,当遇到问题时无法解决又要从头考虑,浪费了时间

            应大致确定思路,再从小处出发,模式化考虑,将问题分解为多个函数。

            考虑好函数的接口。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>


int max(int la,int lb) //c的数学函数库中无此函数和min() 
{
if(la>lb)
return la;
return lb;
}


int main()
{
int t,i,j,k=0,c[1111],num1[1111],num2[1111],la,lb,l,cnt;
char a[1111],b[1111];
scanf("%d",&t); 
   
while(t--)
{
     getchar();  //gets()函数可吸收一行字符包括\n  因此在gets之后如果再有字符型的输入函数则不用加getchar() 

     scanf("%s%s",a,b);//   但是scanf就要加了 否则就会将回车当成下一个输入 
  
 la=strlen(a);
     lb=strlen(b);
 memset(num1,0,sizeof(num1));//数组清0后可以统一操作 如两个长短不相同的与长短相同程序统一 不用在分类讨论 
     memset(num2,0,sizeof(num2)) ;
     memset(c,0,sizeof(c));
     for(i=0;a[i]!='\0';++i)    //将数组倒序排列方便操作  序号对不好时举个例子 如i=0 
     {
       num1[i]=a[la-1-i]-'0';
 }
 for(i=0;b[i]!='\0';++i)     
 {
  num2[i]=b[lb-1-i]-'0';
 }
    
     l=max(la,lb);
     cnt=0;
     for(i=0;i<=l ;++i)    //主干程序 模拟大数相加 将其分解为一位数字的相加与进位两步 主要利用循环思想 就是找规律 
     {
         c[i]=num1[i]+num2[i]+cnt;
         if(c[i]>9)
         {
         c[i]-=10;
         cnt=1;
 }
 else
 {
  cnt=0;
 }
 }
 while(c[i]==0)    
 {
  --i;
  }
  
  ++k;
  printf("Case %d:\n",k);
  printf("%s + %s = ",a,b);
  for(j=i;j>=0;--j)
  {
  printf("%d",c[j]); 
  }
  if(t>0)
  printf("\n");   //格式应当注意想好之后再提交  不能应为小问题失掉比赛 
  printf("\n");
}
return 0;
 } 

基础练习(一)
12:00:00
               
D - A + B Problem II
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

I have a VERY SIMPLE problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
正整数高精度1000位加法

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

	
		2
1 2
112233445566778899 998877665544332211 

Sample Output

	
		Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值