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;
    }
  • 相关阅读:
    【原创】xenomai内核解析---内核对象注册表—xnregistry(重要组件)
    【原创】xenomai内核解析--双核系统调用(三)--如何为xenomai添加一个系统调用
    【原创】xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
    【原创】xenomai内核解析--信号signal(二)---xenomai信号处理机制
    环境篇:数据同步工具DataX
    环境篇:DolphinScheduler-1.3.1安装部署及使用技巧
    大数据篇:一文读懂@数据仓库(PPT文字版)
    如何把百万级别的订单根据金额排序
    数据库系统设计概述
    《Redis设计与实现》笔记4—独立功能的实现
  • 原文地址:https://www.cnblogs.com/Cloud-king/p/8484942.html
Copyright © 2011-2022 走看看