zoukankan      html  css  js  c++  java
  • HDU1002 : A + B Problem II

    A + B Problem II

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

    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<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int m3[100002];
    int f = 1;
    
    void Reverse(char *word,int len)   // 反转数字 
    {                          
        char temp;
        int i, j;
        for (j = 0, i = len - 1; j < i; --i, ++j) {
            temp = word[i];
            word[i] = word[j];
            word[j] = temp;
        }
    }
    
    int check(int a[],int num)      //归整
    {  int k=0,len=num;
        while(a[len-1]==0&&len>1) len--;    //去掉前导0
        for(k=0; k<len; k++)
            if(a[k]>=10)
            {
              a[k+1]=a[k+1]+ a[k]/10;  a[k]=a[k] % 10;
            }
        if (a[k]!=0) len=k+1;  //确定数组最终长度
        return len;
    }
    
    int addition(int m3[], char m1[], int num1, char m2[], int num2)
    {
        int i,len1,len2,len;
        len1=num1;
        len2=num2;
        len=(len1>=len2)?len1:len2;     //定位数
    
        for(i=0; i<=len; i++) m3[i]=0;   //初始化
        for (i=len1; i<len + 1; i++) m1[i]='0'; //缺位前导补0
        m1[i] = '';
        for (i=len2; i<len + 1; i++)    m2[i]='0'; 
        m2[i] = '';
        Reverse(m1,len1);
        Reverse(m2,len2);
        for (i=0; i<=len; i++)
            m3[i]=(int)(m1[i]-'0'+m2[i]-'0');   //加法
        len=check(m3,len);              
        return len;
    }
    
    int Fun(char a[]){   //确定位数
        int i = 0;
        for(i = 0; a[i] != ''; i++);
        return i;  //精确位数
    }
    
    int main(){
        char num1[100001],num2[100001];
        int m,len1,len2,len;
        cin >> m;
        while(m--){
            cin >> num1 >> num2;
            len1 = Fun(num1);
            len2 = Fun(num2);
            if(f != 1){
                cout << endl;
            }
            cout << "Case " << f << ":" << endl;
            cout << num1 << " + " << num2 << " = ";
            len = addition(m3,num1,len1,num2,len2);
            for(int i = len -1; i >= 0; i--){
                cout << m3[i];
            }
            cout << endl ;
            f++;
        }
        return 0;
    }
  • 相关阅读:
    Node.js 常用工具 util
    jQuery 选择器
    Node.js 创建HTTP服务器
    Node.js GET/POST请求
    JavaScript 用法
    Node.js 事件
    Node.js 函数
    Bootstrap<基础二> 网格系统
    读文章《Flexbox详解》笔记
    好文要读
  • 原文地址:https://www.cnblogs.com/wlglucky/p/12458766.html
Copyright © 2011-2022 走看看