Canadian Computing Competition: 2001 Stage 1, Junior #4, Senior #2
A spiral of numbers can start and end with any positive integers less than . Write a program which will accept two positive integers and as input, and output a list of numbers from to inclusive, shown in a spiral. You may assume that the end value is greater than or equal to the start value.
A spiral starts with the first number in the centre. The next number appears immediately below the first number. The spiral continues with the numbers increasing in a counter-clockwise direction until the last number is printed.
Sample Input 1
Copy
10
27
Sample Output 1
Copy
27 26
16 15 14 25
17 10 13 24
18 11 12 23
19 20 21 22
Sample Input 2
Copy
7
12
Sample Output 2
Copy
12 11
7 10
8 9
Solution
import java.io.*;
import java.util.*;
public class Solutions {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int start =in.nextInt();
int end = in.nextInt();
int [][] spiral =
{{73, 72, 71, 70, 69, 68, 67, 66, 65, 100},
{74, 43, 42, 41, 40, 39, 38, 37, 64, 99},
{75, 44, 21, 20, 19, 18, 17, 36, 63, 98},
{76, 45, 22, 7, 6, 5, 16, 35, 62, 97},
{77, 46, 23, 8, 1, 4, 15, 34, 61, 96},
{78, 47, 24, 9, 2, 3, 14, 33, 60, 95},
{79, 48, 25, 10, 11, 12, 13, 32, 59, 94},
{80, 49, 26, 27, 28, 29, 30, 31, 58, 93},
{81, 50, 51, 52, 53, 54, 55, 56, 57, 92},
{82, 83, 84, 85, 86, 87, 88, 89, 90, 91}};
for(int i =0; i < 10; i++) {
for(int l=0; l < 10; l++) {
spiral[i][l] += start-1;
}
}
for(int i=0; i < 10; i++) {
for(int l=0; l <10; l++) {
if(spiral[i][l] > end) {
spiral[i][l] = 0;
}
}
}
for(int i=0; i < 10; i++) {
for(int l=0; l < 10; l++) {
if(spiral[i][l] ==0) {
System.out.print(" ");
}else {
System.out.print(spiral[i][l] + " ");
}
}
System.out.println();
}
}
}