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;
}
分享到:
相关推荐
Title: Problem Solving in Data Structures & Algorithms Using Java: The Ultimate Guide to Programming Author: Hemant Jain Length: 436 pages Edition: First Edition Language: English Publisher: ...
Title: Problem Solving in Data Structures & Algorithms Using C++: Programming Interview Guide Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2017-01-08 ISBN...
Data Structures & Algorithms Using JavaScript by Hemant Jain English | 17 May 2017 | ASIN: B072J44X62 | 614 Pages | AZW3 | 4.22 MB This book is about the usage of data structures and algorithms in ...
Coding The Coding Interview 6th Edition PDF, walks through Data Structures and Algorithms problems as well as answers.
Cracking The Coding Interview 5th Edition, goes over coding interview and Data Structures and Algorithms problems as well as solutions.
The book covers a large number of the most common interview problems, as well as Ruby implementations of all major data structures and algorithms. All code has been thoroughly tested, and is written ...
This is the collection of algorithms, data structures and Interview Questions with solutions. This repository contains my solutions for common algorithmic problems and implementation of Data ...
leetcode卡awesome-interview A curated ...Structures Title Stars Programming languages Natural languages Highlights 48855 JavaScript English, 简体中文, 繁体中文, 한국어, 日本语, Polski,