반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. String STL

2. 문제 이해


만약 12345가 있다면, 1 + 2 + 3 + 4 + 5 = 15 -> 1 + 5 = 6이므로 답은 6이 도출된다.

하지만 

4 + 5 = 9 

-> 3 + 9 = 12 -> 1 + 2 = 3 

-> 2 + 3 = 5 

-> 1 + 5 = 6 처럼 끝에서 두개씩만 추출하여 연산을 계속 해주어도 똑같은 답이 도출된다. 


stack을 이용해도 되고 , string을 이용해도 된다.


소스 코드 : 


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
46
47
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
 
using namespace std;
 
stack<int> s;
 
int main()
{
    string str;
 
    while (1)
    {
        cin >> str;
 
        if (str.at(0== '0')
            return 0;
 
        while (!str.empty())
        {
            if (str.size() == 1)
                break;
            int a = str.back() - 48;
            str.pop_back();
            int b = str.back() - 48;
            str.pop_back();
 
            int c = a + b;
            if (c >= 10)
            {
                a = c % 10;
                b = c / 10;
                c = a + b;
            }
            str.push_back(c + 48);
        }
 
        cout << str << endl;
    }
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[11375번] 열혈강호  (3) 2017.02.10
[14430번] 자원 캐기  (0) 2017.02.08
[1967번] 트리의 지름  (0) 2017.02.04
[11725번] 트리의 부모 찾기  (0) 2017.02.03
[2250번] 트리의 높이와 너비  (2) 2017.02.03