자라는 개발자/문제풀이

백준 c++ 3의 배수

자란다 2022. 3. 29. 14:05
728x90
반응형

1769 3의 배수


문제풀이

#include <iostream>
#include <string>
using namespace std;
void fast_io(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
string s;
int a,cnt;
int main()
{
    fast_io();

    cin >> s;
    while(s.length()!=1)
    {
        a=0;
        cnt++;
        for(int i=0;i<s.length();i++)
        {
            a+=s[i]-'0';
        }

        s=to_string(a);
    }
    a=s[0]-'0';
    if(a<10)
       {
           cout << cnt << "\n";
           if(a%3==0) cout << "YES";
           else cout << "NO";
       }
   return 0;
}

재밌는 문제였다!

728x90
반응형