Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 Last Week’s Problem Bignum Adding, subtracting, multiplying, and dividing large numbers

3 Solution Variables: private String bigNum; private byte[] digits; private byte sign; Constructor: public BigNum( String num ) { bigNum = num;

4 Solution (cont.) // determine if negative/positive if( bigNum.charAt( 0 ) == '-' ) { sign = -1; bigNum = bigNum.substring( 1 ); } else sign = 1; // store the digits digits = new byte[ bigNum.length() ]; for( int i = 0; i < digits.length; i++ ) digits[i] = (byte) ( bigNum.charAt( digits.length - 1 - i ) - '0' ); }

5 Solution (cont.) Subtraction: public BigNum subtract( BigNum other ) { // add the opposite return add( other.swapSign() ); } Main logic behind adding and subtracting: for( int i = 0; i < maxLength - 1; i++ ) { digit = otherDigit = 0; // get the digits, if they exist if( i < length ) digit = (byte) ( sign * digits[i] );

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

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

8 Solution (cont.) // since we're reversing the buffer at the end, add the negative sign after the digit value if( result < 0 ) buffer.append( -result + "-" ); else buffer.append( result ); }

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

10 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 Y W 1 V 1 W 2 V 2 W 3 V 3... W Y V Y 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.

11 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 28 Apr-2 May, 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!


Download ppt "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."

Similar presentations


Ads by Google