`
xiao_2008
  • 浏览: 21351 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java english interview--Data Structures

阅读更多
1.

Implement the body of the following function using a binary search of the array. You do not need to check the precondition.  

public static boolean has42(int[ ] data, int start, int end)

    // Precondition: The elements data[start]...data[end] are sorted from smallest

    // to largest. This array segment might be empty (indicated by end being less

    // than start).

    // Postcondition: A true return value indicates that the number 42 appears in

    // data[start]...data[end]. A false return value indicates that 42 doesn’t

    // appear.
  
 

Solutions:

 
public static boolean has42(int[ ] data, int start, int end)
         {
            int middle;
           
            if (end < start)
               // Empty array segment cannot contain 42.
               return false;
           
            middle = (start + end) / 2;
            else if (data[middle] == 42)
               // We found the number 42.
               return true;
            else if (data[middle] > 42)
               // Continue search of data[start]...data[middle-1].
               return has42(data, start, middle);
             else
               // Continue search of data[middle+1]...data[end].
               return has42(data, middle+1, end);
         }



2.Suppose that I have the following declarations:     int[ ] data = new int[100];
    int i;
Write a small segment of Java code that will shift data[50]...data[98] up one spot to the locations data[51]...data[99]. Then insert the number 42 at location data[50]. Use a for loop (not System.arraycopy).

Solutions:  
Note that the correct loop must run i backward:
        for (i = 99; i >= 51; i--)
            data[i] = data[i-1];
        data[50] = 42;



3.What are the steps to inserting a new item at the head of a linked list? Use one short English sentence for each step.


Solutions:  
Make a temporary IntNode variable called temp refer to a newly allocated node.  Fill in the data of the new node. Make the link field of the new node equal to the old head pointer. Make the head pointer refer to the new node.



4
The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where n is the number of bears that you have):
1.If n is even, then you may give back exactly n/2 bears.
2.If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many bears. (By the way, the last digit of n is n%10, and the next-to-last digit is ((n%100)/10).
3.If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game is to end up with EXACTLY 42 bears.
For example, suppose that you start with 250 bears. Then you could make these moves:
--Start with 250 bears.
--Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears.
--Since 208 is even, you may return half of the bears, leaving you with 104 bears.
--Since 104 is even, you may return half of the bears, leaving you with 52 bears.
--Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears.
--You have reached the goal!

Write a recursive method to meet this specification:
public static boolean bears(int n)


   // Postcondition: A true return value means that it is possible to win


   // the bear game by starting with n bears. A false return value means that


   // it is not possible to win the bear game by starting with n bears.


   // Examples:


   //   bear(250) is true (as shown above)


   //   bear(42) is true


   //   bear(84) is true


   //   bear(53) is false


   //   bear(41) is false


   // Hint: To test whether n is even, use the expression ((n % 2) == 0).


Solutions:

 
public static boolean bears(int n)
        {
           int ones, tens;

           if (n < 42) return false;
           if (n == 42) return true;
           if ((n%2) == 0)
              if (bears(n/2)) return true;
           if (((n%3)==0) || ((n%4)==0))
           {
              ones = n % 10;
              tens = (n % 100)/10;
              if ((ones != 0) && (tens != 0) && (bears(n-ones*tens)))
                 return true;
           }
           if ((n%5) == 0)
              if (bears(n-42)) return true;
          return false;
        }
分享到:
评论

相关推荐

    Algorithm-Problem-Solving-with-Algorithms-and-Data-Structures-using-Python.zip

    Algorithm-Problem-Solving-with-Algorithms-and-Data-Structures-using-Python.zip,使用python的算法和数据结构解决问题的代码,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Play-with-Data-Structures-master.zip

    《Play-with-Data-Structures-master》是一个涵盖了数据结构基础到高级应用的全面教程,包含1-12章的源代码实现。这个课程是学习和深入理解数据结构的理想资源,对于计算机科学的学生、程序员以及对算法和数据结构有...

    Algorithm-Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore.zip

    Algorithm-Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore.zip,Robert Lafore第二版的数据结构与算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-Play-with-Data-Structures.zip

    Algorithm-Play-with-Data-Structures.zip,我的MOOC课程代码&lt; JAVA中的游戏数据结构&gt;。更新的内容和做法也包括在内。并用Java语言编写了Java语言。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-Algorithms-and-Data-Structures-in-Java.zip

    "Algorithm-Algorithms-and-Data-Structures-in-Java.zip"这个压缩包很可能包含了关于如何在Java中实现和应用各种算法与数据结构的教学资料或示例代码。下面将详细探讨算法和数据结构的概念及其在Java中的应用。 ...

    hapi-structures-v23-2.2.zip

    《深入理解RPKI与HSM集成:开源项目hapi-structures-v23-2.2.zip解析》 在IT行业中,网络安全与数据保护是至关重要的领域。资源公钥基础设施(RPKI)是一种用于验证互联网路由安全性的重要技术,而硬件安全模块...

    Algorithm-javascript-datastructures-algorithms.zip

    Algorithm-javascript-datastructures-algorithms.zip,用于教育目的的javascript和typescript数据结构和算法的集合。javascript算法和数据结构手册的源代码包,算法是为计算机程序高效、彻底地完成任务而创建的一组...

    Algorithm-data-structures-questions.zip

    在"Algorithm-data-structures-questions.zip"的"data-structures-questions-master"文件中,可能包含了关于算法与数据结构的各种练习和问题,旨在帮助学习者深入理解和掌握这些概念。通过解决这些问题,你可以提升...

    Object-Oriented+Data+Structures+Using+Java,+4th+Edition-(2016).pdf

    Welcome to the fourth edition ofObject-Oriented Data Structures Using Java™. This book pres- entsthealgorithmic,programming,andstructuringtechniquesofatraditionaldatastructures course in an object-...

    data-structures-Java.pdf

    《data-structures-Java.pdf》这本教材深入浅出地介绍了算法复杂性的概念及其分析方法。 ##### 1.1 渐进复杂性分析和大O记号 - **渐进复杂性分析**:这是一种用于评估算法性能的方法,主要关注随着输入数据规模的...

    PyPI 官网下载 | mercury-engine-data-structures-0.9.2.tar.gz

    《PyPI官网下载 | mercury-engine-data-structures-0.9.2.tar.gz——Python库解析与分布式技术概览》 PyPI(Python Package Index)是Python开发者常用的资源库,其中包含了大量的开源软件包和模块。在本文中,我们...

    Algorithm-Algorithms-and-Data-Structures-in-Ruby.zip

    Algorithm-Algorithms-and-Data-Structures-in-Ruby.zip,算法、数据结构和编程挑战的ruby实现,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-math-advanced-data-structures-and-algorithms.zip

    Algorithm-math-advanced-data-structures-and-algorithms.zip,数学、高级数据结构和算法-使用前请检查,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-Advanced-Data-Structures-with-Python.zip

    Algorithm-Advanced-Data-Structures-with-Python.zip,使用python为高级数据结构和算法编写的代码。对于每个代码,都有一个相关的标记,用于解释和应用该算法或数据结构。,算法是为计算机程序高效、彻底地完成任务而...

    Algorithm-Algorithms-and-Data-Structures.zip

    在"Algorithms-and-Data-Structures-master"目录中,我们可以预期找到各种算法和数据结构的实现源码,可能包括以下内容: 1. **排序算法**:例如,快速排序、归并排序、堆排序、插入排序、选择排序等,每种排序算法...

    Algorithms-and-Data-Structures-implemented-in-Go-f-Go.zip

    Algorithms_and_Data_Structures_implemented_in_Go_f_Go.zip Algorithms_and_Data_Structures_implemented_in_Go_f_Go.zip Algorithms_and_Data_Structures_implemented_in_Go_f_Go.zip Algorithms_and_Data_...

    Python-for-Algorithms--Data-Structures--and-Interviews, 关于算法和数据结构的Udemy课程文件.zip

    Python-for-Algorithms--Data-Structures--and-Interviews, 关于算法和数据结构的Udemy课程文件 用于算法。数据结构和访谈的 python ! 欢迎访问Udemy课程的知识库: 用于算法,数据结构和访谈的python !这是为你...

    Python-中文数据结构和算法教程-python-data-structures-and-algorithms.zip

    Python_中文数据结构和算法教程_python_data_structures_and_algorithms.zipPython_中文数据结构和算法教程_python_data_structures_and_algorithms.zipPython_中文数据结构和算法教程_python_data_structures_and_...

Global site tag (gtag.js) - Google Analytics