Hdu 1455 Sticks (Classic search)

Source: Internet
Author: User

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; int n, map [70], v [70]; int sum; // The length of all wood rods and int total, length, flag; bool cmp (int x, int y) {return x> y;} // num indicates the number of wood sticks, and len indicates the length of the wood sticks, pos indicates the position of the stick void dfs (int num, int len, int pos) {if (num = total) {flag = 1; return ;}if (flag) return; for (int I = pos; I <n; I ++) {if (v [I]) continue; if (map [I] + len = length) {v [I] = 1; dfs (num +, 0); // a wood rod is successfully combined. num + 1 v [I] = 0 ;} else if (map [I] + len <length) {v [I] = 1; dfs (num, map [I] + len, I + 1 ); v [I] = 0; if (len = 0) return;/* if the length of the front edge is 0 during the current search, and the first root fails to be used, this indicates that the first root is always discarded, so this combination will not succeed */while (map [I] = map [I + 1]) // if this does not meet the requirements, the same length does not meet the requirements. Skip I ++ ;}} int main () {int I; while (scanf ("% d", & n) & n) {sum = 0; for (I = 0; I <n; I ++) {scanf ("% d", & map [I]); sum + = map [I];} memset (v, 0, sizeof (v); sort (map, map + n, cmp ); // sort the sticks from the upper to the lower for (length = map [0]; length <= sum; length ++)/* search for the sticks with the longest length, because the minimum length of the combined wooden rod is greater than or equal to the longest root */{if (sum % length) continue; // if it cannot be divisible, it cannot constitute an integer. The root stick flag = 0; // mark total = sum/length; // The length after the combination with length, total indicates the number of wood sticks obtained dfs (, 0); if (flag) {printf ("% d \ n", length); break ;}} return 0 ;}

 

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.