【華為OJ】201301 JAVA題目0-1級__華為OJ

來源:互聯網
上載者:User

輸入:待輸入整數的個數 整數數組

要求:將輸入的整數分為和相同的兩組,其中5的倍數的數放在一組,3的倍數(非5的倍數)的在另一組

輸出:若能夠分組,輸出true 若不能,輸出false

#include<iostream>#include<string>#include<math.h>using namespace std;void inc(int* mark, int len);bool canFind(int *list, int sum,int len) {int count = (int)pow(2, len);int*mark = new int[len];for (int i = 0; i < count; i++) {int val = 0;for (int j = 0; j < len; j++) {if (mark[j] == 1) {val += list[j];}}if (val == sum) {return true;}inc(mark,len);}return false;}void inc(int* mark,int len) {int carry;int idx = 0;do {mark[idx]++;carry = mark[idx] / 2;mark[idx] %= 2;idx++;} while (carry > 0 && idx < len);}int main(){int n,fi=0,th=0,ot=0,total=0, total_5 = 0, total_3 = 0, total_le =0 ;int num_5[100] = {0};int num_3[100] = {0};int num_le[100] = { 0 };cin >> n;for (int i = 0;i < n;i++){int a;cin >> a;total += a;if (a % 5 == 0){num_5[fi++] = a;total_5 += a;}else if (a % 3 == 0){num_3[th++] = a;total_3 += a;}else{num_le[ot++] = a;total_le += a;}}if (total % 2 != 0)cout << "false" << endl;else if (total_le == abs(total_3 - total_5)) cout << "true" << endl;else if (total_le < abs(total_3 - total_5))cout << "false" << endl;else{int aim = total/2-total_5;if(canFind(num_le, aim,ot)) cout<<"true"<<endl;else 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.