[如何解决C ++中各种竞争性编程挑战中所要求的交互式问题

问题描述 投票:0回答:1

[存在许多竞争性的编程挑战,例如Google的代码冲突,并且那里可能存在一些交互问题,因此,如何解决这些问题就成为问题。

c++ interactive
1个回答
0
投票

让我向您解释如何使用C ++代码示例解决交互式问题,

这里是基本的代码交互问题:https://codeforces.com/gym/101021/problem/1

这是我对这个问题的可接受的解决方案,通过它您将很快了解发生了什么,基本上是使用二进制搜索:

#include<bits/stdc++.h>
using namespace std;

#define ll long long

int main()
{
    string s;
    ll first = 1, last = 1000000, mid;

    while(last!=first)
    {
        mid = (last + first +1)/2;

        cout << mid;
        cout << endl;
        char response[3];

        scanf("%s", response);

        if (strcmp(response, "<") == 0)
            last = mid - 1;

        else
            first = mid;

    } 

    cout << "! " << last;
    cout << endl;


}

要注意的重要一点是我使用了endl,这是因为int大多数编程语言都将输出到屏幕上是一项昂贵的操作,因此语言通常会不断收集输出以一次性打印输出,但这为避免int交互问题,因此我们使用endl代替“ / n”,这更省时。

© www.soinside.com 2019 - 2024. All rights reserved.