文章
2
粉丝
0
获赞
12
访问
363
一开始的时候写了个全排列,后面想想不对劲,似乎一个数只需要满足每一位加起来为三的倍数、并且为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;
}
登录后发布评论
暂无评论,来抢沙发