zoukankan      html  css  js  c++  java
  • 高精度------C++

    高精度运算------C++

    (加减乘除)

     

     例:ZOJ2001  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1001

    The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

    Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

    ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).


    Input

    The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.


    Output

    For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.


    Sample Input

    3
    24 1
    4358 754
    305 794


    Sample Output

    34
    1998
    1

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    const int maxn=1005;
    using namespace std;
    
    int main()
    {
        int n;
        //freopen("Atext.in","r",stdin);
        cin >> n;
        while(n--){
            string a,b;
            int flag=0,n,m,len,i;
            int num1[maxn]={0},num2[maxn]={0},ans[maxn]={0};
            cin >> a >> b;
            n=a.size();
            for(int i=0;i<n;i++){
                num1[i]=a[i]-'0';
            }
            m=b.size();
            for(int i=0;i<m;i++){
                num2[i]=b[i]-'0';
            }
            len=n>m? n:m;
            for(i=0;i<len;i++){
                ans[i]+=num1[i]+num2[i];     //反向数之和
                if(ans[i]>9){
                    ans[i]-=10;
                    ans[i+1]++;
                }
            }
            /*for(int i=0;i<len/2;i++){
                swap(ans[i],ans[len-i-1]);  //将和逆序
            }*/
            if(ans[len]!=0)len++;
            for(i=0;i<len;i++){
                if(ans[i]!=0||flag==1){
                cout << ans[i];
                flag=1;
                }
            }
            cout << endl;
        }
        return 0;
    }
  • 相关阅读:
    sniffer 和 debug flow
    如何认识TOS----DSCP 对照表
    NAT alg 和 ASPF
    使用refind引导多系统
    Backdooring a OS VM
    exit(0)与exit(1),return三者区别(详解)
    Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)
    2017年中国大学生程序设计竞赛-中南地区赛暨第八届湘潭市大学生计算机程序设计大赛题解&源码(A.高斯消元,D,模拟,E,前缀和,F,LCS,H,Prim算法,I,胡搞,J,树状数组)
    高斯消元法(Gauss Elimination)【超详解&模板】
    2015 计蒜之道 初赛(4)爱奇艺的自制节目(枚举 贪心)
  • 原文地址:https://www.cnblogs.com/Cloud-king/p/8484942.html
Copyright © 2011-2022 走看看