Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- string s;
- cin >> s;
- int sum = 0;
- for (char c : s){
- sum += (int)c - 48;
- }
- int d = sum % 3;
- int res = 0;
- if (d == 0){
- cout << 0;
- }
- else if (d == 1){
- if (s.length() == 1){
- cout << -1;
- return 0;
- }
- bool is_found = false;
- for (int i=0; i<s.length(); i++){
- int t = (int)s[i] - 48;
- if (t % 3 == 1){
- res++;
- is_found = true;
- break;
- }
- }
- if (!is_found){
- int t_2 = 0;
- for (int i=0; i<s.length(); i++){
- int t = (int)s[i] - 48;
- if (t % 3 == 2){
- t_2++;
- }
- }
- if (t_2 == 2){
- if (s.length() == 2){
- cout << -1;
- return 0;
- }
- else{
- cout << 2;
- return 0;
- }
- }
- else{
- cout << -1;
- return 0;
- }
- }
- else{
- cout << res;
- }
- }
- else{
- if (s.length() == 1){
- cout << -1;
- return 0;
- }
- bool is_found = false;
- for (int i=0; i<s.length(); i++){
- int t = (int)s[i] - 48;
- if (t % 3 == 2){
- res++;
- is_found = true;
- break;
- }
- }
- if (!is_found){
- int t_1 = 0;
- for (int i=0; i<s.length(); i++){
- int t = (int)s[i] - 48;
- if (t % 3 == 1){
- t_1++;
- }
- }
- if (t_1 == 2){
- if (s.length() == 2){
- cout << -1;
- return 0;
- }
- else{
- cout << 2;
- return 0;
- }
- }
- else{
- cout << -1;
- return 0;
- }
- }
- else{
- cout << res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement