Java Blue Bridge postfix expression

Given N plus, M minus sign, and N + M + 1 integers A1, A2, · · ·, AN + M + 1, small
that would like to know all of the N plus, M minus sign in and N + M + is a legitimate integer Couchu the
suffix expression, the result of which is the largest?
Please output the maximum result.
For example, 123 + -, the "23 + 1 -> 4 The postfix a result, is the largest.
[Input format
The first line contains two integers N and M.
The second line contains N + M + 1 integers A1, A2, · · ·, AN + M + 1 ] [output format
output an integer representing the answer.
Sample input] [
. 1. 1
. 1 2. 3
[output] Sample
4
Analysis: calculated first digital arranged, and M is the number of strong adding the remainder of N + 1 are summed, and finally subtraction available;
code is as follows:
Import java.util.Scanner;
public class CXL {
public static void main (String [] args) {
int N = 0, M = 0, SUM = 0;
Scanner Scanner new new INPUT = ( the System.in);
N = input.nextInt ();
M = input.nextInt ();
int a[]=new int [N+M+1];
for(int i=0;i<a.length;i++) {
a[i]=input.nextInt();
}
sum=sta(a,N,M);
System.out.println(sum);

}
static int sta(int a[],int N,int M) {
	int c,sumN=0,sumM=0,sum=0;
	for(int i=0;i<a.length-1;i++) {
		for(int j=i+1;j<a.length;j++) {
			if(a[i]>a[j]) {
				c=a[i];
				a[i]=a[j];
				a[j]=c;
			}
		}
	}
	for(int i=0;i<M;i++) {
		sumM=sumM+a[i];
	}
	for(int i=M;i<a.length;i++) {
		sumN=sumN+a[i];
	}
	sum=sumN-sumM;
	return sum;
}

}

Please also indicate if there is insufficient come, thank you! ! !

Published 13 original articles · won praise 0 · Views 123

Guess you like

Origin blog.csdn.net/vi_to/article/details/104727932