201301 Java topic level 0-1

Source: Internet
Author: User
Tags abs

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

Related Article

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.