zoukankan      html  css  js  c++  java
  • 大数问题(相加) A + B

    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

     AC代码:

    #include<iostream>
    #include<string.h>
    using namespace std;
    #include<stdio.h>
    #define MAX 1010
    int add1[MAX], add2[MAX], res[MAX];
    char tmp1[MAX], tmp2[MAX];
    int main()
    {
        int N, i, j, len, len1, len2, tmp, k;
            scanf("%d",&N);
            getchar();
            for(j=0;j<N;j++)
            {
                memset(add1,0,sizeof(add1));
                memset(add2,0,sizeof(add2));
                memset(res,0,sizeof(res));
                memset(tmp1,0,sizeof(tmp1));
                memset(tmp2,0,sizeof(tmp2));
                scanf("%s %s",tmp1,tmp2);
                len1 = strlen(tmp1);
                len2 = strlen(tmp2);
                for(i=len1-1,k=0;i>=0;i--)
                    add1[k++] = tmp1[i] - '0';
    
                for(i=len2-1,k=0;i>=0;i--)
                    add2[k++] = tmp2[i] - '0';
                tmp = 0;
                if(len1 >= len2)
                {
                    for(i=0;i<=len1;i++)
                    {
                        res[i] = (add1[i] + add2[i] +tmp)%10;
                        tmp = (add1[i] + add2[i] +tmp)/10;
                    }
                }
                else if(len1 < len2)
                {
                    for(i=0;i<=len2;i++)
                    {
                        res[i] = (add1[i] + add2[i] +tmp)%10;
                        tmp = (add1[i] + add2[i] +tmp)/10;
                    }
                }
                if(len1 >= len2) len = len1;
                else len = len2;
                printf("Case %d:
    %s + %s = ",j+1, tmp1 , tmp2);
                if(res[len]!=0) printf("%d",res[len]);
                for(i=len-1;i>=0;i--)
                        printf("%d",res[i]);
    
                printf("
    ");
                if(j!=N-1) printf("
    ");
            }
        return 0;
    }

     my AC:

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #define N 1010
    using namespace std;
    char a[N],b[N];
    int c[N],d[N],ans[N];
    
    int main(){
        int t ;
        cin>>t;
        getchar();
        for(int i=1;i<=t;i++)
        {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            memset(c,0,sizeof(c));
            memset(d,0,sizeof(d));
            memset(ans,0,sizeof(ans));
            cin>>a>>b;
            int la=strlen(a);
            int lb=strlen(b);
            for(int i=0;i<la;i++) c[i]=a[la-1-i]-'0';
            for(int i=0;i<lb;i++) d[i]=b[lb-1-i]-'0';
            int lans;
            (la>lb)?lans=la:lans=lb;
            int temp=0;
            for(int i=0;i<=lans;i++){
                ans[i]=(c[i]+d[i]+temp)%10;
                temp=(c[i]+d[i]+temp)/10;
            }
            cout<<"Case "<<i<<":"<<endl;
            cout<<a<<" + "<<b<<" = ";
            for(int i=lans-1;i>=0;i--)
            cout<<ans[i];
            cout<<endl;
            if(i!=t)cout<<endl;
        }
    
    }
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #define N 1010
    using namespace std;
    char a[N],b[N];
    int c[N],d[N],ans[N];
    
    int main(){
        int t ;
        cin>>t;
        getchar();
        for(int i=1;i<=t;i++)
        {
            fill(a,a+N,0);
            fill(b,b+N,0);
            fill(c,c+N,0);
            fill(d,d+N,0);
            fill(ans,ans+N,0);
            cin>>a>>b;
            int la=strlen(a);
            int lb=strlen(b);
            for(int i=0;i<la;i++) c[i]=a[la-1-i]-'0';
            for(int i=0;i<lb;i++) d[i]=b[lb-1-i]-'0';
            int lans;
            (la>lb)?lans=la:lans=lb;
            int temp=0;
            for(int i=0;i<=lans;i++){
                ans[i]=(c[i]+d[i]+temp)%10;
                temp=(c[i]+d[i]+temp)/10;
            }
            cout<<"Case "<<i<<":"<<endl;
            cout<<a<<" + "<<b<<" = ";
            for(int i=lans-1;i>=0;i--)
            cout<<ans[i];
            cout<<endl;
            if(i!=t)cout<<endl;
        }
    
    }
    #include<iostream>
    #include<string.h>
    using namespace std;
    #include<stdio.h>
    #define MAX 1010
    int add1[MAX], add2[MAX], res[MAX];
    char tmp1[MAX], tmp2[MAX];
    int main()
    {
    int N, i, j, len, len1, len2, tmp, k;
    scanf("%d",&N);
    getchar();
    for(j=0;j<N;j++)
    {
    memset(add1,0,sizeof(add1)) ;    ||用0填充
    memset(add2,0,sizeof(add2));
    memset(res,0,sizeof(res));
    memset(tmp1,0,sizeof(tmp1));
    memset(tmp2,0,sizeof(tmp2));
    
    tips:函数解释
    
    void *memset(void *s, int ch, size_t n);
    
    函数解释:将s中前n个字节替换为ch并返回s;
    
    cin>>tmp1>>tmp2;
    
    量 数 组 长 度:
    len1 = strlen(tmp1);
    len2 = strlen(tmp2);
    for(i=len1-1,k=0;i>=0;i--)||改类型并换顺序
    add1[k++] = tmp1[i] - '0';
    
    for(i=len2-1,k=0;i>=0;i--)
    add2[k++] = tmp2[i] - '0';
    tmp = 0;
    if(len1 >= len2)
    {
    for(i=0;i<=len1;i++)
    {
    res[i] = (add1[i] + add2[i] +tmp)%10;
    tmp = (add1[i] + add2[i] +tmp)/10;
    }
    }
    else if(len1 < len2)
    {
    for(i=0;i<=len2;i++)
    {
    
    进位处理方法:
    res[i] = (add1[i] + add2[i] +tmp)%10;
    tmp = (add1[i] + add2[i] +tmp)/10;
    }
    }
    if(len1 >= len2) len = len1;
    else len = len2;
    printf("Case %d:
    %s + %s = ",j+1, tmp1 , tmp2);
    if(res[len]!=0) printf("%d",res[len]);
    for(i=len-1;i>=0;i--)
    printf("%d",res[i]);
    
    printf("
    ");
    if(j!=N-1) printf("
    ");
    }
    return 0;
    }

    char tmp1[MAX], tmp2[MAX];

    cin>>tmp1>>tmp2;

    一长串的字符数组,还可以直接用cin进行输入

    2. getchar()必须加

    3.字符型与整数类型互换

    for(i=len1-1,k=0;i>=0;i--)
    add1[k++] = tmp1[i] - '0';

    for(i=len2-1,k=0;i>=0;i--)
    add2[k++] = tmp2[i] - '0';

    其实呢-48也是一样的;

     

    4.错误代码

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    int main()
    {
    int t;
    long sa,sb;
    cin>>t;
    getchar();
    for(int i=1;i<=t;i++)
    {
    char a[5000],b[5000];
    for(int j=0;;j++)
    {
    char word=getchar();
    if(word>='0'&&word<='9')
    a[j]=word;
    else
    {
    sa=j;
    break;
    }
    }
    for(int k=0;;k++)
    {
    char word=getchar();
    if(word>='0'&&word<='9')
    b[k]=word;
    else if(word=='
    ')
    {
    sb=k;
    break;
    }
    }
    cout<<"Case "<<i<<":"<<endl;
    for(int m=0;m<sa;m++)
    {
    cout<<a[m];
    }
    cout<<" + ";
    for(int n=0;n<sb;n++)
    {
    cout<<b[n];
    }
    cout<<" = ";
    
    int c[5000]={};编译有警告
    int sc,next,shu,shua,shub;
    if(sa>sb)
    sc=sa;
    else
    sc=sb;       出错数据举例,999 999,所给出结果数组的长度设置有问题,应该分情况讨论,有0单独分情况,并设置不显示
    for(int o=sc-1;o>=0;o--,sa--,sb--)
    {
    if(sa>0)
    shua=a[sa-1]-48;
    else
    shua=if(sb>0)
    
    shub=b[sb-1]-48;
    else
    shub=0;
    shu=shua+shub;
    if(shu>=10)
    {
    c[o-1]=1;
    shu=shu-10;
    c[o]+=shu;       进位不全面,1 99999999999,数据出错,所以不适宜顺序相加机制,应该采用逆序相加,顺序输出的方式
    }
    else
    }
    
    for(int p=0;p<sc;p++)
    cout<<c[p];
    cout<<endl;
    if(i!=t)
    cout<<endl;
    }
    #include<iostream>
    
     
  • 相关阅读:
    React Native商城项目实战08
    React Native商城项目实战07
    React Native商城项目实战05
    React Native商城项目实战06
    React Native商城项目实战04
    React Native商城项目实战03
    React Native商城项目实战02
    单选框input:radio
    myDate97用法
    STRUTS2配置动态页面
  • 原文地址:https://www.cnblogs.com/carry-2017/p/7194877.html
Copyright © 2011-2022 走看看