题解 | 九倍平方数

九倍平方数

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

// 写成一坨了,纪念一下自己辣的一坨
#include <iostream>
#include <string>
using namespace std;

void solve()
{
    string s;cin>>s;
    int cnt2 = 0,cnt3 = 0,sum = 0;
    for (char c:s)
    {
        // 0和1是不变的
        // 2有机会增加2,3有机会增加6
        sum+=c-'0';
        if (c == '3')
            cnt3++;
        if (c == '2')
            cnt2++;
    }
    sum%=9;if (sum == 0){cout<<"YES\n";return ;}
    if (sum %2)
    {
        int need2= (9-sum)/2;
        if (cnt2 >= need2)
        {cout<<"YES\n";return;}
        if (need2 == 4)
        {
            if (cnt2&&cnt3)
            {cout<<"YES\n";return;}
        }
        if (need2 ==3)
        {
            if (cnt3)
            {cout<<"YES\n";return;}
        }
    }
    else   
    {   
        // 凑出18来就行了
          int need2= (18-sum)/2;
        if (cnt2 >= need2)
        {cout<<"YES\n";return;}
        if (need2 == 5)
        {
            if (cnt3&&cnt2>=2)
            {cout<<"YES\n";return;}
        }
        else  
        {
            if (cnt3 >=2&&cnt2>=need2-6)
            {cout<<"YES\n";return;}
            if (cnt3&&cnt2>=need2-3)
            {cout<<"YES\n";return;}
        }
    }
    cout<<"NO\n";
}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);int t;cin>>t;
    while(t--)solve();return 0;
}
// 64 位输出请用 printf("%lld")

RogeAustine题解系列 文章被收录于专栏

这里是RogeAustine的题解专栏,里面包含的题目都是十分典型的经典题目。

全部评论

相关推荐

牛客51274894...:照片认真的吗,找个专门拍证件照的几十块钱整端正点吧,要不就别加照片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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