zoukankan      html  css  js  c++  java
  • 杭电1002

    A + B Problem II

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


    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

    就是大数的加减
    //此题的要点在于 把数字当字符串储存 用数组储存
    //计算的时候注意要减去‘0’,因为那是我们的字符串
    //在我们换算 的时候一定要加上temp 我就是因为没加 错了一个下午
    #include <iostream> #include<string> using namespace std; int main() { int n; cin>>n; int ui=1; while(n--){ string a,b; int sum[1001];//用来放结果 cin>>a>>b; int lena,lenb; lena=a.length(); lenb=b.length(); // cout<<lena<<" "<<lenb<<endl; int ka=lena-1; int kb=lenb-1; int q=0;//用来表示数组下标 int temp=0;//用来表示进位 // cout<<a[2]+a[1]-'0'-'0'<<endl; while(lena!=0&&lenb!=0) { int numa=a[ka]-'0';//和 因为string是字符类 要减去0的ascii码 int numb=b[kb]-'0'; //cout<<numa<<" " <<numb<<endl; sum[q]=(numa+numb+temp)%10;//我们要的是余数位 if((numa+numb+temp)>=10){ //这里的每一次判断必须加上temp进位 temp=1; }else{ temp=0; } q++; ka--; kb--; lena--; lenb--; } //cout<<a[ka]<<endl; if(lena>lenb){ while(lena){ int numa=a[ka]-'0' ; sum[q]=(numa+temp)%10;//我们要的是余数位 temp=(numa+temp)/10; q++; ka--; // kb--; lena--; //lenb--; } } else if(lenb>lena){ while(lenb){ int numb=b[kb]-'0'; sum[q]=(numb+temp)%10; temp=(numb+temp)/10; q++; kb--; lenb--; } } else { if(temp==1){ sum[q]=1; q++; } } cout<<"Case "<<ui<<":"<<endl; ui++; int alll=a.length(); int blll=b.length(); for(int j=0;j<alll;j++) { cout<<a[j]; } cout<<" + "; for(int jk=0;jk<blll;jk++) { cout<<b[jk]; } cout<<" = "; for(int i=q-1;i>=0;i--){ cout<<sum[i]; } if(n==0){ cout<<endl; } else{ cout<<endl; cout<<endl; } } return 0; }

    然而,我花了一个下午。其实就是把temp给弄错了

    错误代码为:

    while(lena!=0&&lenb!=0)
         {
             int numa=a[ka]-'0';//和 因为string是字符类 要减去0的ascii码
             int numb=b[kb]-'0';
    
           //cout<<numa<<" " <<numb<<endl;
        sum[q]=(numa+numb+temp)%10;//我们要的是余数位
            if((numa+numb)>=10){  //就是这边错了!!!!!!
                temp=1;
            }else{
                temp=0;
            }
            q++;
            ka--;
            kb--;
            lena--;
            lenb--;
         }

    bug为:

  • 相关阅读:
    背景100%显示
    你还在用Html.PartialAsync? 教你更简单一招搞定
    记一次TokuMX数据库集群恢复
    使用SolrNet访问Solr-5.5.0
    从注册表中删除打印机列表中的打印机
    DEV之GridControl常用设置
    在Devexpress中设置按钮列(无需写代码)
    Sql中获取表结构(字段名称,类型,长度,说明)
    DevExpress之ChartControl基本用法
    SQL Server类型与C#类型对应关系[转]
  • 原文地址:https://www.cnblogs.com/William-xh/p/6719668.html
Copyright © 2011-2022 走看看