110 likes | 232 Vues
Lynbrook Computer Science. “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” –Tom Cargill. Last Week’s Problem. Bignum
E N D
Lynbrook Computer Science “The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” –Tom Cargill
Last Week’s Problem • Bignum • Adding, subtracting, multiplying, and dividing large numbers
Solution Variables: private String bigNum; privatebyte[] digits; privatebytesign; Constructor: publicBigNum( String num ) { bigNum = num;
Solution (cont.) // determine if negative/positive if( bigNum.charAt( 0 ) == '-' ) { sign = -1; bigNum = bigNum.substring( 1 ); } else sign = 1; // store the digits digits = newbyte[ bigNum.length() ]; for( inti = 0; i < digits.length; i++ ) digits[i] = (byte) ( bigNum.charAt( digits.length - 1 - i ) - '0' ); }
Solution (cont.) Subtraction: publicBigNum subtract( BigNum other ) { // add the opposite return add( other.swapSign() ); } Main logic behind adding and subtracting: for( inti = 0; i < maxLength - 1; i++ ) { digit = otherDigit = 0; // get the digits, if they exist if( i < length ) digit = (byte) ( sign * digits[i] );
Solution (cont.) if( i < otherLength ) otherDigit = (byte) ( other.sign * other.digits[i] ); // sum of the digit and any carry result = (byte) ( digit + otherDigit + carry ); // adding if( other.sign == 1 ) { if( result >= 10 ) { carry = 1; result -= 10; } else carry = 0; }
Solution (cont.) // subtracting else { // don't carry if this is the last digit if( result < 0 && i != length - 1 ) { carry = -1; result += 10; } else carry = 0; }
Solution (cont.) // since we're reversing the buffer at the end, addthe negative sign after the digit value if( result < 0 ) buffer.append( -result + "-" ); else buffer.append( result ); }
New Problem of the Week Denise the robot unicorn wants to bring her infamous infinitely large collection of fabulous items to school for show-and-tell. However, she can only carry a limited weight, so she wants to bring the most fabulous collection she can carry. There are many different types of fabulous items, including, but not limited to, fabulous pixies, fabulous stars, and fabulous dolphins. Each type of item has a certain level of fabulousness and a certain weight.
New Problem of the Week (cont.) • Help Denise devise and implement an algorithm to determine the maximum fabulousness of the collection to bring. • Input will be read from the console, in this format: X YW1 V1W2 V2W3 V3...WY VY Where X (1 < X < 1000) is the maximum weight Denise can carry, Y (1 < Y < 1000) is the number of items (how many lines you have to read), and the W (1 < W < 1000) and V (1 < V < 9012) values are the weight and value, respectively, of each type of item. • The output (to the console) shall consist of a single number, the maximum value of the items Denise will carry.
USACO Schedule • Sent out via e-mail • Check your spam • 22-25 Oct, 2010 – USACO Qualification Contest ** OPTIONAL ** • 5-8 Nov, 2010 – USACO November Contest • 3-6 Dec, 2010 – USACO December Contest • 7-10 Jan, 2011 – USACO January Contest • 4-7 Feb, 2011 – USACO February Contest • 11-14 Mar, 2011 – USACO March Contest • 28Apr-2May, 2011 – USACO US Open • Early Jun, 2011 – USA Invitational Computing Olympiad (~8 days, by invitation), Clemson, SC • 22-29 Jul, 2011 – IOI – Pattaya, Thailand (by invitation) –> conflicts with IMO!