The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

hahahah

Posted by Chin Loong on October 26, 2001 at 4:57 AM

> Now if you can post a really interesting homework assignment that will teach me something new as I write it, then maybe I'll write it (but you'll have to also tell me the due date, so I can post it the day after). Which reminds me, Chin, I came up with a pretty cool variation of that "draw the diamond" homework from a while back. I'll post it some time later (here's a teaser: it takes a command-line parameter specifying which grade you'd like on the project and uses a different algorithm for each grade from F to A+ (yes, I had too much time on my hands)).

> - Chet

hey! that's cool! :p u can pass the codes here so that we can play around with it ;) i'm a bit busy with new incoming "potential" application projects rite now but i sure can find time to have some fun ^_^

Chet, remember a while back somebody asked for the algorithm of a odd-numbered n x n matrix squares which has the numbers of 1 to (n x n) inside each square which all sums up to the same total (horizontally, vertically, and if i'm not mistaken, diagonally too). well, when i was young i really loved to play with that algorithm, and i was seriously tempted to post the answer.. but well, guilt held me back :p. he gotta do his own assignment ;)

anyway, that's a pretty interesting algorithm.. i didn't exactly tried (i was still pretty young at that time) to figure out the formula/algorithm.. but it follows certain rules.

#assuming left-bottom most square is (0,0) and coord <=> (i,j)

1. i don't know why but i always start with the top row of the matrix, in the middle. place "1" there. ((int)(i_max/2)+1),j_max)

2. head north-east. (i+1,j+1)

for(int NUMBER=2;NUMBER<=nxn;NUMBER++)
{
IF it's a valid square (inside the matrix and empty)
{
place the NUMBER there.
}
ELSE (it's not a valid square)
{
IF it's outside the matrix
{
IF the destination column (i+1) is valid (inside matrix)
{
place the NUMBER at the bottom of the column (i+1,j_min)
}
ELSE IF the destination row (j+1) is valid (inside matrix)
{
place the next number at the left end of the row (i_min,j+1)
}
ELSE IF both the column and row not valid - outside matrix)
{
go down one square (i,j-1) and place the NUMBER there
}
}
ELSE (if not invalid square, must be non-empty square)
{
go down one square (i,j-1) and place the NUMBER there
}
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us