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 ...
随机推荐
- JS 创建动态表格练习
创建动态表格 1.1 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- spring容器、spring MVC容器以及web容器的区别
本文转载自 https://www.cnblogs.com/xiexin2015/p/9023239.html 说到spring和springmvc,其实有很多工作好多年的人也分不清他们有什么区别,如 ...
- [COCI2017.1]Deda —— 解锁线段树的新玩法
众所周知,能用线段树做的题一定可以暴力 但考场上也只能想到暴力了,毕竟还是对线段树不熟练. deda 描述 有一辆车上有n个小孩,年龄为1~n,然后q个询问,M X A代表在第X站时年龄为A的小孩会下 ...
- MySQL5.7的搭建以及SSL证书
Centos7 安装MySQL 5.7 (通用二进制包) 1.1 下载软件包 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-l ...
- JavaScript高级程序设计(第3版) 第三章 (基本概念)
3.1 语法 1.不以数字开头的数字,字母,下划线,美元符号 2.注释:html <!-- --> css/**/ js单行// 多行/**/ 3.ES5 引入了严格模式(strict m ...
- [Linux] 022 RPM 包查询
1. 查询是否安装 (1) 查询包是否安装 $ rpm -q 包名 选项 释义 -q (query) 查询 (2) 查询所有已安装的 RPM 包 $ rpm -qa 选项 释义 -a (all) 所有 ...
- MYSQL学习笔记/2019
以下内容均为转载,只是方便今后学习:如有不妥,请联系删除:特此感谢原博主精心制作!(文章底部贴有原文链接) /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ m ...
- [Bzoj1047][HAOI2007]理想的正方形(ST表)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1047 题目虽然有一个n的限制,但求二维区间最值首先想到的还是RMQ,但是如果按照往常RM ...
- #python# error:urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
设置代理后访问网页报错,百度有人说地址拼写不对,确认拼写后依然报错 因为使用的是xici免费代理,想到可能代理不可用造成getaddrinfo failed, 更换其他代理,error消失
- python学习第十五天字典的创建及增删改查操作方法
字典是python比较常见的数据类型,跟列表一样,比如的字典的创建,字典的常见的操作的方法,增加,删除,修改,查找等方法,字典的一共的数据方法为 keys() values() fromkeys() ...