题目描述
一个数组中有若干正整数,将此数组划分为两个子数组,使得两个子数组各元素之和a,b的差最小,对于非法输入应该输出ERROR。
输入
数组中的元素
输出
降序输出两个子数组的元素和
样例输入 Copy
10 20 30 10 10
10 20 abc 10 10
样例输出 Copy
40 40
ERROR
思路:
1.先getline处理读入的字符串,每读到一个空格,就可以判断一个子串是不是数字。如果是则存入数组之中,如果不是就error。
2.如果1步骤合法,那么开始dfs处理数组。关键:搜索每一个最小于等于sum/2的子数组,并且找出其中和最接近sum/2的子数组。
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <memory.h>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
bool judge(string s) {
for (int i = 0; i < s.length(); i++)