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. POJ 2796[UVA 1619] Feel Good

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16786   Accepted: 4627 Case T ...

  2. java基本数据类型,访问控制符,运算符执行顺序

    1.java数据类型 内置数据类型:boolean(1),  byte(8), char(16), short(8), int(32), long(64), float(32), double(64) ...

  3. python 代码模板

    命令[python3 -m pydoc -p 1234]   通过http://localhost:1234来访问查看文档 # -*- coding: utf-8 -*-""&qu ...

  4. springboot(六)SpringBoot问题汇总

    SpringBoot2.0整合Mybatis,取datetime数据类型字段出来时,发现少了8小时. 过程:mysql中注册时间查询出来结果是正确的,只是java程序运行出来后显示少了8小时.经前辈指 ...

  5. C# 窗体内有子控件时鼠标检测

    public partial class FormPop : Form { public FormPop() { InitializeComponent(); } private void FormP ...

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

    20155337 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 •认识时间与日期 •格林威治标准时间:简称GMT时间,参考格林威治皇家天文台的标准太阳时间. ...

  7. string字符串js操作

    String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...

  8. [C++]Linux之间隔时间内循环执行指定程序

    #include<time.h> #include<unistd.h>//usleep(num) #include<stdio.h> #include<std ...

  9. java先导课程学习总结

    经过两个星期四节课的java学习,我也对java这门语言有了一定的认识.刚开始上课的时候,我认为java把C语言中老师所说的模块化编程进行了强调,进行一个类,一个类的编程,在类中构造相应的方法,使用的 ...

  10. Database学习 - mysql 数据库 外键

    外键 外键约束子表的含义:如果在父表中赵达不到候选键,则不允许在子表上进行insert/update 外键预约对父表的含义:在父表上进行update/delete以更新或删除子表中有一条或多条对应匹配 ...