Xiao Ming playing games (Java)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/l870358133/article/details/102508088

Xiao Ming playing game Daguai upgrade, this game allows the player to choose from i-off consecutive hit off the j (i <= j), each played one off or deduction will reward gold coins. The number of gold coins known each level award or deduction, Bob should choose from a few off consecutive hit a few off, in order to get the most gold? Please note that Xiao Ming can only choose one, of course, no gain, then you can not vote.

Input formats:

Enter two lines:

The first row is the total number of n-clearance; second line is subtracted each level the number of coins or reward (reward integer, negative numbers subtraction)

Output formats:

Xiao Ming output maximum number of coins available.

Sample Input 1:

6
-1 23 -9 14 -2 -1

Output Sample 1:

28

Description: Xiao Ming hit the fourth selection in a row off from level 02 to get the total number of coins is 28, this is the choice of the best strategy in line with the rules.

Sample Input 2:

3
-1 -2 -3

Output Sample 2:

0

Description: Each level is negative, do not play.


 code show as below:

import java.util.*;
public class Main {
	static int max=0;
	static int n;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		int[] num = new int[n];
		for(int i=0;i<n;i++)
			num[i] = sc.nextInt();
		recursion(num, 0, 0, 0);
		System.out.println(max);
	}
	public static void recursion(int[] num, int i, int j, int Sum) {
		if(j<n) {
			int tmp1 = Sum+num[j];				//往后累加下一个关卡
			max = Math.max(max, tmp1);
			recursion(num, i, j+1, tmp1);
		}
		if(i<j) {
			int tmp2 = Sum-num[i];				//减去头上的关卡
			max = Math.max(max, tmp2);
			recursion(num, i+1, j, tmp2);
		}
	}
}

The question is not complicated, recursive method to do, just consider a little better, Xiao Ming to play all levels must be linked to no problem.

Guess you like

Origin blog.csdn.net/l870358133/article/details/102508088