互动问题是指我们的解决方案或代码与法官实时互动的问题。当我们为交互式问题开发解决方案时,提供给我们解决方案的输入数据可能不是预先确定的,而是专门为该问题而构建的。该解决方案与法官进行一系列数据交换,并且在对话结束时,法官将决定我们的解决方案是否正确。
猜数字(一个交互式问题)
在这个问题中,用户必须在与法官沟通的过程中猜测数字。为用户提供上限和下限,他/她可以询问法官数字是否为要猜测的数字。如果数字小于要猜测的数字,则法官以-1答复,如果数字大于要猜测的数字,则法官以1答复,或者如果数字等于要猜测的数字,则法官以0答复。
方法1:线性猜测
用户可以查询法官在下限和上限之间的所有数字,以找到解决方案。
C++
#include
using namespace std;
int main()
{
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 6
// Iterating from lower_bound to upper_bound
for (int i = lower_bound; i <= upper_bound; i++) {
cout << i << endl;
// Input the response from the judge
int response;
cin >> response;
if (response == 0) {
cout << "Number guessed is :" << i;
break;
}
}
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner sc1 = new Scanner(System.in);
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 6
// Iterating from lower_bound to upper_bound
for (int i = lower_bound; i <= upper_bound; i++) {
System.out.println(i);
// Input the response from the judge
int response = sc1.nextInt();
if (response == 0) {
System.out.println("Number guessed is :" + i);
break;
}
}
}
}
Python3
if __name__=='__main__':
lower_bound = 2;
upper_bound = 10;
# Number to be guessed is 6
# Iterating from lower_bound to upper_bound
for i in range(lower_bound, upper_bound + 1):
print(i)
# Input the response from the judge
response = int(input())
if (response == 0):
print("Number guessed is :", i, end = '')
break;
# This code is contributed by rutvik_56
C#
using System;
class GFG
{
public static void Main(string[] args)
{
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 6
// Iterating from lower_bound to upper_bound
for (int i = lower_bound; i <= upper_bound; i++)
{
Console.WriteLine(i);
// Input the response from the judge
int response = int.Parse(Console.ReadLine());
if (response == 0) {
Console.WriteLine("Number guessed is :" + i);
break;
}
}
}
}
// This code is contributed by Pratham76
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner sc1 = new Scanner(System.in);
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 9
// Applying Binary Search interactively
while (lower_bound <= upper_bound) {
int mid = (lower_bound + upper_bound) / 2;
// Print the guessed number
System.out.println(mid);
// Input the response from the judge
int response = sc1.nextInt();
if (response == -1) {
lower_bound = mid + 1;
}
else if (response == 1) {
upper_bound = mid - 1;
}
else if (response == 0) {
System.out.println("Number guessed is :" + mid);
break;
}
}
}
}
C#
using System;
class GFG {
static void Main() {
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 9
// Applying Binary Search interactively
while (lower_bound <= upper_bound) {
int mid = (lower_bound + upper_bound) / 2;
// Print the guessed number
Console.WriteLine(mid);
// Input the response from the judge
int response = Convert.ToInt32(Console.ReadLine());
if (response == -1) {
lower_bound = mid + 1;
}
else if (response == 1) {
upper_bound = mid - 1;
}
else if (response == 0) {
Console.WriteLine("Number guessed is :" + mid);
break;
}
}
}
}
// This code is contributed by divyesh072019
时间复杂度: O(n)
方法2:应用二进制搜索
我们还可以交互式地应用二进制搜索来找到解决方案。与先前的方法相比,该解决方案是有效的。
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner sc1 = new Scanner(System.in);
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 9
// Applying Binary Search interactively
while (lower_bound <= upper_bound) {
int mid = (lower_bound + upper_bound) / 2;
// Print the guessed number
System.out.println(mid);
// Input the response from the judge
int response = sc1.nextInt();
if (response == -1) {
lower_bound = mid + 1;
}
else if (response == 1) {
upper_bound = mid - 1;
}
else if (response == 0) {
System.out.println("Number guessed is :" + mid);
break;
}
}
}
}
C#
using System;
class GFG {
static void Main() {
int lower_bound = 2;
int upper_bound = 10;
// Number to be guessed is 9
// Applying Binary Search interactively
while (lower_bound <= upper_bound) {
int mid = (lower_bound + upper_bound) / 2;
// Print the guessed number
Console.WriteLine(mid);
// Input the response from the judge
int response = Convert.ToInt32(Console.ReadLine());
if (response == -1) {
lower_bound = mid + 1;
}
else if (response == 1) {
upper_bound = mid - 1;
}
else if (response == 0) {
Console.WriteLine("Number guessed is :" + mid);
break;
}
}
}
}
// This code is contributed by divyesh072019
时间复杂度: O(logn)
算法范例:分而治之
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。