N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的

Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output
6
5

 # include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int sum , num;
int a[] ;
bool v[] ;
int len , n; bool cmp(const int &x , const int &y)
{
return x > y ;
} bool dfs(int count , int L , int pos) //已完成的数量 当前木棒长度 位置
{
if (len == sum)
return ;
if (count == num)
return ;
for (int i = pos ; i < n ; i++)
{
if (v[i])
continue ;
if (L + a[i] == len)
{
v[i] = ;
if (dfs(count+ , , ))
return ;
v[i] = ;
return ;
}
else if (L + a[i] < len)
{
v[i] = ;
if (dfs(count , L+a[i] , i+))
return ;
v[i] = ;
if (L == ) //说明有一根木棒没有派上用场
return ;
while (a[i] == a[i+]) //如果下一根的长度和这根一样 则继续搜索下面的
i++ ;
}
}
return ;
} int main()
{
//freopen("in.txt","r",stdin) ;
while (scanf("%d" , &n) , n)
{
sum = ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d" , &a[i]) ;
sum += a[i] ;
}
sort(a,a+n,cmp) ;
if (a[] > sum - a[]) //如过第1根木棒比剩下的木棒和 还要长
{
printf("%d\n" , sum) ;
continue ;
}
for (len = a[] ; len <= sum ; len++) //一根完整木棒的长度肯定大于最长的小木棒
{
if (sum % len)
continue ;
memset(v , , sizeof(v)) ;
num = sum/len ;
if (dfs(,,))
{
printf("%d\n" , len) ;
break ;
}
}
}
return ; }

hdu 1518 N根木棒 能否拼成正方形  

Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Sample Output
yes
no
yes

 # include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int sum ;
int a[] ;
bool v[] ;
int len , n; bool cmp(const int &x , const int &y)
{
return x > y ;
} bool dfs(int count , int L , int pos) //已完成的数量 当前木棒长度 位置
{ if (count == )
return ;
for (int i = pos ; i < n ; i++)
{
if (v[i])
continue ;
if (L + a[i] == len)
{
v[i] = ;
if (dfs(count+ , , ))
return ;
v[i] = ;
return ;
}
else if (L + a[i] < len)
{
v[i] = ;
if (dfs(count , L+a[i] , i+))
return ;
v[i] = ;
if (L == ) //说明有一根木棒没有派上用场
return ;
while (a[i] == a[i+]) //如果下一根的长度和这根一样 则继续搜索下面的
i++ ;
}
}
return ;
} int main()
{
// freopen("in.txt","r"a,stdin) ;
int T ;
scanf("%d" , &T) ;
while (T--)
{
scanf("%d" , &n) ;
sum = ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d" , &a[i]) ;
sum += a[i] ;
}
if (sum%)
{
printf("no\n") ;
continue ;
}
sort(a,a+n,cmp) ;
len = sum / ;
if (a[] > len)
{
printf("no\n") ;
continue ;
}
memset(v,,sizeof(v)) ;
if (dfs(,,))
{
printf("yes\n") ;
}
else
printf("no\n") ; }
return ; }

hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)的更多相关文章

  1. hdu 1455 Sticks

    Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

  4. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

  5. hdu 1455(DFS+好题+经典)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...

  7. Sticks HDU - 1455 (未完成)

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  8. HDU 1455 Sticks(经典剪枝)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. Hdu 1455

    #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...

随机推荐

  1. Hbase记录-HBaseAdmin类

    HBaseAdmin是一个类表示管理.这个类属于org.apache.hadoop.hbase.client包.使用这个类,可以执行管理员任务.使用Connection.getAdmin()方法来获取 ...

  2. MySQL 获得当前日期时间 函数【转】

    获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | now() | +---------- ...

  3. Jena搭建SPARQL查询RDF数据

    1 Jena搭建SPARQL查询RDF数据 1.1 Jena概要 · SPARQL是W3C的RDF数据工作组设计的一种查询语言和协议,用于RDF数据的查询.经过类似于JDK安装时候的配置,可以在命令行 ...

  4. C#复杂类型序列化

    [Serializable] public class CardItemInfo { private int lineWidth;//线宽 private CardItemInfo childCard ...

  5. 使用ResourceBundle读取配置文件

    在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似: 12 #注释语句 some_key=some_value 形式.以#开头的行作为注释行 ...

  6. 七、UART

    7.1 介绍 UART(Universal Asynchronous Receiver Transmitter),通用异步收发器,用来传输穿行数据时 UART 之间以全双工方式传输数据,连线方法只有 ...

  7. 20155231 2016-2017-2 《Java程序设计》第6周学习总结

    20155231 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 学习目标 理解流与IO 理解InputStream/OutPutStream的继承架构 理解 ...

  8. java 多线程断点下载功能

    import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.Rand ...

  9. Python中crypto模块进行AES加密和解密

    #coding: utf8 import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class p ...

  10. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...