본문 바로가기

Algorithm_BOJ(백준)/수학(Math)

[백준 10610 c++ V] 30

728x90
반응형

- 풀이 링크: 

https://github.com/xhaktmchl/Algorithm_study/blob/main/BOJ/%EC%88%98%ED%95%99(Math)/%5B%EB%B0%B1%EC%A4%80%2010610%20c%2B%2B%20V%5D%2030.cpp 

 

GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이

알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.

github.com

#include <iostream>
#include <algorithm>
//#include <map> // 중복 x 
#include <string> // getline
#include <vector>

using namespace std;
/*
[백준 10610 c++ V] 30
문제:  
접근: 3의 배수 -> 모든 자리의 숫자의 합이 3의 배수여야 함, 30의 배수-> 마지막 자리가==0이어야 함
시간복잡도: O()
풀이:
       //1.입력
    //2.가장큰수로 정렬
    //3.3의 배수인지 검사
    //4.출력 존재 or 미존재
*/
string str;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    //1.입력
    cin >> str;
    //2.가장큰수로 정렬
    sort(str.begin(), str.end(), greater<>());
    //3.3의 배수인지 검사
    int size = str.length();
    int sum = 0;

    if (str[size - 1] != '0') {
        cout << -1 << '\n';
        return 0;
    }
     for (int i = 0; i < size; i++) {
         sum += str[i] - '0';
     }
     //4.출력 존재 or 미존재
     if (sum % 3 == 0) cout << str << '\n';
     else cout << -1 << '\n';
    return 0;
}
반응형

'Algorithm_BOJ(백준) > 수학(Math)' 카테고리의 다른 글

[백준 1934 c++ O] 최소공배수  (0) 2023.02.20
[백준 1735 c++ O] 분수 합  (0) 2023.01.12
[백준 10430 c++ OO] 나머지  (0) 2021.12.22
[백준 5543 c++ O] 상근날드  (0) 2021.08.30
[백준 2587 c++ O] 대표값2  (0) 2021.08.30