AC

大师  •  1天前


include

include

include

include

include

//#define DEBUG 0 using namespace std;

int main() {

string num;
cin >> num;

vector<char> digits(num.begin(), num.end());

ifdef DEBUG

for (auto i : digits) {
    cout << i<<" ";
}
cout << endl;

endif

sort(digits.begin(), digits.end()); 

ifdef DEBUG

    for (auto i : digits) {
        cout << i << " ";
    }
cout << endl;

endif

int n = digits.size();
int m = n / 2;
int k = n - m;

ifdef DEBUG

cout << n << m << k << endl;

endif

int min_sum = INT_MAX;

// 生成所有可能的分割,确保两个数的位数是 m 和 k
do {
    string a_str, b_str;
    for (int i = 0; i < m; ++i) {
        a_str += digits[i];
    }
    for (int i = m; i < n; ++i) {
        b_str += digits[i];
    }

ifdef DEBUG

    cout << a_str << " " << b_str << endl;

endif

    // 去除前导零
    int a = stoi(a_str);
    int b = stoi(b_str);

    if (a + b < min_sum) {
        min_sum = a + b;
    }
} while (next_permutation(digits.begin(), digits.end()));

cout << min_sum << endl;

return 0;

}


评论:

请先登录,才能进行评论