交互式问题是我们的解决方案或代码与法官实时交互的那些问题。当我们为交互问题开发解决方案时,提供给我们解决方案的输入数据可能不是预先确定的,而是专门为该问题构建的。解决方案与法官进行一系列数据交换,在对话结束时,法官决定我们的解决方案是否正确。
猜数字(一个互动问题)
在这个问题中,用户必须在与法官交流期间猜测数字。用户有上限和下限,他/她可以询问判断一个数字是否是要猜的数字。如果数字小于要猜的数字,则法官回复-1,如果数字大于要猜的数字,则为1,如果等于要猜的数字,则为0。
方法一:线性猜测
用户可以查询下限和上限之间的所有数字的判断来找到解决方案。
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)
算法范式:分而治之
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。