萌新求助关于 F 题
#include <bits/stdc++.h> using namespace std; int number[100]; int ss[314514]; bool cmp(int a, int b){ return a > b; } int main() { string s; cin >> s; int cnt = 0; for (int i = 0; i < s.size(); ++i) { cnt += (s[i] - '0'); // cout << cnt << ' '; //cout << s[i] << ' '; number[s[i] - '0']++; ss[i + 1] = (s[i] - '0'); // cout << ss[i]; } // cout << cnt; // for (int i = 1; i <= s.size(); ++i) { // cout << ss[i]; // } // cout << "nmsl"; if (cnt % 3 != 0) { cout << -1; return 0; } int A = 0, B = 0, C = 0; bool flg = false; for (int i = 0; i <= 9; ++i){ for (int j = 0; j <= 9; ++j){ for (int k = 0; k <= 9; ++k){ if ((i*100 + j*10 + k) % 125 == 0 && number[i] && number[j] && number[k]){ if (i == j) if (number[i] < 2) break; if (i == k) if (number[i] < 2) break; if (j == k) if (number[j] < 2) break; if (i == j && j == k) if (number[i] < 3) break; A = i, B = j, C = k; flg = true; goto exit; } } } } exit:; if (!flg) { cout << -1;return 0; } int n = s.size(); bool AA = 0, BB = 0, CC = 0; for (int i = 1; i <= n; ++i) { if (ss[i] == A && !AA) ss[i] = 10, AA = 1; else if (ss[i] == B && !BB) ss[i] = 10, BB = 1; else if (ss[i] == C && !CC) ss[i] = 10, CC = 1; } // for (int i = 1; i <= n; ++i) cout << ss[i]; sort(ss + 1, ss + n + 1, cmp); // for (int i = 1; i <= n; ++i) { // cout << ss[i] << ' '; // } for (int i = 4; i <= n; ++i) { cout << ss[i] ; } cout << A << B << C << endl; }
思路大概就是先判断能不能被 整除,然后如果可以就判断能不能取到 个数组成一个数,使其能被 整除。
结果 /kel