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;
    }
  • 相关阅读:
    resin实现热部署配置
    tomcat实现域名访问步骤
    springboot学习笔记2---配置拦截器:
    springboot学习笔记2:搭建web项目
    springboot学习笔记1:springboot入门
    重识maven
    shiro学习笔记:remeberMe,多次登录锁死账号
    shiro学习笔记:授权管理
    springmvc定时任务及RequestBody注解
    springmvc处理异常
  • 原文地址:https://www.cnblogs.com/Cloud-king/p/8484942.html
Copyright © 2011-2022 走看看