试题 算法训练 Sticks

Sticks

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 113547 Accepted: 26078

问题描述

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

输入格式

The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.

输出格式

The output should contains the smallest possible length of original sticks, one per line.

样例输入

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

样例输出

6
5
首先 ,贴上超级大佬的此题代码网站大佬大佬大佬·


#include<bits/stdc++.h> 
using namespace std; 
int f=0,m,a[100],ss=0,l=0,len=0,s=0,visited[100]; 
int cmp(int x,int y) 
{ 
	return x>y; 
} 
int dfs(int nowlen,int nowget,int cnt) 
{ 
	if(cnt>=m) return 0; 
	if(nowget==l) return 1; 
	for(int i=cnt;i<m;i++) 
	{ 
		if(visited[i]==0) 
		{ 
			if(nowlen+a[i]==len) 
			{ 
//				cout<<"p ";
				visited[i]=1; 
				if(dfs(0,nowget+1,nowget)==1) 
					return 1; 
//				cout<<"q ";
				visited[i]=0; 
				return 0; 
			} 
			else if(nowlen+a[i]<len)  
			{ 
				visited[i]=1; 
//				cout<<"r ";
				if(dfs(nowlen+a[i],nowget,i+1)==1) 
					return 1; 
//					cout<<"f "; 
				visited[i]=0; 
				if(nowlen==0)
					return 0;
				for(;a[i]==a[i+1]&&i+1<m;i++) cout<<"l ";
			} 
		} 
	}  return 0;
} 
int main() 
{ 
	while(cin>>m) 
	{ 
		memset(visited,0,sizeof(visited));
		int i=0; 
		s=0;
		if(!m) 
			break; 
		for( i=0;i<m;i++) 
		{ 
			cin>>a[i];             
			s+=a[i]; 
		} 
		sort(a,a+m,cmp); 
		for(len=a[0];len<=s;len++) 
		{ 
			if(s%len!=0) 
				continue; 
			l=s/len;//共l棵  
			if(dfs(0,0,0)==1) 
				break; 
		} 
		cout<<len<<endl; 
	} 
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱贝贝呗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值