zoukankan      html  css  js  c++  java
  • hdu 1455 sticks

    ..

    Sticks

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 30893    Accepted Submission(s): 4843


    Problem 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 file 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
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 int a[69];//被截断的棍子长度集 
     7 int vis[69]; //标记是否使用过该棍子
     8 int num;//被截断后的棍子数 
     9 
    10 bool cmp(int a,int b)
    11 {
    12     return a>b;//降序 
    13 }
    14 int dfs(int aimlen,int rest,int n)  
    15 {
    16     if(rest==0&&n==0)
    17         return aimlen;
    18     if(rest==0)
    19         rest=aimlen;
    20     for(int i=1;i<=num;i++)
    21     {
    22         if(vis[i])//如果这个数已经被检测过了,跳过 
    23             continue;
    24         if(rest>=a[i])
    25         {
    26             vis[i]=1;
    27             if(dfs(aimlen,rest-a[i],n-1))
    28                 return aimlen;
    29             vis[i]=0;
    30             if(a[i]==rest||aimlen==rest)
    31                 break;
    32             while(a[i]==a[i+1])//因为是降序排列的,如果这个数不满足,那么下一位和它相等的数肯定也不满足 
    33                 i++;
    34         } 
    35     }
    36     return 0; 
    37 }
    38 
    39 
    40 int main()
    41 {
    42     while(scanf("%d",&num)&&num)
    43     {
    44         int i=1,maxn=0,sum=0,l=0;
    45         for(i=1;i<=num;i++)
    46         {
    47             scanf("%d",&a[i]);
    48             sum=sum+a[i];
    49         }
    50         sort(a+1,a+num+1,cmp);
    51         
    52         for(i=a[1];i<=sum;i++)
    53         {
    54             if(sum%i==0)
    55             {
    56                 memset(vis,0,sizeof(vis));
    57                 l=dfs(i,0,num);
    58                 if(l)
    59                     break;
    60             }
    61         }
    62         printf("%d
    ",l);
    63      } 
    64     return 0;
    65 } 
    View Code
  • 相关阅读:
    spring学习10-AOP
    spring学习9-代理模式
    spring学习6-bean的自动装配
    PyQT5使用心得
    Python 时间戳和日期相互转换
    requests模块的入门使用
    Celery异步任务
    MySQL和python交互
    MySQL高级
    MySQL中select的使用
  • 原文地址:https://www.cnblogs.com/greenaway07/p/11198372.html
Copyright © 2011-2022 走看看