![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Aug 2004
Posts: 1
Rep Power: 0
![]() |
first of all, this is a school problem. It's not mandatory to solve, but it's driving me crazy. We're supposed to take an integer input from a file and display it in a matrix, with a 1 in the center, for example if the input was 3 we should have a matrix like this
9 8 7 2 1 6 3 4 5 I'm having trouble doing this. Here's the code I have. import java.io.*;
import java.util.*;
public class Shootout
{
public static void main(String[]args) throws Exception
{
Scanner keyboard = new Scanner(new File("pr92.dat"));
int side = keyboard.nextInt();
int matrix[][] = new int [side][side];
int maxLength = side;
int maxWidth = side;
int direction = 0;
int max = side^2;
int current=0;
for(int i=((side+1)-maxLength); i<maxLength; i++)
{
for(int j=((side+1)-maxWidth); j<maxWidth; j++)
{
if(direction==0)
{
for(int k = 0; k<side; k++)
{
matrix[i][j] = max-j;
current = max-k;
}
direction = 2;
maxWidth--;
}
if(direction==1)
{
for(int k = 0; k<side; k++)
{
matrix[i][j] = current;
current--;
}
direction = 2;
maxWidth--;
}
if(direction==2)
{
for(int k = 0; k<side; k++)
{
matrix[j][i]=current;
current--;
}
}
direction=1;
maxWidth--;
// maxWidth--;
}
maxLength--;
}
for(int i = 0; i<side; i++)
{
for(int j=0; j<side; j++)
{
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
}Please go easy on me. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
This is called a Ulam Spiral... if you want more graphical representation or a few docs to read, check it out via google.
Here are two Java classes to handle this... Enjoy.
import java.awt.*;
public final class SpiralGenerator extends Object {
static final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
int count, remain, distance, direction;
SpiralGenerator() {
count = 1;
remain = 2;
distance = 1;
direction = RIGHT;
}
public void generate(int number, Point point) {
int dx = 0, dy = 0;
for (; count <= number; count++) {
if (--remain == 0) {
switch (direction) {
case UP: distance++; direction = LEFT; break;
case DOWN: distance++;
default: direction--; break;
}
remain = distance;
}
switch (direction) {
case LEFT: --dx; break;
case RIGHT: ++dx; break;
case UP: --dy; break;
case DOWN: ++dy; break;
}
}
point.translate(dx, dy);
}
}public final class PrimeGenerator extends Object {
int number, index, primes[], squares[];
PrimeGenerator() {
index = 0;
number = 1;
primes = new int[1];
squares = new int[1];
}
public int generate() {
if (number > 2) {
loop:
while(true) {
number += 2;
for (int i = 1; i < index && number >= squares[i]; i++) {
if ((number % primes[i]) == 0) continue loop;
}
break loop;
}
}
else number++;
squares[index] = (primes[index] = number) * number;
if (++index == primes.length) reallocate();
return number;
}
void reallocate() {
int array[];
System.arraycopy(primes, 0, (array = new int[primes.length * 2]), 0, primes.length);
primes = array;
System.arraycopy(squares, 0, (array = new int[squares.length * 2]), 0, squares.length);
squares = array;
}
}
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|