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

    Sticks
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 99807   Accepted: 22696

    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

    题解:第一行输入许多相同棍子被切割后的份数m,(m<=64)
    第二行输入被切割后的小棍子的长度。
    求诸多原始棍子中最小长度。
    这些长度均大于零。
    m=0表示输入结束。
     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))//因为dfs函数有返回值,使得目标长度成立,所以返回aimlen长度至主函数中的l.
    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 } 
  • 相关阅读:
    [LeetCode] #7 Reverse Integer
    [LeetCode] #6 ZigZag Conversion
    [LeetCode] #5 Longest Palindromic Substring
    [LeetCode] #4 Median of Two Sorted Arrays
    [LeetCode] #3 Longest Substring Without Repeating Characters
    关于tomcat的环境参数配置
    html里js的execCommand的一点用法
    在execCommand formatBlock 'p'标签里增加class或id或css style?
    关于tomcat setenv
    溢出文本显示省略号效果
  • 原文地址:https://www.cnblogs.com/greenaway07/p/10500800.html
Copyright © 2011-2022 走看看