반응형

문제 출처 :


https://www.acmicpc.net/problem/3062



알고리즘 분석 :


문제 해결에 필요한 사항

1. string


1. 수를 문자열로 입력받는다.

2. rev string에 문자열을 받고 reverse를 통해 뒤집는다.

3. get변수에 stoi를 통해 문자열을 숫자로 변환하여 더한다.

4. get은 int형이니 string형으로 변환하기 위해 아래 while 과정을 거친다.

5. ans와 rev가 같다면 뒤집어도 같은 문자열임을 알 수 있다.













소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;
 
int main()
{
    string str;
    int tc;
    cin >> tc;
 
    while (tc--)
    {
        cin >> str;
        string rev = str;
        reverse(rev.begin(), rev.end());
 
        int get = stoi(str) + stoi(rev);
 
        rev.clear();
        while (get != 0)
        {
            int val = get % 10;
            rev += (char)val +'0';
 
            get /= 10;
        }
 
        string ans = rev;
 
        reverse(ans.begin(), ans.end());
 
        if (ans == rev)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
 
    }
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형