码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1002 A - A + B Problem II (大数问题)

时间:2017-07-16 22:36:15      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:and   tar   style   name   ber   turn   job   数据   otto   

原题代号:HDU 1002

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

原题描述:

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
 
题目大意:完成长度在1000以内的任意两数相加 (转换成字符串处理)
解法:
 
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL;

char a[1000+5],b[1000+5],c[1000+5];

int main()
{
    int t,sum=0;
    cin>>t;
    while(t--)
    {
        if(sum)cout<<endl;
        char str1[1000+5],str2[1000+5];
        mem(a,‘\0‘);
        mem(b,‘\0‘);
        mem(c,‘\0‘);//因为多组数据,所以清零
        cin>>str1;
        for(int i=sizeof(a),j=strlen(str1)-1; j>=0; j--,i--)
            a[i]=str1[j]-48;
        cin>>str2;
        for(int i=sizeof(b),j=strlen(str2)-1; j>=0; j--,i--)//将字符串放在数组末尾,并且将字符转换成ASCLL码所对应的数字
            b[i]=str2[j]-48;
        int num=0,i;
        for(i=sizeof(a); i>0; i--)
        {
            c[i]=a[i]+b[i]+num;
            num=c[i]/10;
            c[i]%=10;
        }
        printf("Case %d:\n%s + %s = ",++sum,str1,str2);
        for(i=0;i<=10005;i++)if(c[i])break;
        for(int j=i; j<=sizeof(c); j++)
            printf("%d",c[j]);//将大数表示出来
        cout<<endl;
    }
    return 0;
}

  

HDU 1002 A - A + B Problem II (大数问题)

标签:and   tar   style   name   ber   turn   job   数据   otto   

原文地址:http://www.cnblogs.com/teble/p/7192089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!