Dividing
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28488 | Accepted: 6968 |
Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.".
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1: Can't be divided. Collection #2: Can be divided.
Source
解题思路:
为了判断是否能够平分收集得到的“大理石”,我的方法是先将这些“大理石”“一个一个”地放入向量中,然后采用回溯法求解。感觉有点把问题复杂化了,但还是成功地求解了改问题。
我的代码:
#include <iostream>
#include <vector>
#include <vector>
using namespace std;
int main()
{
int a[7];
int i,j,s=0;
vector<int> vnum,choose,index;
{
int a[7];
int i,j,s=0;
vector<int> vnum,choose,index;
while( cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6])
{
if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)
break;
s++;
{
if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)
break;
s++;
bool flag = false;
int val = 0;
vnum.clear(); // 清空向量
int val = 0;
vnum.clear(); // 清空向量
// 求总价值
for(i=1; i<7; i++ )
val+=(i*a[i]);
// 将所有价值存至向量vnum
for(i=6; i>0; i-- )
for(j=0; j<a[i]; j++ )
{
vnum.push_back(i);
}
for(i=1; i<7; i++ )
val+=(i*a[i]);
// 将所有价值存至向量vnum
for(i=6; i>0; i-- )
for(j=0; j<a[i]; j++ )
{
vnum.push_back(i);
}
choose.clear();
index.clear();
// 判断是否能平分
if(val%2!=0)
{
flag =false;
}
else
{
int one=0;
// 回溯法搜索平分方法
for( i=0; i<vnum.size(); i++ )
{
if( one+vnum[i]<val/2)
{
one += vnum[i];
index.push_back(i);
choose.push_back(vnum[i]);
}
else if(one+vnum[i]==val/2)
{
flag = true;
break;
}
else if( i==vnum.size()-1 && index.size()!=0 )
{
i = index[index.size()-1];
one -= choose[choose.size()-1];
index.pop_back();
choose.pop_back();
}
}
}
index.clear();
// 判断是否能平分
if(val%2!=0)
{
flag =false;
}
else
{
int one=0;
// 回溯法搜索平分方法
for( i=0; i<vnum.size(); i++ )
{
if( one+vnum[i]<val/2)
{
one += vnum[i];
index.push_back(i);
choose.push_back(vnum[i]);
}
else if(one+vnum[i]==val/2)
{
flag = true;
break;
}
else if( i==vnum.size()-1 && index.size()!=0 )
{
i = index[index.size()-1];
one -= choose[choose.size()-1];
index.pop_back();
choose.pop_back();
}
}
}
if(flag==true)
{
cout<<"Collection #"<<s<<":"<<endl;
cout<<"Can be divided."<<endl;
cout<<endl;
}
else
{
cout<<"Collection #"<<s<<":"<<endl;
cout<<"Can't be divided."<<endl;
cout<<endl;
}
}
{
cout<<"Collection #"<<s<<":"<<endl;
cout<<"Can be divided."<<endl;
cout<<endl;
}
else
{
cout<<"Collection #"<<s<<":"<<endl;
cout<<"Can't be divided."<<endl;
cout<<endl;
}
}
return 0;
}
}