二分-跳石头

题目:跳石头

链接:二分 - Virtual Judge (vjudge.net)

题意:选m个石头搬走,求最短跳跃距离的最大值(起点和终点都有一个石头且不搬走)

思路:用二分答案求解

实现步骤:

1.用check检查mid(判断条件)

2.写主函数

注意:不要忘了首未无石头这一情况

代码:#include <iostream>
using namespace std;
const int N=50010;
int a[N],n,len,m,mina=1e9+1;
int check(int mid){//检查是否最短距离为mid。
    int cnt=0;
    int i=0,now=0;//ii表示目标位置,now表示当前位置
    while(i<n+1){
        i++;
        if(a[i]-a[now]<mid){
            cnt++;
        }
        else{
            now=i;//符合,跳过去
        }
    }
    if(cnt<=m) return 1;//移走的室友数目小于m,就保证了任意两剩下的石头间距大于等于最短距离mid.
    return 0;
    
}
int main(){
    cin>>len>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]<mina) mina=a[i];
    }
    a[0]=0,a[n+1]=len;//首未都有石头
    if(n==0){//如果中间无石头
        cout<<len; return 0;
    }
    long long l=1,r=1e10;
    while(l<r){//模板二,求做大值
        int mid=l+r+1>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个C语言实现的石头剪刀布小游戏,可以控制胜率的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int play, computer, winCount = 0, loseCount = 0, drawCount = 0; char choice; srand(time(NULL)); do { printf("请选择:\n"); printf("1. 石头\n"); printf("2. 剪刀\n"); printf("3. 布\n"); printf("0. 退出\n"); scanf("%d", &play); if (play == 0) { break; } computer = rand() % 3 + 1; printf("你选择了:"); switch (play) { case 1: printf("石头\n"); break; case 2: printf("剪刀\n"); break; case 3: printf("布\n"); break; default: printf("无效选择\n"); continue; } printf("电脑选择了:"); switch (computer) { case 1: printf("石头\n"); break; case 2: printf("剪刀\n"); break; case 3: printf("布\n"); break; } if (play == computer) { printf("平局!\n"); drawCount++; } else if ((play == 1 && computer == 2) || (play == 2 && computer == 3) || (play == 3 && computer == 1)) { printf("你赢了!\n"); winCount++; } else { printf("你输了!\n"); loseCount++; } printf("胜利次数:%d,失败次数:%d,平局次数:%d\n", winCount, loseCount, drawCount); printf("是否继续游戏?(y/n) "); scanf(" %c", &choice); } while (choice == 'y' || choice == 'Y'); return 0; } ``` 这个程序使用了随机数生成电脑的选择,然后根据玩家和电脑的选择进行比较,判断胜负或平局,并统计胜利、失败和平局的次数。你可以通过多次运行程序来控制胜率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值