• POJ 1504 Adding Reversed Numbers


    题目链接:

    http://poj.org/problem?id=1504

    Description

    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

    Hint:
    题意:
    给两个数,求两个数的反向相加的和,并且这个和也要反向输出。
    题解:
    很明显的大数相加的题目,但是这道题目的数据不强,直接用long long也可以强行过。
    代码:
    1.大数代码:
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn = 1000+10;
    #define met(a,b) memset(a,b,sizeof(a))
    char s1[maxn],s2[maxn];
    int num1[maxn],num2[maxn];
    int num[maxn];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%s%s",s1,s2);
            int l1=strlen(s1);
            int l2=strlen(s2);
            met(num1,0); met(num2,0); met(num,0);
            for(int i=l1-1,j=0;i>=0;i--,j++)
                num1[j]=s1[i]-'0';
            for(int i=l2-1,j=0;i>=0;i--,j++)
                num2[j]=s2[i]-'0';
            int k=0;
            if(l1>=l2)
            {
                int l3=l2-1;
                for(int i=l1-1;i>=0;i--)
                {
                    if(l3>=0)
                        num[k]+=num1[i]+num2[l3];
                    else
                        num[k]+=num1[i];
                    l3--;
                    if(num[k]>9)
                    {
                        num[k]-=10;
                        num[k+1]++;
                    }
                    k++;
                }
            }
            else
            {
                int l3=l1-1;
                for(int i=l2-1;i>=0;i--)
                {
                    if(l3>=0)
                        num[k]+=num2[i]+num1[l3];
                    else
                        num[k]+=num2[i];
                    l3--;
                    if(num[k]>9)
                    {
                        num[k]-=10;
                        num[k+1]++;
                    }
                    k++;
                }
            }
            if(num[k]>0)
                k++;
            int flag =0;
            for(int i=0;i<k;i++)
            {
                if(num[i]>0)
                    flag=1;
                if(flag)
                    printf("%d",num[i]);
            }
            printf("
    ");
        }
    }
    

     2.暴力过:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    long long get_reverse(long long int num)
    {
        long long result = 0;
        while(num>0)
        {
            result = result*10 + num%10;
            num = num/10;
        }
        return result;
    }
    
    int main()
    {
        int t;
        
        scanf("%d",&t);
        while(t--)
        {
            long long int num1,num2;
            scanf("%lld%lld",&num1,&num2);
            num1 = get_reverse(num1);
            num2 = get_reverse(num2);
            num1 += num2;
            num1 = get_reverse(num1);
            printf("%lld
    ",num1);
        }
        return 0;
    }
    
  • 相关阅读:
    《挑战程序设计竞赛》 一二章部分代码题解
    动态规划之矩阵连乘和POJ 1651
    关于图片的重绘,从而进行压缩
    iOS开发:读取pdf文件
    如何改变tableview的section的颜色
    端口的作用
    Mac 下,配置SVN
    cocoaPods 的安装和使用
    关于如何调用苹果自带的地图APP
    关于 HTTP 请求头的内容
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5832352.html
Copyright © 2020-2023  润新知