華為oj初級 201301 JAVA題目0-1級__JAVA

來源:互聯網
上載者:User

描述
編寫一個函數,傳入一個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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.