• 高精度------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;
    }
  • 相关阅读:
    怎样的配色能让你的作品高端!上档次呢?
    响应式布局的9项基本原则
    SOHO设计师如何保障自己的权益
    加载动态设计--等待的艺术创作思维
    初学者所必备的设计类网站
    网页设计师该向印刷设计学习的3个规则
    [并发编程] -- 内存模型(针对JSR-133内存模型)篇
    [并发编程] -- 线程池篇
    [并发编程] -- 容器和框架篇
    [并发编程] -- 工具类篇
  • 原文地址:https://www.cnblogs.com/Cloud-king/p/8484942.html
Copyright © 2020-2023  润新知