zoukankan      html  css  js  c++  java
  • POJ 1011 Sticks

    这个问题是做哭= =
    之前我们把所有变量定义为全局变量,结果总是提交的时间RT,我无法找到故障。后来改为局部变量ac该= =。

    D - Sticks(3.4.2)
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    Description

    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.

    Input

    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.

    Output

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

    Sample Input

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

    Sample Output

    6
    5

    题目大意:

    任意分割木棒,分割后想把这些碎木棒还原到原来状态,可是他忘记了原先有多少木棒以及那些木棒原来有多长。请计算原先木棒的最小可能长度。



    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int n,num[69];
    int pd[69];
    int cmp(int a,int b)
    {
    	return a>b;
    }
    int dfs(int len,int s,int nu)
    {
    	int i;
        if(s==0&&nu==0)//假设剩下的长度和剩下的木棒数为0。则成功
    		return len;
        if(s==0)//假设剩下的长度为0,则又一次赋值
    		s=len;
        for(i=0;i<n;i++)
        {
            if(pd[i]==1)
    			continue;
            if(s>=num[i])
            {
                pd[i]=1;
                if(dfs(len,s-num[i],nu-1))
    				return  len;	
    			pd[i]=0;//假设不成功,则当前棒不使用
                if(num[i]==s||s==len)
    				break;
                while(num[i]==num[i+1])
    				i++;			
            }
        }
    	return 0;
    }
    int main()
    {
        while(cin>>n&&n)
        {
    		int i;
    		int sum;
            sum=0;
    		int len,k;
    		for(i=0;i<n;i++)
    		{
    			cin>>num[i];
    			sum=sum+num[i];
    		}
    		sort(num,num+n,cmp);
    		for(len=num[0];len<=sum;len++)
    		{
    			memset(pd,0,sizeof(pd));
    			if(sum%len==0)
    			{
    				k=dfs(len,0,n);
    				if(k)
    					break;
    			}
    		}
    		cout<<k<<endl;
        }
        return 0;
    }
    /*
    9
    5 2 1 5 2 1 5 2 1
    4
    1 2 3 4
    0
    */



  • 相关阅读:
    Java内存回收
    Android四大基本组件介绍与生命周期
    JAVA中获取当前系统时间
    jquery如何判断元素是否被点击_百度知道
    css控制div显示/隐藏方法及2种方法比较原码
    点击图标不断震动效果
    jquery 如何动态添加、删除class样式方法介绍_jquery_脚本之家
    HTML5绘制矩形和圆形并且还有获取在这个图层内的坐标的思路和代码
    Attribute name invalid for tag form according to TLD异常解决办法_gaigai_百度空间
    html5绘图
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5015017.html
Copyright © 2011-2022 走看看