题解 | 九倍平方数

九倍平方数

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

各个位相加为9的倍数可以被9整除,每个2可以增加2,每个3可以增加6

#include <iostream>
#include<string>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        int num[10] = {0};
        int sum = 0;
        string s;
        cin >> s;
        for (char c:s) {
            int x=c-'0';
            sum+=x;
            num[x]++;
        }
        int rem=sum%9;
        int c2=num[2],c3=num[3];
        int m=c2*2+c3*6;
        bool is=false;
        for(int i=0;i<=m;i+=2){
            int need=i;
            int b=min(need/6,c3);
            need-=6*b;
            if(need<=2*c2&&need%2==0){
                if((rem+i)%9==0){
                    is=true;
                    break;
                }
            }
        }
        cout<<(is?"YES":"NO")<<"\n";
    }
    return 0;
}

全部评论

相关推荐

10-28 10:48
已编辑
门头沟学院 Java
孩子我想要offer:发笔试后还没笔试把我挂了,然后邮箱一直让我测评没测,后面不知道干嘛又给我捞起来下轮笔试,做完测评笔试又挂了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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