So far we\'ve talked about container classes ( the Bag class, Sequence class, ST
ID: 3607239 • Letter: S
Question
So far we've talked about container classes ( the Bag class, Sequence class, STL multiset class, Linked List, Dynamic Arrays, Template Functions)
PROBLEM 3 Suppose we are creating a daily check in system. Every day employees check into the program and their id is saved. . You want to make sure that a person checks-in only once, therefore every night the program removes copies. Consider the data structures we have talked about so far. What do you use to implement this and why?Explanation / Answer
public class GreatestCommonDivisor { /** * Calculate greatest common divisor of two numbers using recursion. * * Time complexity O(log(a+b)) *
* @param a Long integer * @param b Long integer * @return greatest common divisor of a and b */ public static long gcdUsingRecursion(long a, long b) { a = Math.abs(a); b = Math.abs(b); return a == 0 ? b : gcdUsingRecursion(b%a, a); } /** * A much more efficient method is the Euclidean algorithm, which uses a division algorithm such as long division * in combination with the observation that the gcd of two numbers also divides their difference. *
* @see Euclidean Algorithm (Wikipedia) */ public static final long gcdUsingEuclides(long x, long y) { long greater = x; long smaller = y; if (y > x) { greater = y; smaller = x; } long result = 0; while (true) { if (smaller == greater) { result = smaller; // smaller == greater break; } greater -= smaller; if (smaller > greater) { long temp = smaller; smaller = greater; greater = temp; } } return result; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.