• poj 1011 sticks 解题。


    看了那么多代码

    这个算是比较简单而且算是最好的了。。。。

     1 #include<iostream>
     2 using namespace std;
     3 int finish;
     4 int length;
     5 int in[50];
     6 //count:棒子的总数(如果为0则结束)
     7 //len  目前检察棒子长度的剩余量
     8 //plen 现在检察的片段长度
     9 void dfs(int count,int len,int plen)
    10 {
    11     --in[plen];
    12     if(count==0)
    13     {
    14         finish=1;
    15     }
    16     if(!finish)
    17     {
    18         len-=plen;
    19         if(len!=0)
    20         {
    21             int next_plen = min(len,plen);
    22             for(;next_plen>0;--next_plen)
    23             {
    24                 if(in[next_plen]) 
    25                     dfs(count,len,next_plen);
    26             }
    27         }
    28         else
    29         {
    30             int max=50;
    31             while(!in[max])
    32                 --max;
    33             dfs(count-1,length,max);
    34         }
    35     }
    36     ++in[plen];
    37 }
    38 int main()
    39 {    
    40     int n;
    41     while(~scanf("%d",&n) && n)
    42     {
    43         memset(in,0,sizeof(in));
    44         int sum=0;
    45         int max=0;
    46         finish=0;
    47         while(n--)
    48         {
    49             int b;
    50             scanf("%d",&b);
    51             in[b]++;
    52             sum+=b;
    53             if(max<b)
    54             {
    55                 max=b;
    56             }
    57         }
    58         length=max;
    59         while(true)
    60         {
    61             if(sum%length==0)
    62             {
    63                 dfs(sum/length,length,max);
    64             }
    65             if(finish)
    66             {
    67                 break;
    68             }
    69             length++;
    70         
    71         }
    72         cout<<length<<endl;
    73     }
    74 
    75 }

    原文网址:http://www.hankcs.com/program/algorithm/poj-1011-sticks.html

  • 相关阅读:
    Android随笔
    Android随笔
    阅读笔记
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Java随笔
    Android随笔
    NC20265 着色方案(dp)
  • 原文地址:https://www.cnblogs.com/cagercoding/p/6833325.html
Copyright © 2020-2023  润新知