Solving Problems Quickly UAkron Programming Team January 20, 2012.

17
Solving Problems Quickly UAkron Programming Team January 20, 2012

Transcript of Solving Problems Quickly UAkron Programming Team January 20, 2012.

Page 1: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Solving Problems Quickly

UAkron Programming TeamJanuary 20, 2012

Page 2: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Part 1 Rules• Teams of 1. One problem per round.• This is about getting a correct solution QUICKLY!

– But it still has to execute in the time allowed.• Problem will be revealed on the board.

– Entire specification will be on one slide.– The spec will be less verbose than typical problems.

• You get ONE SUBMIT (if your first submit is wrong, keep working, but the 20 minute penalty will likely be insurmountable) – The command to run the program should be typed into the console but not

executed. – THEN raise your hand (or otherwise get my attention if I’m distracted). I

will come over and test it.• First correct answer wins.

Page 3: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Part 1 Hints

• Do the minimum amount of work necessary to get a correct solution that runs within time.

• Double check your output formatting (you only get one submit).

• Quickly try at least a couple non-sample input cases and edge cases (0/1/many, upper bounds, halting conditions, etc.)

Page 4: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Ready?

Page 5: Solving Problems Quickly UAkron Programming Team January 20, 2012.

1. Generalized Euler 1

• If we list all the natural numbers below 11 that are multiples of 3 or 5, we get 3, 5, 6, 9 and 10. The sum of these multiples is 33.

• Each line will contain three positive integers n a b, with n < 100002, a < 10000 and b < 10000. Find the sum of all the multiples of a or b below n. Stop when n = a = b = 0. The sum will be less than 231.

• Sample Input Sample Output11 3 5 330 0 0

Page 6: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Example code• Scanner scan = new Scanner(System.in);

while(true){ int n = scan.nextInt(); if(n == 0) return; int a = scan.nextInt(); int b = scan.nextInt(); int sum = 0;

// O(n) is good enough when n is this small. for(int i = 1; i < n; i++) if(i % a == 0 || i % b == 0) sum += i;

System.out.println(sum);}

Page 7: Solving Problems Quickly UAkron Programming Team January 20, 2012.

2. Pr1m41 F34r• Leet speak is silly, but here’s a problem about it anyway. You’ll be given a string

containing only alphabetic characters and spaces. Convert the following letters to digits:E, e 3 L, l 1 T, t 7 S, s 5A, a 4 I, i 1 O, o 0 G, g 9

• Any spaces and letters that were not converted to digits should be discarded, the digits should be concatenated into one positive integer n with 1 < n < 1000000 (guaranteed).

• If n is prime, print: “Pr1m41 F34r”. If n is composite, print “This problem is silly.” An input string of “Make it stop” indicates the end of input and should not be processed.

• Sample Input Sample OutputPrimal Fear This problem is silly.Rabble This problem is silly.Fried Crud Pr1m41 F34rMake it stop

Page 8: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Example code

• Since we only need to check one integer for primality per input case, don’t waste time making the prime check too efficient. The following is definitely fast enough:

boolean isPrime(int n){ for(int i = 2; i <= Math.sqrt(n); i++) { if(n % i == 0) return false; } return true;}

Page 9: Solving Problems Quickly UAkron Programming Team January 20, 2012.

3. More Generalized Euler 1

• If we list all the natural numbers below 11 that are multiples of 3 or 5, we get 3, 5, 6, 9 and 10. The sum of these multiples is 33.

• Each line will contain three positive integers n a b, with 100,000 < {n, a, b} < 2,000,000,002. Find the sum of all the multiples of a or b below n. Stop when n = a = b = 0. The sum will be less than 263.

• Sample Input Sample Output100002 100001 100001 1000010 0 0

Page 10: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Notes

• O(n) isn’t good enough when n is 2 billion.– Try 2000000000 222222 333333

• Instead, increment through <a, 2a, 3a, 4a, …> while k*a is less than n, summing as you go. Then do the same for <b, 2b, 3b, 4b, …> and sum as you go unless the term k*b is also divisible by a (in which case it’s already been counted). – This is O(n/a) + O(n/b), and in the worst case 2000000000 /

100000 = 20000 iterations.• There’s also a O(1) solution to this problem. Can you

find it?

Page 11: Solving Problems Quickly UAkron Programming Team January 20, 2012.

4. Anagrams• Given two strings, determine if they are anagrams, meaning that one

string can be created by rearranging the letters in the other (ignoring casing and spaces in our case). When the two strings are both exactly “Quit”, this indicates the end of input and should not be processed.

• Sample Input Sample OutputSpecial YesAce Slip YesRadar Tea Quill NoQuadrilateralStopStoopQuitQuit

Page 12: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Part 2 Rules

• Teams of 2. • This is about getting TWO correct solutions

QUICKLY!– But they still have to execute in the time allowed.

• Problems will be handed out.– Name your source files:

<teamname>_<problem>.<extension>• You get multiple submits.– Copy solutiuons to:

/home/grad/dpoeschl/2012spring/week1

Page 13: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Part 2 Hints

• The order in which you solve the problems might matter (but it might not)

• Teamwork, teamwork, teamwork.• All the hints from part 1– Do the minimum amount of work necessary to get 2

correct solutions that run within time.– Double check your output formatting, you only get

one submit.– Quickly try at least a couple non-sample input cases.

Page 14: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Ready?

• Take a few minutes to discuss strategy.

Page 15: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Round 1

• Bank Robbin (B-W 2011)• Tower of Defense (B-W 2011)

• (See Resources slide for links to these problems)

Page 16: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Ran out of time… more next week.

Page 17: Solving Problems Quickly UAkron Programming Team January 20, 2012.

Resources

• B-W Contest– http://contest.acm.bw.edu/contest/problems/