Sticks

Sticks

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 5
描述
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.
输入
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.
输出
The output should contains the smallest possible length of original sticks, one per line.
样例输入
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
样例输出
6

5


大清早写博客呀,但手痒没办法

这题的意思就是给你很多木棒,要请把木棒合并在一块。然后使得合并的木棒的长度最短,并且要求木棒的长度相同。eg:1 2 3 4   (1,4)和(2,3) ,这一看就是一个经典的搜索+剪枝 题目。

设总长度为sum,每个的长度为l,一共的数目num=sum/l:

那么l大于最大长度从而就可以从最大长度开始搜索。遇见相同的跳过,没必要在去做了。

代码就一个回溯;


代码:


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<cassert>
using namespace std;
//#define LL __int64

#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif

int a[100];
int n;
int l;
int num;
bool vis[100];
int cmp(int a,int b) {
    return a>b;
}
bool dfs(int s,int le,int pos) {
    int ok;
    if(le==0) ok=0;
    else ok =1;
    if(s==num) return true;
    for(int i=pos+1; i<n; i++) {
        if(vis[i]) continue;
        if(le+a[i]==l) {
            vis[i]=1;
            if(dfs(s+1,0,-1)) return true;
            vis[i]=0;
            return false;
        }
        if(le+a[i]<l) {
            vis[i]=1;
            if(dfs(s,le+a[i],i)) return true;
            vis[i]=0;
            if(ok==0) break;
            while(a[i]==a[i+1]) i++;
        }
    }
    return false;
}

int main() {
    while(cin>>n) {
        if(n==0) break;
        int sum=0;
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a,a+n,cmp);

        for(l=a[0]; l<=sum; l++) {
            memset(vis,0,sizeof(vis));
            if(sum%l==0) {
                num=sum/l;
                if(dfs(1,0,-1)) {
                    printf("%d\n",l);
                    break;
                }
            }
        }
    }
    return 0;
}


/*

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4

*/




好的,这是一个比较经典的小游戏,我可以用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、付费专栏及课程。

余额充值