文章

2

粉丝

0

获赞

12

访问

363

头像
求30的倍数 题解:
P1736 华东师范大学2020年机试题
发布于2026年3月5日 21:59
阅读数 204

一开始的时候写了个全排列,后面想想不对劲,似乎一个数只需要满足每一位加起来为三的倍数、并且为10的倍数即可。

满足条件后只需要做一个逆排序即可

#include<bits/stdc++.h>
using namespace std;


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    
    int len = s.size();
    int sum = 0;
    bool zero_flag = false;
    for (const auto& c : s){
        sum += c - '0';
        if (c - '0' == 0) zero_flag = true;
    }
    
    if (!zero_flag || sum % 3 != 0){
        cout << -1 << '\n';
        return 0;
    }

    sort(s.rbegin(),s.rend());
    cout << s << endl;
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发