Sticks --UVA - 307

Sticks

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 

5 2 1 5 2 1 5 2 1 

1 2 3 4 

Sample Output 

5

题意: 给出一定数量的小木棒的长度,它是由等长的若干木棒随意砍断所得到的。对于给定的一组小木棒,请求出原始木棒的最小长度。即拼接成若干个等长的木棍,求拼接木棍最短的长度。

思路:拼接成木棍,那么原来的木棍长度是多少?我们可以进行枚举原来木棍的长度,然后判断是否题意,那么枚举的时候难道是从1开始每个数都要尝试一边吗?当然不是的:既然是拼接成等长的,那么枚举第一个数的时候,就应该是一组小木棍中最长的一个,并判断一组小木棍的总长度是否是枚举数的倍数(这个条件的剪枝,可以减少很多时间),枚举必须要有一个上限,上限一定是所有木棍的总和。如果sum/2已经测试过不合适,(sum/2,sum)这个区间里面的数就不必要尝试了,因为sum不可能是其的倍数。我们尝试着连接的时候 ,从较短的木棍连接,还是从较长的木棍连接?,很明显,应该从较长的木棍开始连接,原因:长的的木棍,需要连接的木棍可能较少,选择的范围比较小,复杂度较低。

第一次:搜原始木棒的长度时,sum必须是它的倍数。

第二次:如果一个木棒与前一个木棒长度相等,并且前一个已经尝试拼接过,而且没有拼接成功(book[i-1]==0),那么这个木棍就没必要再进行尝试拼接,可以减掉。

第三次:当前拼接的长度为0时,即从0开始拼接另一根木棒,但是有一个木棍可以拼接,但是尝试所有的木棍都没有拼接成功,即可证明尝试的长度不符合题意,可直接 return 0;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,sum,m,k;       //n木棒数量,m为原始的木长 ,k为原始木长
int a[100];          //sum为木棒总长度。
int book[100];
int cmp(int a,int b)
{
    return a>b;
}

int dfs(int len,int s,int j)//len此前连接木棍的长度
{
    //j表示从j点开始向下搜。
    //s为连接好木棍的数量,
    if(len==m)
    {
        s++;
        j=0;
        len=0;
    }
    if(s==k)
        return 1;
    for(int i=j+1; i<n; i++)
    {
        if(book[i]||a[i]==a[i-1]&&!book[i-1]&&i>0)
            continue;
        if(a[i]+len>m)
            continue;
        book[i]=1;
        if(dfs(len+a[i],s,i))
            return 1;
        book[i]=0;
        if(len==0)     //后面的不符合题意,则前面连接好的木棒需要拆分。
            return 0;
    }
    return 0;
}

int main()
{
    while(~scanf("%d",&n),n)
    {
        sum=0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        sort(a,a+n,cmp);
        for(m= a[0]; m <= sum/2; m++)
        {
            memset(book,0,sizeof(book));
            if(sum % m != 0)
                continue ;
            k=sum/m;
            book[0]=1;
            if(dfs(a[0],0,0))
            {
                printf("%d\n",m);
                break;
            }
        }
        if(m>sum/2)
            printf("%d\n",sum);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较经典的小游戏,我可以用C语言帮您实现。以下是代码实现: ```c #include <stdio.h> int main() { int sticks = 21; // 初始火柴数 int take, computerTake; // 玩家和计算机取的火柴数 printf("Game begin:\n"); while (sticks > 0) { printf("%d sticks left in the pile.\n", sticks); printf("How many sticks do you wish to take (1~4)? "); scanf("%d", &take); while (take < 1 || take > 4 || take > sticks) { printf("Illegal move. Please try again.\n"); printf("How many sticks do you wish to take (1~4)? "); scanf("%d", &take); } sticks -= take; if (sticks == 0) { printf("%d sticks left in the pile.\n", sticks); printf("You have taken the last sticks.\n"); printf("***You lose!\nGame Over.\n"); break; } computerTake = (5 - take) % 5; printf("%d sticks left in the pile.\n", sticks); printf("Computer take %d sticks.\n", computerTake); sticks -= computerTake; if (sticks == 0) { printf("%d sticks left in the pile.\n", sticks); printf("Computer has taken the last sticks.\n"); printf("***You win!\nGame Over.\n"); break; } } return 0; } ``` 程序中,我们使用了一个 while 循环,不断进行玩家和计算机的取火柴操作,直到火柴数量为 0。每次玩家取火柴前,都会判断其取的火柴数量是否合法,不合法则要求重新输入。而计算机则根据玩家取的火柴数量,采用一定的策略来取火柴。这里,我们使用了一个简单的策略:让计算机取的火柴数量加上玩家取的火柴数量等于 5。这样,计算机就可以在保证不输的前提下,尽可能地拖延游戏进程,增加玩家输的可能性。 运行程序后,可以按照提示进行游戏。如果您想让计算机更聪明一些,可以尝试采用其他的策略,比如根据当前剩余的火柴数和玩家的取火柴数量,来决定计算机应该取多少火柴。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值