Computer Science, asked by gaglu3941, 9 months ago

Bob and Alice play a game in which Bob gives Alice a challenge to think of any number Mbetween 1 to N. Bob then tells Alice a number X. Alice has to confirm whether X is greater orsmaller than number M or equal to number M. This continues till Bob finds the numbercorrectlyYour task is to find the maximum number of attempts Bob needs to guess the number thoughtof by Alice....code in java6​

Answers

Answered by sidhant014
3

Answer:

hey mate it would be better if you search the answer from google I hope you will solve your problem very quickly

Answered by aryansuts01
0

Answer:

Concept:

In order to have as few implementation dependencies as feasible, Java is a high-level, class-based, object-oriented programming language. Because Java is a general-purpose programming language, compiled Java code can run on all platforms that accept Java without the need to recompile. This is what is meant by the phrase "write once, run anywhere." Regardless of the underlying computer architecture, Java applications are often compiled to bytecode that can run on any Java virtual machine (JVM). Although Java has fewer low-level features than either C or C++, it has syntax that is similar to each of them.

Given:

In a game that Bob and Alice play, Bob challenges Alice to come up with any number M between 1 and N. Then Bob gives Alice the number X. Whether X is bigger, less, or equal to number M must be verified by Alice. This repeats until Bob correctly determines the number. Your goal is to determine the most attempts possible. Bob needs to guess the number that Alice came up with.

Find:

write the code for the given question

Answer:

The game begins with an integer,, which is used to create a collection of unique integers in the range from to (i.e., ). The two players alternate turns while Alice always has the first turn. The current player selects a prime number from on each move. After that, the player takes away and all of its multiples from. The game is won by the first person who is unable to move. Bob and Alice have fun together. Print the name of the game's winner on a new line using the value for each game. Print Alice if she triumphs; print Bob if she loses.

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       ArrayList<Integer> primes = new ArrayList<Integer>();

       primes.add(2);

       for (int i = 3; i <= 100000; i+=2) {

           int sqrt = (int)Math.sqrt(i);

           boolean prime = true;

           for (int p : primes) {

               if (p > sqrt)

                   break;

               if (i%p==0) {

                   prime = false;

                   break;

               }

           }

            if (prime)

               primes.add(i);

       }

       int[] counts = new int[100001];

       int currIndex = 0;

       for (int i = 0; i <= 100000; i++) {

           if (currIndex < primes.size() && primes.get(currIndex) == i)

               currIndex++;

           counts[i] = currIndex;

       }

       int g = sc.nextInt();

       for (int i = 0; i < g; i++) {

           System.out.println(counts[sc.nextInt()]%2==0?"Bob":"Alice");

       }

   }

}

#SPJ2

Similar questions