Problem solution · Java

CCC 2002 S5 - Bouncing Ball

CCC 2002 S5 - Bouncing Ball: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
94 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 2002 S5 - Bouncing Ball, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 94 lines of Java from the credited upstream file ccc02s5.java.
  • The implementation visibly relies on sequence storage.
  • 1 loop block detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2002 S5 - Bouncing Ball · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2002// Problem S5: bouncing ball // fixed previous bug: Jan 2005 // given the width and height// starting position startx,0 and first bounce width,starty// the slope of the line is starty / (width-startx)// the thus y = slope * (x-startx) is the equation of the line // if you project out this line and look where// multiples(k) of width or height hit the line,// test if they are within 5 of a multiple of the other // finally the # bounces are the number of widths and heights// that divide into this point. import java.awt.*;import hsa.*; public class S5Bounce{     public static void main (String[] args)    {	double width, height, startx, starty, x, y, a, b;	double slope;	Console c = new Console (); 	TextInputFile fi = new TextInputFile ("ball5.in");	TextOutputFile fo = new TextOutputFile ("ball5.out");	int numwidth, numheight, k, bounce;	boolean done; 	// read info;	width = fi.readLong ();	height = fi.readLong ();	startx = fi.readLong ();	starty = fi.readLong (); 	done = false;	slope = starty / (width - startx);	for (k = 1 ; k <= 1000000 && !done ; k++)	{	    // calculate where the line hits multiples of the width	    y = slope * ((k * width) - startx);	    // calculate where the line hits multiples of the height	    x = (k * height) / slope + startx; 	    // calculate the number of heights, closest to y	    numheight = (int) ((y - (height / 2)) / height) + 1;	    // calculate the number of widths, closest to x	    numwidth = (int) ((x - (width / 2)) / width) + 1;	    a = numheight * height;	    b = numwidth * width; 	    if ((Math.abs (a - y) < 5) || (Math.abs (b - x) < 5))	    {		// if sunk on multiple of width, bounces = 1 less than		// numbers of widths(k), plus the number of heights in this y value		if (Math.abs (a - y) < 5)		    if (a != y)			bounce = k - 1 + (int) (y / height);		    else			// if it hit precisely, don't count last one			bounce = k - 1 + (int) (y / height) - 1; 		// if sunk on multiple of height, bounces = 1 less than		// numbers of heights(k), plus the number of widths in this x value		else		    if (b != x)			bounce = k - 1 + (int) (x / width);		    else			// if it hit precisely, don't count last one			bounce = k - 1 + (int) (x / width) - 1; 		fo.println (bounce);		c.println (bounce);		done = true;	    } 	}	if (!done)	{	    fo.println ("0");	    c.println ("0");	}	fi.close ();	fo.close ();    }}   

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗