读书人

杭电1002题意解决办法

发布时间: 2012-04-23 13:17:38 作者: rapoo

杭电1002题意
哪位大神 能用汉语解释下杭电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



[解决办法]
写得很清楚了啊,case1:两个int数相加。
case2:数值超过了int型最大值,大型数通过每位相加然后加进位最终得出结果。

[解决办法]
翻译如下:

问题描述  我有一个非常简单的问题。给出两个整数A和B,你的任务是计算A + B的总和。

输入:
输入的第一行包含一个整数T(1 < = T < = 20)这意味着测试用例的数目。然后是T行,每一行都是两个正整数,A和B 。注意到整数是非常大的,这意味着你不应该用32位的整数处理它们。你会认为每一个整数的长度不超过1000。

输出:
对于每个测试的情形,你应该输出两排。第一行“Case #:“,#意味着第几个测试用例。第二行是一个表达式”A + B = Sum”,Sum是A + B 的和.注意表达式中有空格。在两个测试用例之间输出一个空白的一行。

下面是我写的一个代码:

C/C++ code
#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){    char a[1010],b[1010],res[1010];    int i,j,c,T,index,count;    scanf("%d",&T);    getchar();    for (count=1;count<=T;count++)    {        scanf("%s",a);        scanf("%s",b);        c=index=0;        for (i=strlen(a)-1,j=strlen(b)-1;i>=0&&j>=0;i--,j--)        {            res[index]=a[i]+b[j]-48+c;            c=res[index]>'9'?1:0;            res[index++]-=c*10;        }        while (i>=0)        {            res[index]=a[i]+c;            c=res[index]>'9'?1:0;            res[index++]-=c*10;            i--;        }        while (j>=0)        {            res[index]=b[j]+c;            c=res[index]>'9'?1:0;            res[index++]-=c*10;            j--;        }        if (c==1)        {              res[index++]=c+48;        }        printf("Case %d:\n",count);                printf("%s + %s = ",a,b);        for (;index>0&&res[index-1]=='0';index--);        res[index]=0;        for (i=index-1;i>=0;i--)        {            printf("%c",res[i]);        }        printf("\n");        if (count!=T)        {            printf("\n");        }    }        return 1;} 

读书人网 >C语言

热点推荐