구현(Implementation)

백준 4388: 받아올림

cepiloth 2018. 6. 13. 11:48
반응형

백준 4388: 받아올림

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



1. 문제 요약

두 수가 주어지고 덧셈을 했을 때 받아 올림이 몇 번 발생하는지 구하는 문제


2. 알고리즘

각 숫자의 마지막 수의 덧셈에서 10 이상이 값이 나오면 올림이 발생함으로 10이상 나오는 경우를 카운팅 한다.


3. 코드

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
48
#include <iostream>
#include <algorithm> // min
#include <functional>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <sstream>
#include <queue>
using namespace std;
 
int main() {
 
    std::ios::sync_with_stdio(false); cin.tie(0);
 
    while(true) {
        int n, k; cin >> n >> k;
        
        // 종료 조건
        if(n == 0 && k == 0) {
            break;
        }
 
        int sol = 0;
        while(n && k) {
            int carry = 0;
            int cand = (n % 10 + k % 10/ 10;
            if (cand) {
                carry = cand;
            }
            n = n / 10;
            k = k / 10;
 
            if (n > k) {
                k += carry;
            } else {
                n += carry;
            }
 
            if (carry) {
                sol++;
            }
        }
        cout << sol << endl;
    }
    return 0;
}
cs

반응형