algorithm/BOJ

BOJ 10610번 30

_JunHo 2020. 1. 17. 16:02

BOJ : https://www.acmicpc.net/problem/10610

github : https://github.com/junho0956/Algorithm/blob/master/10610/10610/%EC%86%8C%EC%8A%A4.cpp

 

30의 배수가 되기 위해서는 2가지의 조건이 필요합니다.

1) 마지막숫자가 0 이여야합니다. ==> 0의 존재여부를 확인해주시면 됩니다.

2) 수를 전부 더 했을 때 3의 배수 즉, 3으로 나누어떨어져야합니다.

30의 배수는 30 60 90 120 150 180 210 ,,, 이 되는데 숫자들을 자리수끼리 더해보면 결국 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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
 
bool cmp(char& a, char& b) { return a > b; }
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    string str;
    cin >> str;
 
    long long total = 0, check = 0;
    for (int i = 0; i < str.size(); i++) {
        total += str[i] - '0';
        if (str[i] == '0') check = 1;
    }
 
    if (check && total % 3 == 0) {
        sort(str.begin(), str.end(), cmp);
        cout << str << '\n';
    }
    else cout << "-1\n";
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter