Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solving Problems Quickly UAkron Programming Team January 20, 2012.

Similar presentations


Presentation on theme: "Solving Problems Quickly UAkron Programming Team January 20, 2012."— Presentation transcript:

1 Solving Problems Quickly UAkron Programming Team January 20, 2012

2 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.

3 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.)

4 Ready?

5 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 2 31. Sample InputSample Output 11 3 533 0 0 0

6 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); }

7 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  5 A, 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 InputSample Output Primal FearThis problem is silly. RabbleThis problem is silly. Fried CrudPr1m41 F34r Make it stop

8 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; }

9 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 2 63. Sample InputSample Output 100002 100001 100001100001 0 0 0

10 Notes O(n) isn’t good enough when n is 2 billion. – Try 2000000000 222222 333333 Instead, increment through while k*a is less than n, summing as you go. Then do the same for 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?

11 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 InputSample Output SpecialYes Ace SlipYes Radar Tea QuillNo Quadrilateral Stop Stoop Quit Quit

12 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: _. You get multiple submits. – Copy solutiuons to: /home/grad/dpoeschl/2012spring/week1

13 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.

14 Ready? Take a few minutes to discuss strategy.

15 Round 1 Bank Robbin (B-W 2011) Tower of Defense (B-W 2011) (See Resources slide for links to these problems)

16 Ran out of time… more next week.

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


Download ppt "Solving Problems Quickly UAkron Programming Team January 20, 2012."

Similar presentations


Ads by Google