大数相加(相减,相乘,相除)

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 308085    Accepted Submission(s): 59547


Problem 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.
 

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<algorithm>
#include<string.h>
int main()
{
    char a[1000],b[1000];
    int l,h,t,i,j,k=0,a1[1000],b1[1000],c1[1000];
    scanf("%d",&t);
    getchar();
    while(t--)
    {k++;
    memset(a1,0,sizeof(a1));
    memset(b1,0,sizeof(b1));
    memset(c1,0,sizeof(c1));
        scanf("%s%s",a,b);
        l=strlen(a);
        h=strlen(b);
        for( j=l-1,i=0;i<l;i++,j--)
        a1[j]=a[i]-'0';
        for(j=h-1,i=0;i<h;i++,j--)
        b1[j]=b[i]-'0';
        for(i=0;i<1000;i++)
    {    c1[i]=c1[i]+b1[i]+a1[i];
        if(c1[i]>9)
        {c1[i]-=10;
        c1[i+1]=1;}
    }
    printf("Case %d:\n%s + %s = ",k,a,b);
    for(i=999;i>=0;i--)
    {
        if(c1[i]>0)
        {
            for(j=i;j>=0;j--)
            printf("%d",c1[j]);
            break;
            
        }
    }
    printf("\n");
if(t!=0)
printf("\n");
        
                
    }
    
    
} 


解题分析:
由于整数较大,所以用字符保存,由于相加时是由后向前加,所以,先将字符串倒向储存,然后,每位依次相加,若大于10,对10取余,对结果反向输出。
此题扩展:
大数相减:
与大数相加步骤相似,注意,应是大数减小数,然后再判断符号:
#include <iostream>
#include <string.h>
using namespace std;
const int N=1005;

void Sub(char *p1,char *p2,int *p3)
{
    int i,j,k=0;
    
    int bit=0,t=0; //bit:表示借位,1:表示需要借位,0:表示不需要借位。t:存储中间计算的位相减值
    int len1=strlen(p1);  //被减数的长度
    int len2=strlen(p2); //减数的长度
    
    for(i=len1-1,j=len2-1;i>=0 && j>=0;--i,--j) //此为循环遍历两数,分别对位进行相减操作
    {
        t=(p1[i]-'0')-(p2[j]-'0')-bit;    //计算两个位之间的差值,同时要考虑借位
        
        if(t<0)  //如果t小于0,表示需要借位,bit赋值为1,且最终相减结果需加10作为调整并存储到p3数组中,自己用笔画一下就好理解.
        {
            bit=1;
          //例:123-456,t相减实际值分别为-3(3-6-0(bit)),-4(2-5-1(bit)),-4(1-4-1(bit)),加10调整后为:
           // 7,6,6,由于最高位相减后还bit为1即还需借位,  因此还需调整,转换为第一种情况,即用1000-667=333,且结果为负    
            p3[k++]=t+10;        
        }
        else
        {
            bit=0; //相减为正,则无需调整,直接将t赋给p3对应位。
            p3[k++]=t;
        }
        
    }
    while(i>=0)  //strlen(p1)>strlen(p2) ,result is greater than zero
    {
        t=p1[i]-'0'-bit;
        if(t<0)
        {
            bit=1;
            p3[k++]=t+10;
        }
        else
        {
            bit=0;
            p3[k++]=t;
        }
        i--;
    }
    while(j>=0) //strlen(p1)<strlen(p2),result is less than zero
    {
        t=10-bit-(p2[j]-'0');        
        p3[k++]=t;
        j--;
    }
    if(bit==1)  //对仍有进位的情况考虑,主要分两种:一种是strlen(p1)<strlen(p2),另一种是p1-p2<0,这两种情况bit为1
    {
        p3[0]=10-p3[0];
        for(i=1;i<k;++i)
        {
            p3[i]=10-p3[i]-bit;
        }        
    }
    if(bit==1)
        cout<<"-";
    for(i=k-1;i>=0;--i)
        cout<<p3[i];
    cout<<endl;
}
int main()
{
    char c1[N],c2[N];
    int a[N];
    int i,j;
    while(cin>>c1>>c2)
    {
        memset(a,0,sizeof(a));
        Sub(c1,c2,a);
    }
    return 0;
}

大数相乘:
核心代码:
#include <iostream> 
#include<stdio.h> 
#include<string.h>
using namespace std;  
void mul(char *ch1, char *ch2)  
{  
    int len1=strlen(ch1),len2=strlen(ch2);  
    char ch3[1000009];  
    int i,j,carry;  
    for (i=0; i<1000009; i++)  
        ch3[i]='\0';//=0  
    for (i=0; i<len1; i++)  
    {  
        for (j=0; j<len2; j++)//下面可能asc码大于128  
        {  
            ch3[i+j]=ch3[i+j]+(ch1[i]-'0')*(ch2[j]-'0');//这里用字符型表示ch3要注意  
            if (ch3[i+j]>9&&(i+j)>0)  
            {  
               ch3[i+j-1]+=ch3[i+j]/10;  
               ch3[i+j]=ch3[i+j]%10;  
            }  
        }  
    }  
    for (i=len1+len2-1; i>0; --i)//防止上面进位时大于9的情况   
    {  
        if (ch3[i]>9)  
        {  
            ch3[i-1]=ch3[i-1]+ch3[i]/10;  
            ch3[i]%=10;  
        }  
    }  
  
    if (ch3[0]>9)//if (ch[3]>99)  
    {  
        cout<<ch3[0]/10;  
        ch3[0]=ch3[0]%10;  
    }  
    for (i=0; i<len1+len2-1; i++)  
        cout<<char(ch3[i]+48);  
    cout<<endl;  
}  
int main()  
{  
    char ch1[10005],ch2[10005];  
    while (cin>>ch1>>ch2)  
    {  
          mul(ch1,ch2);  
    }  
    return 0;  
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值