B - Adding Reversed Numbers
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).
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.
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.
324 14358 754305 794
3419981
题解:这道题的本质是一个字符串处理的问题,当然类似于高精度的加法,需要对每一位进行加法处理和进位处理。这个题的一个难点是两次翻转,那么其实我们经过计算可以发现,其实我们根本不需要任何翻转就可以达到同样的效果。比如24与1,我们直接让2+1就可以得到结果34,再比如4358 754,我们同样也可以通过对每一位进行加法使得每一位变成11 8 9 8,然后通过进位得到1 9 9 8,最后一组考察的是处理前导0的问题,因为305 794 加法之后得到 10 9 9,进位得到0 0 0 1,那么我们只要找到第一个不为0的数字然后输出,就可以达到那样的效果。
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;
char str1[1005],str2[1005];
int revstr1[1005],revstr2[1005];
int res[1005];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
/*重置部分*/
memset(res,0,sizeof(res));//重置res,revstr1,revstr2避免上一组数据对下一组数据产生影响
memset(revstr1,0,sizeof(revstr1));
memset(revstr2,0,sizeof(revstr2));
/*读入与转化部分*/
scanf("%s%s",str1,str2);//通过两个char数组接受两个字符串类型的数字
for(int i=0; i<=strlen(str1)-1; i++)//通过两个int数组接受两个转化为整型数值的数字方便进行加法运算
revstr1[i]=str1[i]-'0';
for(int i=0; i<=strlen(str2)-1; i++)
revstr2[i]=str2[i]-'0';
/*PS:我们当然也可以通过直接对字符进行加减运算获得结果,但相对来说整型加减运算更加方便*/
/*加法与进位部分*/
int length=max(strlen(str1),strlen(str2));//length表示当前结果的最大长度
for(int i=0; i<length; i++)
{
res[i]+=revstr1[i]+revstr2[i];
if(res[i]>9)
{
res[i+1]+=res[i]/10;
res[i]%=10;
if(i+1>=length) //如果进位时最大位提高了,我们就要更新结果长度
length++;
}
}
/*去除前导0部分*/
int p=0; //从第0位开始寻找,如果时0就继续找,知道找到第一个不是0的数
while(!res[p])
p++;
/*输出部分*/
for(int i=p; i<length; i++)//这时我们上面计算得到的p(第一个不是0的数的下标),length(结果的长度)就用上了
printf("%d",res[i]);
printf("\n");
}
return 0;
}