Algorithm/BOJ
[BOJ]10610 - 30 (C++)
꾸지새미언니
2022. 1. 4. 17:44
문제
https://www.acmicpc.net/problem/10610
코드
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
string str;
cin >> str;
vector<int> v;
//더해서 3의 배수인지 보기
int total =0;
for (int i=0; i<str.size();i++){
total += str[i] - '0';
v.push_back(str[i]-'0');
}
sort(v.begin(), v.end(), greater<int>());
if(total%3==0 && v[str.size()-1]==0){
for(auto i : v)
cout << i;
} else{
cout << -1;
}
}
풀이
숫자를 다 뜯어서 각 자릿수의 숫자를 따로 봐야하기 때문에 문자열로 입력받았다.
각 문자열을 돌면서 합(total)을 구하고 문자 하나하나 vector에 삽입한다.
그 다음에는 벡터를 내림차순으로 정렬해준다.
이렇게 정렬된 벡터는 맨 뒷자리 수(일의 자리 수)가 0이어야 하고, total이 3의 배수이어야 한다.(30의 배수인지 알 수 있는 방법)
해당 조건들을 만족하면, 이미 정렬된 벡터를 순서대로 출력하면 된다!