题解 | 九倍平方数

九倍平方数

https://www.nowcoder.com/practice/032c72fab5fe4a2ea8e11d40378a493d

#include <iostream>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--)
    {
        string n;
        cin >> n;
        int sumn = 0;
        int count2 = 0;
        int count3 = 0;
        int nlength = n.size();
        bool yes = false;
        for (int i = 0; i < nlength; i++)
        {
            if (n[i] == '2') count2++;
            if (n[i] == '3') count3++;
            sumn += n[i]-'0';
        }

        for (int i = 0; i <= count2; i++)
        {
            for (int j = 0; j <= count3; j++)
            {
                int sumntemp = sumn + i * 2 + j * 6;
                if (sumntemp % 9 == 0)
                {
                    cout << "YES" << endl;
                    yes = true;
                }
                if (yes)break;
            }
            if (yes)break;
        }

        if (!yes) cout << "NO" << endl;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务