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的更多相关文章

  1. nyoj 546——Divideing Jewels——————【dp、多重背包板子题】

    Divideing Jewels 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Mary and Rose own a collection of jewells. ...

  2. 河南省第五届ACM程序设计大赛

    D:   遥 控 器 #include<cstdio> #include<cstring> #include<iostream> #include<algor ...

  3. hdu.1044.Collect More Jewels(bfs + 状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. hdu 1044 Collect More Jewels(bfs+状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  7. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  8. 771. Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  9. [Swift]LeetCode771. 宝石与石头 | Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

随机推荐

  1. applicationContext.xml无错有红叉,Error occured processing XML 'Provider org.apache.xerces.parsers.解决方案

    applicationContext.xml无错有红叉,网上讲的取消xml验证的方法没用... 甚至我的myeclipse10连windows-->perferences-->myecli ...

  2. WEB开发:Java与Php对比

    比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较PHP和Java的Web开发.以下是我就几个主要方面进行比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上 ...

  3. Buy Tickets 【POJ - 2828】【线段树】

    题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而 ...

  4. TensorFlow学习笔记3-从MNIST开始

    TensorFlow学习笔记3-从MNIST开始学习softmax 本笔记内容为"从MNIST学习softmax regression算法的实现". 注意:由于我学习机器学习及之前 ...

  5. 编程语言-Python-GUI

    PyQt5 import sys from PyQt5 import QtWidgets,QtCore app = QtWidgets.QApplication(sys.argv) widget = ...

  6. Winsows10-1909正式版原版下载资料

    [简体中文版] 一.win10 1909消费者版(零售版),含家庭版.家庭单语言版.教育版.专业版.专业教育版.专业工作站版 (6个版本) 1.64位系统:Windows 10 (consumer e ...

  7. JSP基础--九大内置对象

    JSP九大内置对象 Object findAttribute(String name):依次在page.request.session.application范围查找名称为name的数据,如果找到就停 ...

  8. Java语言的特点与工作原理

    Java语言的特点 1.简单性 Java语言与我们常听到的C++语言很像,但是没有C++那么繁琐.因为Java就是在C++之上设计出来的,设计者把C++的一些特性去掉了,这些特性在实际开发中,程序员也 ...

  9. 浏览器调起摄像头(jquery+layui)

    /* 实例化camvas配置参数 config = { video:{width:Number(scale*4),height:Number(scale*3)},//视频比例4:3 canvasId: ...

  10. [NOIP2016PJ]魔法阵

    今天模拟赛的题,,,唯一没有Giao出来的题(不然我就AKIOI了~) 最开始没想到数学题,把所有部分分都说一遍吧: 35分:纯暴力O(M^4)枚举,对于每一组a,b,c,d验证其是否合法. 60分: ...