Cod sursa(job #3317097)

Utilizator winemomComan Erin winemom Data 22 octombrie 2025 10:37:53
Problema Cbinteractiv Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <stdlib.h>
//#include <cstdlib>
//#include <ctime>
using namespace std;

int caut_binar(int st, int dr)
{
    return (st + dr) / 2;
}

int main()
{
    int n, k;
    cin>>n;

    int st = 1, dr = n;
    int mij = caut_binar(st, dr);
    bool ghicit = false;
    int raspuns;

    while(!ghicit)
    {
        system("CLS");
        cout <<"? " << mij << '\n';
        cin >> raspuns;
        if (raspuns == 0) {
            st = mij + 1;
        } else if (raspuns == 1) {
            dr = mij;
        } else {cout << "Nu-i bun inputul. Du-te acasa!";return 0;}
        if(st == dr)
            ghicit = true;
        else
            mij = caut_binar(st, dr);
    }

    system("CLS");
    cout << "! " << dr;
}