描述
編寫一個函數,傳入一個int型數組,返回該數組能否分成兩組,使得兩組中各元素加起來的和相等,並且,所有5的倍數必須在其中一個組中,所有3的倍數在另一個組中(不包括5的倍數),能滿足以上條件,返回true;不滿足時返回false。
知識點 字串,迴圈,函數,指標,枚舉,位元運算,結構體,聯合體,檔案操作,遞迴
已耗用時間限制 10M
記憶體限制 128
輸入
輸入輸入的資料個數
輸入一個int型數組
輸出
返回true或者false
範例輸入 4 1 5 -5 1
範例輸出 true
#include <iostream> #include<vector>#include<algorithm>#include<string>using namespace std;bool sum_same(int sum1, int sum2, int i, vector<int> &v){ for (int j = 0; j <= i; j++){ sum1 += v[j]; } for (int j = i + 1; j < v.size(); j++) sum2 += v[j]; if (sum1 == sum2) return true; else return false;}int main(){ vector<int> v,v3,v5; int n; cin >> n; int t; for (int i = 0; i < n; i++){ cin >>t; if (t % 5 == 0){ v5.push_back(t); } else if (t % 3 == 0){ v3.push_back(t); } else{ v.push_back(t); } } int sum1=0; int sum2 = 0; for (int i = 0; i < v5.size(); i++) sum1 += v5[i]; for (int i = 0; i < v3.size(); i++) sum2 += v3[i]; sort(v.begin(), v.end()); int flag = false; do{ for (int i = 0; i < v.size(); i++){ if (sum_same(sum1, sum2, i, v)){ cout << "true" << endl; return 0; } } } while (next_permutation(v.begin(), v.end())); cout << "false" << endl; return 0;}