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 Palindromeso Base conversiono Boolean algebrao Primes
Converting basesint 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 "0123456789ABCD...XYZabcd...xyz".indexOf(char)o Not needed for bases≤10o Trick also applies to checking if a character is a vowel, a whitespace
character, etc.
GCD/LCMint 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 1.
• 15 pts: length of binary string <25• 20 pts: length of binary string <1,000• 25 pts: length of binary string <1,000,000• Sample input: 101110• 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 Stringo int will overflowo 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 methodo Make sure you do less than 200,000,000 operationso Copying Strings may take up to n=1,000,000 operations!o Use a StringBuffer instead
Top Related