【华为OJ】201301 JAVA题目0-1级
2016-09-14 17:28
337 查看
输入:待输入整数的个数 整数数组
要求:将输入的整数分为和相同的两组,其中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的倍数的数放在一组,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;
}
相关文章推荐
- 201301 JAVA题目0-1级 华为OJ
- 【华为OJ】201301 JAVA 题目0-1级 将数组分为相等的两组
- 【华为OJ】201301 JAVA 题目0-1级
- 华为OJ 201301 JAVA题目0-1级
- 【华为OJ】【104-201301JAVA题目0-1级】
- 华为OJ——201301 JAVA题目0-1级
- 华为OJ 初级:201301 JAVA题目0-1级
- 华为oj 201301 JAVA题目0-1级
- 华为oj初级 201301 JAVA题目0-1级
- OJ-----201301 JAVA题目0-1级
- 华为OJ——201301 JAVA 题目2-3级(棋盘走法)
- 华为oj中级 201301 JAVA 题目2-3级
- 华为OJ——201301 JAVA 题目2-3级
- 【华为OJ】【059-201301-JAVA-题目2-3级】
- 华为OJ题目 句子逆序 java
- 201301 JAVA题目0-1级
- OJ---- 201301 JAVA 题目2-3级(走棋盘)
- 华为oj题目 DNA序列 [java语言]
- 华为oj题目(java版)
- 201301 JAVA题目0-1级