Huawei OJ Primary: 201301 JAVA topic level 0-1 __java

Source: Internet
Author: User

Describe

Write a function, pass in an int array, returns whether the array can be divided into two groups, so that the elements of the two groups are combined and equal, and that all multiples of 5 must be in one group, all multiples of 3 in another group (excluding a multiple of 5), which satisfies the above conditions and returns true , false when not satisfied.

Knowledge points strings, loops, functions, pointers, enumerations, bitwise operations, structures, associations, file operations, recursion
Run time limit 10M
Memory Limits 128
Input

Enter the number of data entered

Enter an array of int


Output

Returns TRUE or False


Sample input 4 1 5-5 1
Sample output True
Import java.util.ArrayList;

Import Java.util.Scanner;
		public class main{public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in);
		int length = Scanner.nextint ();
		int[] numbers = new Int[length];
		for (int i = 0; i < length; i++) {Numbers[i] = Scanner.nextint ();
		System.out.print (Result (numbers));

	Scanner.close ();   private static Boolean result (int[] numbers) {int numMultiple5 = 0;   The number of all multiples of 5 and int numMultiple3 = 0;  The number of all multiples of 3 and arraylist<integer> ArrayList = new arraylist<integer> (); The number of stores that are not multiples of 3 and 5 for (int i = 0; i < numbers.length i++) {if (Numbers[i]% 5 = 0) NumMultiple5 + = Numbers[i] ;  All multiples of 5 add else if (Numbers[i]% 3 = 0 && numMultiple5% 5!= 0) NumMultiple3 + = Numbers[i];  The multiples of all 3 are added else Arraylist.add (Numbers[i]);
		Storage is not multiples of 3 and 5}//Definition J, used as the number of pointers to traverse ArrayList int j = 0;
		boolean bool = Check (NumMultiple3, NumMultiple5, ArrayList, J);
	return bool;

}	private static Boolean check (int sum3, int sum5, arraylist<integer> list, int j) {if (J >= list.size ())/
		/When J is greater than list.size (), indicating that the list has already traversed the return sum3 = = SUM5;  else {//traversal, which will not be multiples of 3 and 5, added to the previously stored numMultiple3 and numMultiple5, returns the result of the final addition comparison return check (Sum3 + list.get (j), SUM5, list, J + 1) | | 
		Check (sum3, SUM5 + list.get (j), List, J + 1);
 }
	}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.