HDOJ1002题A + B Problem II,2个大数相加

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input
2 1 2 112233445566778899 998877665544332211

Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110

#include < stdio.h>
#include < stdlib.h>
#include < string.h>
int main()
{
    char a[1010],b[1010],c[1010];
    int a1,b1,m,i,l,i1,j1,n1,m1,a2,b2,j=1,t1,t2,n,p=0;
    scanf("%d",&n);
    while(n--)
    {
        p=0;
        scanf("%s",a);
        scanf("%s",b);
        printf("Case %d:\n",j++);
        printf("%s + %s = ",a,b);
        a1=strlen(a);
        b1=strlen(b);
        a2=a1;
        b2=b1;
        for(i=0; a1>=0||b1>=0; i++,a1--,b1--)
        {
            if(a1>=0&&b1>=0)
            {
                c[i]=a[a1]+b[b1]-'0'+p;
            }
            else if(a1>=0&&b1<0)
            {
                c[i]=a[a1]+p;
            }
            else if(a1<0&&b1>=0)
            {
                c[i]=b[b1]+p ;
            }
            p=0;
            if(c[i]>'9')
            {
                c[i]=c[i]-10;
                p=1;
            }
        }
        if(p==1)
            printf("%d",p);
        t1=1;
        t2=i-1;
        n1=m1=0;
        for(i1=0; i1
        {
            if(a[i1]=='0')
                n1++;
        }
        for(j1=0 ; j1
        {
            if(b[j1]=='0')
                m1++;
        }
        if(n1==a2&&m1==b2)
        {
            printf("0");
        }
        else
        {
            for(l= i-1 ; l>0; l--)
            {
                if(t2==l&&c[l]=='0'&&p!=1)
                {
                    t2--;
                    continue;
                }
                printf("%c",c[l]);
            }
        }
        if(n!=0)
            printf("\n\n");
        else
            printf("\n");
    }
    return 0;
}
时间: 2024-09-20 15:05:01

HDOJ1002题A + B Problem II,2个大数相加的相关文章

C - Train Problem II——(HDU 1023 Catalan 数)

传送门 Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7616    Accepted Submission(s): 4101 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train

【JAVA大数训练】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 196702    Accepted Submission(s): 37626 Problem Description I have a very simple problem for you. Given two integers A and B, yo

[LeetCode 第11题] -- Linked List Cycle II

题目链接: Linked List Cycle II 题目意思: 给定一个链表,如果链表有环求出环的起点,否则返回NULL 解题思路:      1. 判断链表是否有环: 两个指针,一个一次走一步,一个一次走两步,如果指针相遇说明有环,否则无环.     2. 如果有环的情况下,我们可以画个图(图片来自网络)                   假设两个指针在z点相遇.则          a. 指针1走过路程为a + b:指针2走过的路程为 a+b+c+b          b. 因为指针2的

[ACMcoder] A + B Problem II

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

HDOJ 1002 A + B Problem II

Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines fol

[华为机试真题][2014]64.实现两个合法时间相加

题目 描述: 给定两个合法的时间(格式固定:hh:mm:ss,时间合法,不用考虑其它情况),输入两个时间相加后的结果:注意,相加后的结果也必需是一个合法的时间: 附合法时间定义:小时在[00-23]之间,分钟和秒分别是在[00-59]之间: 运行时间限制: 无限制 内存限制: 无限制 输入: 时分秒格式的时间字符串,如00:00:00 输出: 时分秒格式的时间字符串,如00:00:00 样例输入: 00:00:00 00:00:01 样例输出: 00:00:01 答案提示: 建议将时间转换为秒数

欧拉项目【ProjectEuler】系列-第一题

欧拉项目[ProjectEuler]系列-第一题 ----人既无名 既然是第一次,当然要写个基本的介绍咯. 欧拉项目是一系列挑战数学/计算机编程的问题. 需要的不仅仅是数学见解,还要利用计算机编程技巧,需要解决大多数问题,当然,数学帮助你运用优雅而有效的方法,实现漂亮而快速的代码. 欧拉项目的网站是http://projecteuler.net,只要上去注册一个账号就可以开始你的欧拉之旅了,当你把一个问题解决之后就可以参加该问题的讨论,说说你的解决办法,看看其他人的处理思路咯.言归正传,开始欧拉

KMP专题【完结】

第一题 hdu 1711 Number Sequence 点击打开hdu 1711 思路: 1 kmp是用来匹配字符串,只能够匹配单一的字符串 2 kmp的算法的过程:   1:假设文本串的长度为n,模式串的长度为m:   2:先例用O(m)的时间去预处理next数组,next数组的意思指的是当前的字符串匹配失败后要转到的下一个状态:   3:利用o(n)的时间去完成匹配: 3 时间复杂度为o(n+m)即o(n): 点击查看代码 第二题 hdu 1686 oulipo 点击打开hdu 1686

HDOJ1001-1005题解

1001--Sum Problem(http://acm.hdu.edu.cn/showproblem.php?pid=1001) #include <stdio.h> int sum(int n) { if(n % 2) return (n + 1) / 2 * n; else return (n / 2) * (n + 1); } int main() { int n; while(scanf("%d",&n) != EOF){ printf("%d\