Title Description
Write a function, passing in an array of int, to return whether the array can be divided into two groups, so that the elements in the two groups are added together and equal, and that all multiples of 5 must be in one group, and all multiples of 3 in another group (not including a multiple of 5), can satisfy the above conditions, return True ; returns false when not satisfied.
Input Description:
The first line is the number of data, the second row is the input data
Output Description:
Returns TRUE or False
Input Example:
41 5-5 1
Output Example:
True
1 ImportJava.util.*;2 3 Public classMain46 {4 Public Static voidMain (string[] args) {5Scanner sc =NewScanner (system.in);6 while(Sc.hasnext ()) {7 intn =sc.nextint ();8 intsum1 = 0, sum2 = 0;9 int[] A =New int[n];Ten intCount = 0; One for(intI =0;i<n;i++){ A intTMP =sc.nextint (); - if(tmp%5==0){ -sum1+=tmp; the } - Else if(tmp%3==0) -Sum2 + =tmp; - Else{ +a[count++] =tmp; - } + } A intsum = Math.Abs (sum1-sum2);//only the absolute values are considered here. atSystem.out.println (f (0,count,a,0, sum)); - } - } - - Public Static BooleanFintIintNint[] A,intResultintsum) { - if(i==N) { in returnMath.Abs (Result) = = sum;//The absolute value is equal to - } to Else{ + return(f (i+1,n,a,result+a[i],sum) | | F (i+1,n,a,result-a[i],sum)); - } the } *}
This recursive function behind, think Ah, some people say this is ksum problem, have time to study.
201301 Java topic level 0-1