zoukankan      html  css  js  c++  java
  • 交互题

    https://vjudge.net/contest/283149#problem/I 专题

    Subway Pursuit

    题意:猜火车的位置,

    每次区间缩小到一定程度就可以用rand函数猜一个数字,猜对了就结束,猜错了之后,在这个区间火车可能会向左走K,向右走K。4k是一个人为订的范围,指区间变得这么大的时候就才一个数。大一点也行。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    typedef long long ll;
    
    ll n, k;
    char s[10];
    
    int main() {
        srand(time(0));
        scanf("%lld%lld", &n, &k);
        ll l = 1, r = n;
        while(1){
            if(r - l <= (ll)4 * k){
                ll x = rand() % (r - l + 1) + l;
                printf("%lld %lld
    ", x, x);
                fflush(stdout);
                scanf("%s", s);
                if(s[0] == 'Y' || s[0] == 'B') return 0;
                else{
                    l = max(1ll, l - k);
                    r = min(n, r + k);
                }
            }
            else{
                ll mid = (l + r) / 2;
                printf("%lld %lld
    ", l, mid);
                fflush(stdout);
                scanf("%s", s);
                if(s[0] == 'B') return 0;
                else if(s[0] == 'Y'){
                    l = max(1ll, l - k);
                    r = min(n, mid + k);
                }
                else{
                    l = max(1ll, mid + 1 - k);
                    r = min(n, r + k);
                }
            }
        }
        return 0;
    }

     The hat

     尽量还是用cout

    相邻的两个人相差1或-1(圆的), 求那个人与对面那个人的树枝相等。必须是4K个人才有可能相等。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int maxm = 1e5 + 5;
    typedef long long ll;
    int  n;
    int fun(int x) {
    int l ,r;
    cout << "? " << x <<endl;
    cin >> l;
    cout << "? " << x + n / 2 << endl;
    cin >> r;
    if(r - l == 0) {
    cout << "! " << x <<endl;
        exit(0);
    }
    return r - l;
    }
    
    
    int main() {
    cin >> n;
    if(n % 4) {
    cout << "! -1" << endl;
        return 0;
    }
    int l = 1, r = n / 2, mid;
    while(l <= r) {
        mid = (l + r) / 2;
        ll vl = fun(l);
        ll ml = fun(mid);
        if(vl * ml < 0) {
            r = mid;
        }
        else {
            l = mid + 1;
        }
    }
    cout << "! -1" << endl;
    return 0;
    }
  • 相关阅读:
    iOS 有用的代码片段
    iOS 限制软盘输入法
    UIlabel 遇到\n 换行iOS
    关于delegate 与 protocol 的理解 iOS
    ios 跳转到app store
    iOS 上下左右滑动手势
    求某段程序运行的高精度时间
    转载——GDB中应该知道的几个调试方法
    文章翻译——使用 GNU 的 GDB调试器,内存布局和栈——01
    第十章扩展——__cdecl 的压栈方式——printf
  • 原文地址:https://www.cnblogs.com/downrainsun/p/10371978.html
Copyright © 2011-2022 走看看