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 } 
  • 相关阅读:
    统一前后台数据交互格式
    volatile关键字 学习记录2
    利用AOP与ToStringBuilder简化日志记录
    JAVA的那些数据结构实现总结,实现,扩容说明
    JAVA中的数据结构
    对把JDK源码的一些注解,笔记
    分析下为什么spring 整合mybatis后为啥用不上session缓存
    JAVA内存关注总结,作为个程序员需要对自己系统的每块内存做到了如指掌
    写颗搜索二叉树动动脑,开启今年的旅程,新年快乐
    内存快照排查OOM,加密时错误方法指定provider方式错误引起的OOM
  • 原文地址:https://www.cnblogs.com/greenaway07/p/10500800.html
Copyright © 2011-2022 走看看