Topic:
Returns the and of the maximum number of words for a two-dimensional array
Requirements:
Randomly generate two-dimensional arrays that require the array elements to have positive negative
Two-dimensional arrays are connected to each other
Array of contiguous n (n>=1) elements that make up the array of Word groups
Source:
Import Java.util.Random;
Import Java.util.Scanner;
public class Erweixunhuanzishuzu {
public static void Main (string[] args) {
Generating a random array
int array[][] = new int[1000][1000];
int arraytemp[] = new int [1000];
int row,col,sum,sumtemp;
int t,m;
int qx=0,qy=0;
int zx=0,zy=0;//record the starting and ending positions of the sub-series, respectively
for (int i=0;i<1000;i++)
arraytemp[i]=0;
System.out.print ("Please enter the number of rows and columns of the array separately:");
Scanner sc=new Scanner (system.in);
row = Sc.nextint ();
col = Sc.nextint ();
Random r = new Random ();
SYSTEM.OUT.PRINTLN ("The resulting sequence of random numbers is:");
for (int i=0;i<row;i++) {
for (int j=0;j<col;j++) {
Array[i][j]=r.nextint ()%10;
ARRAY[I][J+COL]=ARRAY[I][J];
}
}
for (int i=0;i<row;i++) {
for (int j=0;j<col;j++) {
System.out.print (array[i][j]+ "");
if (j = = Col-1)
System.out.println ("");
}
}
To find the largest sub-array
SUM=ARRAY[0][0];
Sumtemp=sum;
for (int i=0;i<row;i++) {
for (int x=0;x<1000;x++)
arraytemp[x]=0;
for (t=i;t>=0;t--) {
for (int s=0;s<2*col;s++) {
if (t>=0) {
Arraytemp[s]+=array[t][s];
qx=t;
Qy=s;
System.out.println ("&" +qx+ "" +qy);
System.out.println ("%" +arraytemp[s]+ "%");
}
}
SUMTEMP=ARRAYTEMP[0];
m=0;
for (int j=0;j<2*col;j++) {//column-based elimination
if (sumtemp<=0) {
Sumtemp=0;
m=j+1;
qx=t;
Qy=j;
System.out.println ("&1" +qx+ "+qy");
}
System.out.println ("#" +sumtemp+ "#");
System.out.println ("*" +arraytemp[j+1]+ "*");
if (J+1<qy+col) {
SUMTEMP+=ARRAYTEMP[J+1];
if (sumtemp>sum) {
Sum=sumtemp;
qx=t;
Qy=m;
Zx=i;
zy=j+1;
System.out.println ("@" +qx+ "" +qy);
System.out.println ("%" +zx+ "" +zy);
}
}
/*else
break;*/
}
}
}
System.out.println ("The number of the largest sub-arrays is:" +sum);
System.out.println ("sub-array is:");
for (int i=qx;i<=zx;i++)
for (int j=qy;j<=zy;j++) {
System.out.print (array[i][j]+ "");
if (J==zy)
System.out.println ("");
}
}
}
Results:
Programming Summary:
Now for modular programming gradually have their own understanding, from the beginning to hear this concept, only know it, do not know why, after this several times about the array code, "why" gradually emerged.
The emergence of a new problem can always be based on its characteristics, according to different priorities, a problem to be refined into a number of "widgets", to solve these groups than directly solve the big problem itself is much easier.
The harvest for this program:
See the programming requirements of this time, the feeling should be divided into two aspects of the problem:
1, the realization of the two-dimensional array of its own "ring"
2, and the maximum number of words to find the problem of the group
Because in the understanding of their own level, what I need to do now is to absorb other people programming algorithms, thinking, of course, this is only to improve their programming level of a stage, with the increase of this exercise, I believe there will be more harvest and understanding, as long as I would like to do, and not imagine I have to do.
Now there is a preliminary experience, many algorithms can be combined, some of the previously implemented algorithms through a reasonable arrangement, can solve a new problem, which is a result of the accumulation of problem-solving method, it takes a long time to insist on and efforts.
I'm on my way ~
Two-dimensional array and maximum word Group 2