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;
    }
  • 相关阅读:
    再谈Asp.Net页面生命周期
    多线程、方便扩展的Windows服务程序框架
    用NuGet.Server管好自家的包包
    github for Windows
    MongoVUE 15天试用期解决办法
    NET插件系统——提升系统搜索插件和启动速度的思考
    Visual Studio Ultimate 2012 RC 安装手记
    自动完成菜单
    HBase
    WCF消息可靠性于有序传递
  • 原文地址:https://www.cnblogs.com/Cloud-king/p/8484942.html
Copyright © 2011-2022 走看看