Not a Math Honor Society meeting. Or: How I learned to do math again, this time with cows.

Slides:



Advertisements
Similar presentations
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Advertisements

Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Advanced Programming 15 Feb The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
This title is orange because of Halloween. It is totally not because it’s always orange.
Mathematical. Approach  Many of these problems read as brain teasers at first, but can be worked through in a logical way.  Just remember to rely on.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
USACOW: The Aftermath. PotW Solution Scanner s = new Scanner(System.in); int n = s.nextInt(), k = s.nextInt(), x = 0; for (int i = 0; i < n; i++) x ^=
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
2. I/O Text Files CCSA225 - Advanced Java Programming Sarah Alodan
CS1101: Programming Methodology Aaron Tan.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Lesson 6C – Loops 3 – Advanced File Processing By John B. Owen All rights reserved ©2011.
Moooooo Or: How I learned to stop worrying and love USACO.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Houyang 3/25/13. USACO March Contest Congrats to Johnny Ho for scoring over 900 points in the Gold Division o 7th place in US Kudos to Jonathan Uesato.
Happy Birthday Julia Courier New. USACO December Contest - Congratulations to Jon+Julia+Andy for promoting to gold And Johnny - 2th place in gold among.
Public void main What do you call something that’s not static?
February 23, Announcements ACSL – March 9 Digital Electronics – 2 Graph Theory – 2 Boolean Algebra – 1 USACO – March
Solving Problems Quickly UAkron Programming Team January 20, 2012.
Happy Birthday Tony Palindromes.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
The SECOND Meeting Or: How I learned exciting new things in CS Club.
Boolean Algebras Lecture 27 Section 5.3 Wed, Mar 7, 2007.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
The SECOND Meeting Or: How I learned exciting new things in CS Club.
CSE 143 Lecture 22 Huffman slides created by Ethan Apter
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
What is Binary Code? Computers use a special code of their own to express the digital information they process. It's called the binary code because it.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Boolean Algebras Lecture 24 Section 5.3 Wed, Mar 22, 2006.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Java String Methods - Codehs
Recursion Version 1.0.
Week 2 - Wednesday CS 121.
OO Design and Programming II I/O: Reading and Writing
Strings and File I/O.
Week 6 Discussion Word Cloud.
CSS161: Fundamentals of Computing
Advanced Programming Behnam Hatami Fall 2017.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Chapter 9 Strings and Text I/O
CIS16 Application Development and Programming using Visual Basic.net
Conditionals.
Recursion Problems.
Loop problem Write a method printLetters that prints each letter from a word separated by commas. For example, the call: printLetters("Atmosphere") should.
CS2011 Introduction to Programming I Strings
Building Java Programs
Building Java Programs
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Presentation transcript:

Not a Math Honor Society meeting. Or: How I learned to do math again, this time with cows.

Lask Week’s PotW – two solutions DON'T add extra prompts for filename, etc. File f=new File("cowmilk.in"); FileInputStream in=new FileInputStream(f); byte b[]=new byte[(int)f.length()+10]; in.read(b); in.close(); PrintWriter o = new PrintWriter(new BufferedWriter(new FileWriter("cowmilk.out"))); String s=new String(b); o.println(s.replaceAll("\\s+","\n").trim()); o.close(); or File f=new File("cowmilk.in"); Scanner s=new Scanner(f); PrintWriter o = new PrintWriter(new BufferedWriter(new FileWriter("cowmilk.out"))); if(s.hasNext()) o.print( s.next() ); while(s.hasNext()) { o.println(); o.print( s.next() ); } s.close(); o.close();

How much math do I need to know for USACO? No calculus o Aww, no hyperbolic paraboloids and prolate spheroids! :'( Mostly just logical and systematic thinking process But math can come in handy Common "math" type problems include, but not limited to, dealings with: o Palindromes o Base conversion o Boolean algebra o Primes

Converting bases int getNum() { int t = 0; while (hasNextDigit()) // imaginary functions t = t * base + nextDigit(); return t; } void putNum(int t) { if (t == 0) return; putNum(t / base); // recursion! putDigit(t % base); // imaginary function }

Converting bases (cont.) Trick for converting higher bases, at a loss of efficiency: o " ABCD...XYZabcd...xyz".indexOf(char) o Not needed for bases ≤ 10 o Trick also applies to checking if a character is a vowel, a whitespace character, etc.

GCD/LCM int gcd(int a, int b) // use Euclidean algorithm { while (true) { a = a % b; if (a == 0) return b; b = b % a; if (b == 0) return a; } } int gcd(int a, int b) { return b==0?a:gcd(b,a%b); // hax } int lcm(int a, int b) { return a * b / gcd(a, b); // well-known property }

PotW – Farmer John’s Experiment Bessie the cow has become obsessed with becoming young again. Farmer John gives her some "magical water" that does the following to her "age" for every glass she drinks: o If her current age is even, divide it by two. o If it is odd, add one. Given Bessie's age in binary, output how many glasses she will have to drink to reduce her age to pts: length of binary string <25 20 pts: length of binary string <1, pts: length of binary string <1,000,000 Sample input: Sample output: 8 Time limit: 1sec. Use standard input/output this time

Hints For 15 pts, n<25 - convert the binary string to an int o As long as the number is not equal to 1, keep applying the function. For 20 pts, n<1,000 - use a String o int will overflow o To check if it is even, get the last digit using "charAt" o To divide by two, remove the last digit using "substring" o To add one, find the number of consecutive 1s at the end of the string Replace all of them with 0s Replace the 0 before these 1s with a 1. o Special case: What if the entire string is composed of 1s? For 25 pts, n<1,000,000 - you need a faster method o Make sure you do less than 200,000,000 operations o Copying Strings may take up to n=1,000,000 operations! o Use a StringBuffer instead