Computer Science/Algorithm Problem

백준] 3486 - Adding Reversed Numbers(ACM-ICPC Regionals)

TwinParadox 2019. 2. 7. 15:02
728x90

시간 제한 : 1초

메모리 제한 : 128MB




입력

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. Two integers are less than 100,000,000.




출력

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.




소스코드

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main(void)
{
	int N;
	cin >> N;
	while (N--)
	{
		string a, b, sum = "";
		int sumInt = 0, aInt, bInt;
		cin >> a >> b;

		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
		aInt = stoi(a);
		bInt = stoi(b);

		sumInt = aInt + bInt;
		sum = to_string(sumInt);
		reverse(sum.begin(), sum.end());
		sumInt = stoi(sum);
		cout << sumInt << '\n';;
	}
}




Tip

C++에서는 reverse와 string만 이용하면 별다른 구현 없이 문제를 풀 수 있다. 문제를 간략하게 설명하자면, 입력받은 두 수를 뒤집어서 계산한 값을 뒤집어서 출력하는 것이다.



728x90