The majority of forums are now only available as archives, which means posting/editing is disabled.
The Anything and Everything forum is still open.
The Anything and Everything forum is still open.
Help with pascal triangle program
|
|||
|
Rank: ? (1)
Member #: 23526 |
plz guyz im really dumb, but i really want to get this done
im trying to let the user type in the row they want to see, and print it out, thats what i have so far.. thx import cs1.Keyboard; public class Triangle { public static final int EVEN = 2; public static void main(String[] args) { int numRow; System.out.println("Which row do you want to see?" numRow = Keyboard.readInt(); int[][] triangle = new int [numRow][]; for (int i = 0; i<numRow; i++) { triangle[i] = new int[i+1]; triangle[i][0] = 1; for (int j=1; j<i; j++) { triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } triangle[i][i] = 1; } System.out.println(triangle[numRow]); } } |
||
|
|||
|
|||
|
Rank: ? (171)
Member #: 11947 |
try this instead:
The n row is a array of binomial coeffecients, which essentially are combinations. the list is: nC0, nC1, nC2... nCn but to reduce processing time you can only have to do the floor of half of n, then repeat backwards. This might be an easier algorthim, and would look something like this Code:
I'm sure the combination and getRow methods are easy enough to impliment. |
||
|
|||
|
|||
|
Rank: Unregistered
|
*
*** ***** How to print like the above shape in Java? Can any one help me..? |
||
|
|||
|
|||
|
Rank: ? (171)
Member #: 11947 |
depends on the length you want to print out.
since you know that.. then the first row would print LENGTH-1 spaces, the second LENGTH-2.... 0. as long as it's a monospaced font you'll be fine. if you wanted to print until someone hit a stop button, say to a textfield or file, then you'll have to read the file, and print a single space before each line everytime you added a line. » Post edited 2005-06-09, 11:13am by Umojan.
|
||
|
Please login or register to post a reply.
