Divideing Jewels
Divideing Jewels
时间限制: 1 Sec 内存限制: 128 MB
提交: 63 解决: 17
[提交][状态]
题目描述
Mary
and Rose own a collection of jewells. They want to split the collection
among themselves so that both receive an equal share of the jewels.
This would be easy if all the jewels had the same value, because then
they could just split the collection in half. But unfortunately, some of
the jewels are larger, or more beautiful than others. So, Mary and Rose
start by assigning a value, a natural number between one and ten, to
each jewel. Now they want to divide the jewels so that each of them gets
the same total value. Unfortunately, they realize that it might be
impossible to divide the jewels in this way (even if the total value of
all jewels is even). For example, if there are one jewel 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 jewels.
输入
Each
line in the input file describes one collection of jewels to be
divided. The lines contain ten non-negative integers n1 , . . . , n10 ,
where ni is the number of jewels of value i. The maximum total number of
jewells will be 10000.
The last line of the input file will be "0 0 0 0 0 0 0 0 0 0"; do not process this line.
输出
For
each collection, output "#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.
样例输入
1 0 1 2 0 0 0 0 2 0
1 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
样例输出
#1:Can't be divided. #2:Can be divided.
提示
来源
你一定要好好看题,一定先要==||
#include<iostream>
#include<cstdio>
using namespace std;
int sum[15], tot, flag;
void DFS(int q) // q用来存现在分到的价值
{
int i;
if(flag)
return ;
if(q == tot) //如果价值==tot就是可以平分
{
flag = 1;
return ;
}
for(i = 10; i > 0; --i) //从后往前加,后边价值大
{
if(sum[i] > 0 && q+i <= tot) // i 是价值,sum【i】是i 这个价值的有几个~
{
sum[i]--; //加上一个就减去一个
DFS(q+i); //看q+i 是否够一半
if(flag)
return ;
}
}
}
int main()
{
int i, t;
for(t = 1; ; t++) //循环开始
{
tot = 0;
for(i = 1; i <= 10; i++)
{
cin >> sum[i];
tot += sum[i] * i; // sum【i】是i 这个价值的有几个!!tot总价值
}
if(!tot)
break; // 价值为零,循环结束,程序结束
if(tot & 1) //tot是奇数
{
printf("#%d:Can't be divided.\n", t);
continue;
}
tot >>= 1; // >>位运算里边的东西,可以百度一下,这句话意思就是tot /=2;
flag = false;
DFS(0);
if(flag)
printf("#%d:Can be divided.\n", t);
else printf("#%d:Can't be divided.\n", t);
}
return 0;
}
sum【i】是i 这个价值的珠宝有几个!!
~~
分享更好链接:
http://www.cnblogs.com/dongsheng/archive/2013/04/22/3036160.html
Divideing Jewels的更多相关文章
- nyoj 546——Divideing Jewels——————【dp、多重背包板子题】
Divideing Jewels 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Mary and Rose own a collection of jewells. ...
- 河南省第五届ACM程序设计大赛
D: 遥 控 器 #include<cstdio> #include<cstring> #include<iostream> #include<algor ...
- hdu.1044.Collect More Jewels(bfs + 状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044 Collect More Jewels(bfs+状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- 771. Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
随机推荐
- 交换机vlan配置
vlan:virtual LAN 虚拟局域网 作用:通过VLAN技术,可以对局域网进行隔离,互相隔离开的局域网相互之间不能进行通信,一个VLAN为一个广播域 Vlan配置 GNS3(使用路由器来模拟 ...
- [Python3] 023 面向对象 第三弹
目录 7. 类相关函数 8. 类的成员描述符(属性) 9. 类的内置属性 10. 类的常用魔术方法 10.1 操作类 10.2 描述符 10.3 属性操作 10.4 运算分类相关魔术方法 接上一篇 [ ...
- 应该用forEach改变数组的值吗? 原生JS forEach()和map()遍历的异同点
应该用forEach改变数组的值吗? https://segmentfault.com/q/1010000013170900?utm_source=index-hottest 由于js中的数组是引用类 ...
- CSS选择器,优先级的总结
CSS选择器 css选择器种类 基本选择器: 通配符选择器 * id选择器 #id 类选择器 .className 元素选择器 E 元素后代选择器 E F 子元素选择器 E > F 相邻兄弟元 ...
- 矩阵快速幂(queue递推)
http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others) Me ...
- manacher算法学习(求最长回文子串长度)
Manacher总结 我的代码 学习:yyb luogu题目模板 xzy的模板 #include<iostream> #include<cstdlib> #include< ...
- CVE-2013-2094 porting to x86-32 分析
/* * linux 2.6.37-3.8.8 - x86 * @rikiji * * requires System.map and /dev/ptmx * this: http://zmbs.ne ...
- Solution for NULL pointer dereference
•mmap_min_addr forbids users from mapping low addresses 1. First available in July 2007 2. Several c ...
- linq函数All,Any,Aggregate说明
int[] arrInt; arrInt = ,,,,,,}; );// 所有元素都满足条件,false );// 有任一元素满足条件,true , , , , , , , , }; var quer ...
- shell脚本-巡检内存使用率
#!/bin/bash # by dreamer Q # 巡检内存脚本 #总内存大小 mem_total=`free -m | sed -n '2p' |awk '{print $2}'` #已使用内 ...