可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
I have written code for many patterns but unable to write for this..... not even getting any hint how to proceed.
I want to generate the following output:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
...where the width and height of the rectangle are specified as inputs.
回答1:
I would do it this way:
Initialize a 5x5 two-dimensional array of integers to 0. Have a direction
variable, and define constants or an enum
for the four directions. Start moving 'right' from (0, 0), filling in the array with increasing values until you hit the edge or a number that is not 0. Then, increment the direction (and wrap) and continue. Then print the array in rows.
An alternative using loops is to iterate over all the (x, y) coordinates, and pass x and y into a function that gives you the value at that position. The function I wrote does exactly the same thing as would the function that fills the array, except it doesn't write to an array, and when it reaches the given (x, y) it returns the current value. Not very efficient, but it achieves the result.
回答2:
Since this is a homework question, I'm just going to give you a hint -- start by writing this function:
void predecessor(int in_x, int in_y, int& out_x, int& out_y);
I won't say any more -- up to you to figure out what that function is for :)
回答3:
I would create 4 separate loops.
Loop 1 would create 1 2 3 4 5
Loop 2 would create 6 7 8 9
Loop 3 would create 10 11 12 13
Loop 4 would create 14 15 16
When this is done, you've filled out the outside of the square, and are left with a smaller, 3x3 square inside, which can be filled in exactly the same way, with exactly the same 4 loops.
回答4:
I just had to come up with this myself, code is for counter clockwise but you can change that simply by changing the directions array.
int x = 0;
int y = 0;
int c = width * height;
int directions[4] = { 0, 1, 0, -1};
int distances[2] = { height, width-1};
int di = 0;
int dx = directions[ di];
int dy = directions[ di+1];
int dis = distances[ di];
for( int i=0; i<c; i++)
{
value = data[ y * width + x] = i;
dis--;
if( dis == 0)
{
distances[ di % 2]--;
di++;
dx = directions[ di % 4];
dy = directions[ (di+1) %4];
dis = distances[ di % 2];
}
x += dx;
y += dy;
}
回答5:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
#define RIGHT 1
#define DOWN 2
#define LEFT 3
#define UP 4
void printArray1(int b[SIZE][SIZE], int size);
void printArray2(int b[SIZE][SIZE], int size);
int move(int b[SIZE][SIZE], int *xpos, int *ypos, int *movem, int number);
int main(void) {
int i, j, num;
int dir;
int a[SIZE][SIZE] = { 0 };
printArray1(a, SIZE);
printf("\n\n");
printArray2(a, SIZE);
dir = RIGHT;
i = 0;
j = 0;
num = 1;
for (; num <= (SIZE * SIZE); num++ )
move(a, &i, &j, &dir, num);
printArray1(a, SIZE);
printf("\n\n");
printArray2(a, SIZE);
return 0;
}
void printArray1 (int b[SIZE][SIZE], int size)
{
int i, j;
for ( i = 0; i < size; i++ ) {
for ( j = 0; j < size; j++ )
printf("%3d ", b[j][i]);
printf("\n");
}
printf("\n\n");
return;
}
void printArray2 (int b[SIZE][SIZE], int size)
{
int i, j;
for ( i = 0; i < size; i++ ) {
for ( j = 0; j < size; j++ )
printf("%3d ", b[i][j]);
printf("\n");
}
printf("\n\n");
return;
}
int move(int b[SIZE][SIZE], int *xpos, int *ypos, int *movem, int number)
{
int x, y, dirn, reqdDirn;
x = *xpos;
y = *ypos;
dirn = *movem;
if (b[x][y] == 0 ) {
b[x][y] = number;
}
reqdDirn = dirn;
switch (dirn) {
default:
printf("Unexpected value");
return;
case RIGHT:
x = x + 1;
if (b[x][y] == 0) {
if (x > SIZE-1) {
reqdDirn = DOWN;
x = SIZE - 1;
y = y + 1;
if (y > SIZE-1)
y = SIZE - 1;
}
} else {
// just step back and change direction
x = x - 1;
y = y + 1;
reqdDirn = DOWN;
}
break;
case DOWN:
y = y + 1;
if (b[x][y] == 0) {
if (y > SIZE-1) {
reqdDirn = LEFT;
y = SIZE - 1;
x = x - 1;
if (x < 0)
x = 0;
}
} else {
y = y - 1;
x = x - 1;
reqdDirn = LEFT;
}
break;
case LEFT:
x = x - 1;
if (b[x][y] == 0) {
if (x < 0) {
reqdDirn = UP;
x = 0;
y = y - 1;
if (y < 0)
y = 0;
}
} else {
// just step back and change direction
x = x + 1;
y = y - 1;
reqdDirn = UP;
}
break;
case UP:
y = y - 1;
if (b[x][y] == 0) {
if (y < 0) {
reqdDirn = RIGHT;
y = 0;
x = x + 1;
if (x > SIZE-1)
x = SIZE - 1;
}
} else {
// just step back and change direction
y = y + 1;
x = x + 1;
reqdDirn = RIGHT;
}
break;
}
*xpos = x;
*ypos = y;
*movem = reqdDirn;
}
Output
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
回答6:
//In java
package ds1;
public class SpiralMatrix {
public static void main(String[] args) throws Exception{
int[][] values = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
int x=0,y=0,z=0 ,length=values.length;
System.out.println("Jai ShRaam \n array size :"+length+" \n \n");
//Logic Starts
for (int i=0;i<values.length;i++ )
{
for (int j=0;j<values.length;j++ )
{
if(i==j){
for(int k=i;k<length-i;k++)
{
System.out.print(values[x][k]+" , ");
}
x++;y++;
for(int k=x;k<length-i;k++)
{
System.out.print(values[k][length-x]+" ; ");
}
y++;
for(int k=i+length-y;k>=i;k--)
{
System.out.print(values[length-x][k]+" - ");
}
for(int k=i+length-y;k>=x;k--)
{
System.out.print(values[k][i]+" : ");
}
}//end of i==j
}//end of j loop
//System.out.println();
}
//Logic ends
}//end of psvm
}
//Answer
1 , 2 , 3 , 4 , 5 , 10 ; 15 ; 20 ; 25 ; 24 - 23 - 22 - 21 - 16 : 11 : 6 : 7 , 8 , 9 , 14 ; 19 ; 18 - 17 - 12 : 13 ,
回答7:
public ArrayList<Integer> spiralOrder(final List<ArrayList<Integer>> a) {
ArrayList<Integer> result = new ArrayList<Integer>();
int m = a.get(0).size();
int n = a.size();
if(m>1 && n>1){
int loopCounter = (n > m) ? m*2 : n *2 -1 ;
int opr=1;
int i=0,j=0;
int opA=m,opB=n,opC=0,opD=1;
for(int k=0;k < loopCounter ;k++){
if(opr == 1){
int counter =0;
while(counter < opA){
System.out.print(a.get(i).get(j)+ ";");
result.add(a.get(i).get(j));
counter++;
if(j != opA-1){
j++;
}
else{
break;
}
}
opr =2;
continue;
}
if(opr == 2){
i++;
int counter =1;
while(counter < opB){
System.out.print(a.get(i).get(j)+ ";");
result.add(a.get(i).get(j));
counter++;
if( i != opB-1){
i++;
}
else{
break;
}
}
opr =3;
continue;
}
if(opr == 3){
j--;
int counter =j;
while(counter >= opC){
System.out.print(a.get(i).get(j)+ ";");
result.add(a.get(i).get(j));
counter --;
if(j != opC){
j--;
}
else{
break;
}
}
opr =4;
continue;
}
if(opr == 4){
i--;
int counter = i;
while(counter >= opD){
System.out.print(a.get(i).get(j)+ ";");
result.add(a.get(i).get(j));
counter --;
if(i != opD){
i--;
}else{
break;
}
}
opr =1;
j++;
opA = opA -1;
opB = opB -1;
opC= opC +1;
opD = opD+1;
continue;
}
}
}
else if(n ==1){
for(int k=0;k < a.get(0).size();k++){
result.add(a.get(0).get(k));
}
}
// Populate result;
return result;
}
回答8:
Here is my solution in Java: (I already know that a Pointer points to an specific adress on memory or is declared to make that purpose on C, and because Java does not supports pointers I did this with a non C Pointer logic) So , that said, I did this with the logic of point to the "address" (rown, column) of the matrix.
static void MatrixTravelPointer(int [][] m){
int pointerX = 0;
int pointerY = 0;
List<Integer> elements = new ArrayList<>();
int maxlength_ = 0;
int pos = 0;
for(int i=0;i<m.length;i++){
maxlength_ +=m[i].length;
}
boolean turn = false;
int turns = 1;
while(pos<maxlength_)
{
System.out.print(" "+m[pointerX][pointerY]);
elements.add(m[pointerX][pointerY]);
if( (pointerY == m[0].length - turns && pointerX == m.length - turns)){
turn = true;
turns++;
}
if(turns > 1 && turn == true && pointerX == turns - 1){
turn = false;
}
if(turn == false){
if(pointerY < m[0].length - turns){
pointerY++;
} else if(pointerX < m.length - turns){
pointerX++;
}
}
if(turn == true)
{
if(pointerY >= turns - 1){
pointerY--;
} else if(pointerX >= turns - 1){
pointerX--;
}
}
pos++;
}
System.out.print("\n");
Iterator it = elements.iterator();
while(it.hasNext()){
System.out.print(" "+(int)it.next());
}
}
For now my logic only works for cubic and rectangular matrix.
The logic goes like this using a 4x4 matrix:
Pointer travels in the matrix from [0][0] to [0][3] , at this point it turns the travel pointer to go from [0][3] to [3][3], imagine a top down "L" on Tetris ,at this point we can omit or erase the top row and right column and reverse the travel point from right to left using -- and from down to top. Because each time we form an "L" on the "second turn" we need to reduce the travel size (the "L" size) with the "turns" var depending on the times we form an "L".
回答9:
package arrays;
public class SpiralPrinting {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = new int[][] { { 1, 2, 3 }, { 5, 6, 7 }, { 9, 10, 11 },
{ 13, 14, 15 } };
int cols = a[0].length;
int rows = a.length;
Business1 b = new Business1();
b.print(a, rows, cols);
}
}
class Business1 {
int i = 0;
int j = 0;
int count = 0;
public void print(int[][] a, int row, int col) {
int max = row * col;
while (count < max) {
for (int k = 0; k < col; k++) {
System.out.println(a[i][j]);
j++;
count++;
}
row--;
j--;
i++;
for (int k = 0; k < row; k++) {
System.out.println(a[i][j]);
i++;
count++;
}
i--;
j--;
col--;
for (int k = 0; k < col; k++) {
System.out.println(a[i][j]);
j--;
count++;
}
row--;
j++;
i--;
for (int k = 0; k < row; k++) {
System.out.println(a[i][j]);
i--;
count++;
}
i++;
j++;
col--;
}
}
}