Introduction to algorithm 5: array:

Posted by jrforrester on Thu, 27 Jan 2022 11:05:01 +0100

Introduction to algorithm 5: array:

introduction:

Under the arrangement of the group, we will study the algorithm for three weeks in a unified way this summer vacation. My biggest feeling here is that it's really good for you to be introduced by someone in front. You can take a lot of wrong roads and quickly increase your ability and level, but you are required to study independently in the follow-up, because the route of others is others' after all, It can't be completely suitable for yourself, but the introduction series is the only way for every programmer or algorithm loving student. The introduction series is the basic stage. Only by laying a good foundation can we go further and better. After completing the introductory series of special training, I summarized my problems and problems encountered in the process of doing problems and problem-solving ideas into notes.

I remember when I went to the data structure, the teacher said that the data structure is like the internal skill of Wulin experts. When we just practice, the internal skill is relatively weak, but when we practice persistently, we will eventually develop an internal skill and become a master. come on.

Array:

Computers are fast and can process thousands of data in one second. The previous examples are to process the data immediately after reading a data, and then do not need to use the data anymore; Sometimes, after reading the data, we need to save the data for reuse in the future. If you save several individual data, you can set up several variable storage; But if you want to store thousands of data, you can't define thousands of variables.

1. Luogu p1428 Java fish are more lovely than

AC Code:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //Total number of fish
        int fishSum = scanner.nextInt();
        //The loveliness of six headed fish
        int[] arr = new int[fishSum];
        for (int i = 0; i < fishSum; i++) {
            arr[i] = scanner.nextInt();
        }
        //Cute value
        int[] cute = new int[arr.length];
        //Traverse all fish
        for (int i = 0; i < arr.length; i++) {
            //Compared with the fish on its left
            for (int j =i-1; j >= 0; j--) {
                if (arr[i] > arr[j]){
                    cute[i]++;
                }
            }
        }
        //Output the list of lovely values
//        System.out.println(Arrays.toString(cute));
        for (int i = 0; i < fishSum; i++) {
            System.out.print(cute[i]+" ");
        }
    }
}

Key points of this topic:

  • Two arrays need to be defined, one is the data array of the loveliness degree of the input fish, and the other is the array of the loveliness degree of the fish after comparison than the loveliness number on the left
  • Two layer for loop, one layer traverses each group of data, and one layer traverses the current 🐟 previous 🐟, In comparison, count.

2. Luogu p1427 Java fish digital game

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[100];
        int middle = 0;
        //input data
        for (int i = 0; ; i++) {
            arr[i] = sc.nextInt();
            if (arr[i] == 0){//When 0 is encountered, the input is ended and the current i value is recorded for output
                middle = i;
                break;
            }
        }
        //Traverse the left and right data from middle - 1 to 0. Why traverse in the direction is to output this string of data backwards.
        for (int i = middle-1; i >= 0 ; i--) {
            System.out.print(arr[i]+" ");
        }
    }
}

3. Luogu p5727 hail conjecture

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[1000];
        int count = 1;//Intermediate variable count, see how many steps are needed at last
        if(n == 1){//When the input n is equal to 1
            System.out.println("1");
        }else{//When the input n is not equal to 1
            while(n != 1){//The final result of the operation in the question is n = 1; So will n= 1 as the cycle condition, it ends when n == 1.
                arr[count] = n;//Store the value obtained each time into the array.
                if(n % 2 == 0){//Even time
                    n = n / 2;
                }else{//Odd time
                    n = n * 3 + 1;
                }
                count++;//Count: how many pieces of data are there
            }
            arr[count] = 1;//The condition of the while loop is n= 1; Therefore, when n == 1, it is not stored in the array and needs to be stored separately
            //Traverse to get the desired data.
            for (int i = count; i > 0; i--) {
                System.out.print(arr[i]+" ");
            }
        }
    }
}

4. Trees outside Luogu p1047 Java school

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int l = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[10001];//Because 1 < = l < = 10000;
        int[] b = new int[10001];
        int n = 2*m;
        //Assign values to the beginning and end of each section
        for(int i=0; i<n; i+=2) {
           b[i] = sc.nextInt();//First node
           b[i +1] = sc.nextInt();//Tail node
        }
        //Assign values to points in the whole interval
        for(int i=0; i<=l; i++) {
            a[i] = 1;
        }
        int r,s;
        for(int i=0; i<n; i+=2) {
            r = b[i];   //Interval starting point
            s = b[i+1]; //Interval end point
            for(int j=r; j<=s; j++) {//Map the elements in each interval of array b to 0 in array a
                a[j] = -1; //Mark the matching in the section with your assignment
            }
        }
        int k = 0;
        for(int i=0; i<=l; i++) {
            if(a[i] != -1) {
                k++;    //It is used to count the number of non-zero numbers, that is, the number of numbers not in the interval of array b
            }
        }
        System.out.println(k);
    }
}

Key points of this topic:

  • Using the marking method, first reset all the data in the interval to 1, and then traverse the interval to be moved by the tree. If it is 1, it will become - 1. If it is not 1, it will not move. Finally, the part that is 1 is the part that does not move.

5. Luogu p5728 Java's equal opponent

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;//Count use

        int[][] a = new int[1100][5];//Each student's grades in each subject
        int[] b = new int[1100];//Total score of each student
        //Assign values to each student's grades and total scores
        for (int i = 0; i < n; i++) {
            a[i][0] = sc.nextInt();
            a[i][1] = sc.nextInt();
            a[i][2] =sc.nextInt();
            b[i] =a[i][0] + a[i][1] + a[i][2];
        }
        //During the cycle, you can't compare yourself with yourself, so the second variable should start from the first variable + 1 + 1.
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if(Math.abs(a[i][0] - a[j][0]) <= 5 && Math.abs(a[i][1] - a[j][1]) <= 5 && Math.abs(a[i][2] - a[j][2]) <= 5 && Math.abs(b[i] - b[j]) <= 10){//Remember to take the absolute value and use math abs();
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

Key points of this topic:

  • N < 1000. For convenience, just started. In order to prevent the array from crossing the boundary, you can set it larger appropriately. You can also set it more accurately
  • During the cycle, you can't compare yourself with yourself, so the second variable should start from the first variable + 1 + 1
  • When comparing the scores of each subject with the total score, remember to take the absolute value, otherwise the comparison data is wrong

6. Luogu p5769 Java crafts

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //Enter the length, width and height of this artwork
        int x = scanner.nextInt(), y = scanner.nextInt(), z = scanner.nextInt();
        //Enter cutting data
        int num = scanner.nextInt();
        int[][][] a = new int[x + 1][y + 1][z + 1];//The data starts from 1, so add one to the original basis
        for (int i = 0; i < num; i++) {//Traverse and input the data of each cutting
            //Enter starting point
            int x1 = scanner.nextInt(), y1 = scanner.nextInt(), z1 = scanner.nextInt();
            //Enter end point
            int x2 = scanner.nextInt(), y2 = scanner.nextInt(), z2 = scanner.nextInt();
            //Use the marking method to assign and mark the data in this area
            for (int j = x1; j <= x2; j++) {
                for (int k = y1; k <= y2; k++) {
                    for (int l = z1; l <= z2; l++) {
                        a[j][k][l] = 1;
                    }
                }
            }
        }
        int counter = 0;//The volume of the remaining handicrafts is 1 * 1 * 1, so it can be accumulated directly
        //Traverse the whole handicraft, and the parts that are not marked and assigned are added up,
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= y; j++) {
                for (int k = 1; k <= z; k++) {
                    if (a[i][j][k] == 0) {
                        counter++;
                    }
                }
            }
        }
        System.out.println(counter);
    }
}

Key points of this topic:

  • Using the marking method, the cut part is assigned and marked.

7. Luogu p2550 Java lottery lottery

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] a = new int[n][7];//I bought my own lottery
        int[] b = new int[7]; //Winning lottery
        int[] c = new int[]{0,0,0,0,0,0,0}; //Winning number array

        //Assign a value to the winning ticket
        for (int i = 0; i < 7; i++) {
            b[i] = sc.nextInt();
        }
        //Assign a value to your lottery ticket
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 7; j++) {
                a[i][j] = sc.nextInt();
            }
        }

        for (int i = 0; i < n; i++) {//This cycle is its own lottery
            int temp = 0;
            for (int j = 0; j < 7; j++) {//Traverse each data of a lottery ticket
                for (int k = 0; k < 7; k++) {//Traverse every data of the winning lottery
                    if (b[k] == a[i][j]){
                        temp++;
                    }
                }
            }
            if (temp != 0){
                c[7 - temp ]++;//Because the more the same as the winning lottery ticket, the larger the count and the smaller the number of screens.
            }
        }
        for (int i = 0; i < 7; i++) {//Traverse the winning array and output the number of times of winning at one time
            System.out.print(c[i] + " ");
        }
    }
}

Key points of this topic:

  • The comparison between their own lottery and winning lottery, nested traversal.
  • It should be noted that the more lottery tickets you win, the smaller the number of winners (sixth prize. Fifth prize,,)

8. Statistics in Luogu p1554 Java dream

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[] a = new int[10];
        
        int count;
        for (int i = m; i <= n; i++) {
            int j = i;//To avoid the confusion of i value caused by loop, assign i to the new value j
            while (j != 0){//Operate on each number: find the remaining digits of each digital module 10, and then divide by ten to get the penultimate digit, and so on. Find out each bit of the data and compare 1 - 10. If it is the same, the data corresponding to the array will increase automatically;
                count = j % 10;
                if(count == 0)a[0]++;
                if(count == 1)a[1]++;
                if(count == 2)a[2]++;
                if(count == 3)a[3]++;
                if(count == 4)a[4]++;
                if(count == 5)a[5]++;
                if(count == 6)a[6]++;
                if(count == 7)a[7]++;
                if(count == 8)a[8]++;
                if(count == 9)a[9]++;
                j = j / 10;//Let each bit of the data be a bit, and then calculate the remainder to get the number on each bit
            }
        }
        //Traverse to get the number of each number in 0 - 9 contained in the section class data.
        for (int i = 0; i < 10; i++) {
            System.out.print(a[i] +" ");
        }
    }
}

Key points of this topic:

  • Through the operation of remainder and division by 10, the number on each bit of a data is obtained, judged and accumulated in the corresponding array.
  • When traversing the outermost layer i, re assign the value of i to other variables to prevent subsequent data changes from disturbing the original data.

9. Luogu p1614 Java: heartache of love and sorrow

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//There's n one unpleasant thing
        int m = sc.nextInt();//Continuous m pieces
        int[] a = new int[n];
        int sum = 0,count = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();//Assign stinging points to n unpleasant things
        }

        for (int i = 0; i < m; i++) {
            count += a[i];//Initialize an initial accumulation of m unpleasant things
        }

        for (int i = 0; i < n - m; i++) {//Traverse from 0 to n - m; Because the inner loop is m values starting from i to prevent the array from crossing the boundary
            for (int j = i; j <i + m; j++) {//Traverse the accumulated value of each i value and the following m values
                sum += a[j];
            }
            if(sum < count){//Judge to get the maximum value
                count = sum;
            }
            sum = 0;//Reset after each accumulation to facilitate the next use
        }
        System.out.println(count);
    }
}

Key points of this topic:

  • Pay attention to the selection of the start and end points of traversal to prevent the array from crossing the boundary.
  • Note the use of several intermediate variables.

10 Valley p2911 Java bowene bones G

AC Code:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s1 = sc.nextInt();
        int s2 = sc.nextInt();
        int s3 = sc.nextInt();
        int[] a = new int[16000];//Define an array and place the corresponding face and in the corresponding position in the array
        int maxx = 0,maxs = 0;

        //Traverse three faces and increase the value of the face and the corresponding array.
        for (int i = 1; i <= s1 ; i++) {
            for (int j = 1; j <= s2 ; j++) {
                for (int k = 1; k <= s3 ; k++) {
                    a[i + j + k]++;
                }
            }
        }
        //Because the sum of faces is continuous, from the minimum of three faces to the maximum of the sum of three faces
        for (int i = s1 + s2 + s3; i >= 3 ; i--) {
            if (a[i] >= maxx){//Look at the data in the face and the corresponding array, who is the largest
                maxx = a[i];
                // maxs = i;
            }
        }
        System.out.println(maxs);
    }
}

Key points of this topic:

  • We can define an array to store the accumulated results of each face, and then traverse the three groups of data. When the data of a single face is the same as the mo-i result, we will accumulate one in the number;
  • It is ingenious to define a large array to prevent cross-border and self increment directly in the corresponding face and place of the array.

11. Luogu p1161 Java turn on the light

AC Code:

The first one: use XOR to solve the problem

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(),t,result = 0;
        double a;
        for (int i = 0; i < n; i++) {//Traverse and input each set of data,
            a = sc.nextDouble();
            t = sc.nextInt();
            for (int j = 1; j <= t; j++) {//
                int x = (int)Math.floor(a*j);//[k] Represents the integer part of the real number kk. t lights are turned on or off in each operation.
                result ^= x;//When using XOR, the switch light is corresponding. After n operations, only one light is on, so using XOR can reach the last one
            }
        }
        System.out.println(result);
    }
}

Second: use arrays to solve problems

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		double a[] = new double[n];
		int t[] = new int[n];
		// input
		for(int i = 0;i<n;i++) {
			a[i] = scanner.nextDouble();
			t[i] = scanner.nextInt();
		}
		
		int x[] = new int[2000000];//For% 100% data, T ≤ 2000000
		// Set the status of the switch
		for(int i = 0;i<x.length;i++) {
			x[i] = 0;//It is closed at the beginning
		}
		
		// Change of switch state
		for(int i = 0;i<n;i++) {
			for(int j = 1;j<=t[i];j++) {
				if(x[(int) (a[i]*j)] == 1) {
					x[(int) (a[i]*j)] = 0;
				}else {
					x[(int) (a[i]*j)] = 1;
				}
			}
		}
		// Output results
		for(int i = 0;i<x.length;i++) {
			if(x[i] == 1) {
				System.out.println(i);
			}
		}
		
	}
}

Key points of this topic:

  • You can use the XOR idea to solve the problem. This may not be very clear about the children's shoes you just started. You can check it online. Note that the XOR part is the XOR on the binary.
  • In the learning array stage, the second scheme, marking method, is used to assign the light on value to 1; The closed equal value is assigned to 0; Finally, traverse the lamp with 1.

12. Luogu p5731 Java snake array

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] a = new int[n][n];
        int count = 1;
        int x = 0,y = 0;
        a[0][0] = 1;
        while(count < n * n){//The digital matrix with side length n is output, so the final count value is n*n, which is used as a judgment.
            //Increment right
            while (y + 1 < n && a[x][y + 1] == 0){//The next data to the right exists and the value is 0, indicating that it is not assigned
                a[x][++y] = ++count;
            }
            //Increasing downward
            while (x + 1 < n && a[x + 1][y] == 0){//ditto
                a[++x][y] = ++count;
            }
            //Increment left
            while (y - 1 >= 0 && a[x][y - 1] == 0){//ditto
                a[x][--y] = ++count;
            }
            //Increasing upward
            while (x - 1 >= 0 && a[x - 1][y] == 0){//ditto
                a[--x][y] = ++count;
            }
        }

        //Ergodic square matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%3d",a[i][j]);//Use printf to make each number three characters.
            }
            System.out.println();
        }
    }
}

Key points of this topic:

  • Loop + correct judgment of four directions. Note: don't cross the bounds of the array.
  • Each number takes up 3 characters when outputting.

13. Luogu p5732 Yanghui triangle

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] a = new int[n + 1][n + 1];//The data of the array starts from 1, so the array range + 1;
        //Assign the first and last values of each line to 1; The array starts with 1, which is convenient
        for (int i = 1; i <= n; i++) {
            a[i][1] = a[i][i]=1;
        }
        //The first two lines are copied at the same time as the first and last lines of each line, so start from the third line.
        if (n > 2){
            for(int i = 3;i <= n;i++){
                for(int j = 2;j <= i;j++)//Because a[i][1] and a[i][i] have been assigned values, the loop is 2~n-1
                    a[i][j] = a[i-1][j] + a[i-1][j-1];
            }
        }
        //Output the data of each line;
        for (int i = 1; i <= n ; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(a[i][j]  + " ");
            }
            System.out.println();//Line feed after internal loop traversal
        }
    }
}

Key points of this topic:

  • The first and last data in each row is 1,
  • When traversing from the third line, pay attention to the relationship between them and the starting and ending positions of array traversal.

14. Luogu p1789 Java torch (difficult to understand)

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//Square matrix boundary
        int m = sc.nextInt();//m torches
        int k = sc.nextInt();//k fluorites
        int[][] arr = new int[n + 3][n + 3];//The range definition of the array is a little larger. The following traversal does not need to consider that the array is out of bounds.
        if (n == 2) {//There may be no fluorite, but there must be a torch. So when n < = 2, there are no monsters.
            System.out.print("0");
        } else {
            //Traverse the torch and assign a value of 1 to the points within the torch range
            for (int i = 1; i <= m; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                x = x + 2;
                y = y + 2;
                for (int j = x - 2; j < x + 1; j++) {
                    for (int l = y - 2; l < y + 1; l++) {
                        arr[j][l] = 1;
                        arr[x - 3][y - 1] = 1;
                        arr[x + 1][y - 1] = 1;
                        arr[x - 1][y - 3] = 1;
                        arr[x - 1][y + 1] = 1;
                    }
                }
            }
            //When traversing the fluorite, assign a value of 1 to the points within the fluorite range
            for (int i = 1; i <= k; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                x = x + 2;
                y = y + 2;
                for (int j = x - 3; j <= x + 1; j++) {
                    for (int l = y - 3; l <= y + 1; l++) {
                        arr[j][l] = 1;
                    }
                }
            }
            int count = 0;//Count variable
            //Traverse the entire array. If the value of the elements in the array is 0, there will be monsters in the part where the torch and fluorite are not illuminated
            for (int i = 2; i < n + 2; i++) {
                for (int j = 2; j < n + 2; j++) {
                    if (arr[i][j] == 0) {
                        count++;
                    }
                }
            }
            System.out.print(count);
        }
    }
}


Key points of this topic:

  • Using the marking method, after defining the array, all the elements in the array will be initially assigned to 0, and the part illuminated by the torch and fluorite will be assigned to 1. Finally, the matrix will be traversed. The part of the array element with 0 is the part not illuminated, and monsters will appear.

  • Define the range of the array a little larger, regardless of whether the array is out of bounds or not. When we traverse, we can only traverse the part we want to use,

15. Luogu p1319 Java compression technology

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//Enter n;

        int middle = 0,b = 0, c, d = 0;//Define several variables,
        while(d < n * n){
            c = sc.nextInt();
            middle++;//The middle variable is used to exchange outputs 0 and 1
            for (int j = c; j > 0 ; j--) {
                if (b == n){//When each line reaches n, wrap the line and re assign the variable to 0
                    System.out.println();
                    b = 0;
                }
                //Alternate output 0 and 1
                if (middle % 2 == 1){//When sorting odd numbers
                    System.out.print("0");
                }else{//Even time
                    System.out.print("1");
                }
                b++;//Controls the number of output numbers per line
                d++;//Control while loop
            }
        }
        System.out.println();
    }
}

Key points of this topic:

  • How to control the output of the corresponding 0 when the sorting is odd and the output of the corresponding 1 when the sorting is even. There are many intermediate variables in this problem. Clear the clue when doing the problem.
  • Control line feed

16. Luogu p2615 Java magic square

AC Code:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] a = new int[n][n];
        int x = 0,y = n / 2;
        a[0][y] = 1;
        for (int i = 2; i <= n*n; i++) {
            if (x == 0 && y != n-1) {
                x = n-1;
                ++y;
            } else if (x != 0 && y == n-1) {
                --x;
                y = 0;
            } else if (x == 0 && y == n-1) {
                ++x;
            } else if (a[x-1][y+1] == 0) {
                --x;
                ++y;
            } else {
                ++x;
            }
            a[x][y] = i;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Key points of this topic:

  • Note that the title says that the magic square N must be an odd number (the actual measurement can not be an even number), so there is no need to detect.

  • Just move according to the specified requirements. You don't need to design your own algorithm.

17. Luogu p2141 Java abacus test

AC Code:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        int[] m = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = sc.nextInt();
            m[i] = 1;//Mark all functions of array m with 1
        }
        
        int count = 0;
        for(int i = 0; i < n; i++ ){
            for(int j = i + 1; j < n; j++){
                for(int k = 0; k < n; k++){
                    if(arr[i] + arr[j] == arr[k] && m[k] == 1){//Judge whether the data meets the conditions
                        count++;
                        m[k] = 0;//If the number meets the conditions, assign the corresponding position in the array to 0 to prevent the next calculation,
                    }
                }
            }
        }
        System.out.println(count);
    }
}

Key points of this topic:

  • Use the marking method to get the sum exactly equal to the other two (different) numbers in the set, and then mark this number to prevent the next calculation, such as 1 + 4 = 5; 2+3=5; If 5 is not marked, 5 will be accumulated twice.

Topics: Java Algorithm