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. 同步GitHub上fork的项目

    最近在做“Python练习册,每天一个小程序”,fork了项目并贡献自己写的代码,项目还有其他人在贡献代码,每天都会更新,这就涉及到了自己fork的项目与原项目的同步更新问题,下面就是我最常用的方法. ...

  2. tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)

    phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...

  3. mybatis报错:org.apache.ibatis.builder.BuilderException:Encountered " "shr" "shr " " at line 1,column 1

    程序报错如下: 解决:变量名冲突 表字段‘审核人’简称为shr,与mybatis的OGNL表达式发生冲突. 解决方法:修改冲突的变量名即可. 总结了一下变量命名可能发生冲突的变量集合: bor(字符| ...

  4. java_第一年_JavaWeb(9)

    JavaBean是一个遵循某种特定写法的Java类,有以下特点: 必需具有一个无参的构造函数 属性必需私有化 私有化的属性必需通过public类型的方法暴露给其它程序,其方法命名也有一定的规范 范例: ...

  5. bfs(最短路径)

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  6. BZOJ 1040 [ZJOI2008]骑士 (基环树+树形DP)

    <题目链接> 题目大意: Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的 ...

  7. 【Vue 2.X】基于input[type='number']封装parseFloat、parseInt-自定义指令系列(一)

    一.parseFloat 效果:限制负值输入,且输入值不为空时自动保留两位小数,等同于js的parseFloat(value).toFixed(2) 使用:与v-model配合使用,v-parseFl ...

  8. .net core 简单集成JWT报No authenticationScheme was specified, and there was no DefaultChallengeScheme found错误

    #region JWT 认证 services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) //.AddCustomAuth( ...

  9. 【JAVA】 05-String类和JDK5

    链接: 笔记目录:毕向东Java基础视频教程-笔记 GitHub库:JavaBXD33 目录: <> <> 内容待整理: API-String 特点 String类: 1.St ...

  10. java ArrayList存储基本类型

    package java06; /* 如果希望像集合ArrayList中存储基本数据类型数据,必须使用基本数据类型对应的“包装类” 基本数据类型 包装类(引用类型,包装类都位于java.lang包下 ...