Advertisement
Merevoli

Untitled

Mar 29th, 2022
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. //    freopen("input.txt", "r", stdin);
  7. //    freopen("output.txt", "w", stdout);
  8.  
  9.     string s;
  10.     cin >> s;
  11.  
  12.     int sum = 0;
  13.     for (char c : s){
  14.         sum += (int)c - 48;
  15.     }
  16.  
  17.     int d = sum % 3;
  18.  
  19.     int res = 0;
  20.     if (d == 0){
  21.         cout << 0;
  22.     }
  23.     else if (d == 1){
  24.         if (s.length() == 1){
  25.             cout << -1;
  26.             return 0;
  27.         }
  28.         bool is_found = false;
  29.         for (int i=0; i<s.length(); i++){
  30.             int t = (int)s[i] - 48;
  31.             if (t % 3 == 1){
  32.                 res++;
  33.                 is_found = true;
  34.                 break;
  35.             }
  36.         }
  37.  
  38.         if (!is_found){
  39.             int t_2 = 0;
  40.             for (int i=0; i<s.length(); i++){
  41.                 int t = (int)s[i] - 48;
  42.                 if (t % 3 == 2){
  43.                     t_2++;
  44.                 }
  45.             }
  46.  
  47.             if (t_2 == 2){
  48.                 if (s.length() == 2){
  49.                     cout << -1;
  50.                     return 0;
  51.                 }
  52.                 else{
  53.                     cout << 2;
  54.                     return 0;
  55.                 }
  56.             }
  57.             else{
  58.                 cout << -1;
  59.                 return 0;
  60.             }
  61.         }
  62.         else{
  63.             cout << res;
  64.         }
  65.     }
  66.     else{
  67.         if (s.length() == 1){
  68.             cout << -1;
  69.             return 0;
  70.         }
  71.         bool is_found = false;
  72.         for (int i=0; i<s.length(); i++){
  73.             int t = (int)s[i] - 48;
  74.             if (t % 3 == 2){
  75.                 res++;
  76.                 is_found = true;
  77.                 break;
  78.             }
  79.         }
  80.  
  81.         if (!is_found){
  82.             int t_1 = 0;
  83.             for (int i=0; i<s.length(); i++){
  84.                 int t = (int)s[i] - 48;
  85.                 if (t % 3 == 1){
  86.                     t_1++;
  87.                 }
  88.             }
  89.  
  90.             if (t_1 == 2){
  91.                 if (s.length() == 2){
  92.                     cout << -1;
  93.                     return 0;
  94.                 }
  95.                 else{
  96.                     cout << 2;
  97.                     return 0;
  98.                 }
  99.             }
  100.             else{
  101.                 cout << -1;
  102.                 return 0;
  103.             }
  104.         }
  105.         else{
  106.             cout << res;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement