Php permutation and combination of numbers 1 to 9 are all equal to 20

Source: Internet
Author: User

<? Php
Set_time_limit (0 );
/*
Function Description: huoqu_zhuhe ($ total, $ num_array, $ isone = 0)
Parameter description: $ total --- sum of several numbers;
$ Num_array ------- addend array: $ num_array = array (1, 2, 3, 4, 5, 6, 7, 8, 9), The addend that can be used;
$ Count: The sum of several numbers. It is not limited to zero.
$ Isone --- whether to use different numbers each time. The value 1 is 0. The default value is 1.

Return type: array. The number is a +-connected string: [0] => 3 + 8 + 9 [1] => 4 + 7 + 9

Test result: 1: the speed of the addend array is relatively small. If it is too large, it is slow; 2: each time you can use a different addend, the processing will be slow.
The following method is used: generate all possible sorting, filter duplicate sorting, and obtain a combination.
*/
Function huoqu_zhuhe ($ total, $ num_array, $ count = 0, $ isone = 1 ){
If (empty ($ num_array) |! Is_array ($ num_array )){
Echo 'error: an array of numbers required '; return false;
}
$ Feishu = 0;
For ($ I = 0; $ I <count ($ num_array); $ I ++ ){
If (! Is_numeric ($ num_array [$ I]) {
$ Feishu = 1;
Break;
}
}

If ($ Feishu = 1 ){
Echo 'error; the array must be a valid number '; return false;
}
$ Lian = $ num_array;
$ Savearr = array ();
While (! Empty ($ Lian )){
// Echo 1;
$ Newarr = array ();
$ K = 0;
For ($ I = 0; $ I <count ($ Lian); $ I ++ ){
$ Lianstr = $ Lian [$ I];
$ Arr = explode ('+', $ lianstr );

$ Nowhe = array_sum ($ ARR );
For ($ J = 0; $ j <count ($ num_array); $ J ++ ){
$ Savestr = $ lianstr. '+'. $ num_array [$ j];
If ($ isone = 1 & in_array ($ num_array [$ j], $ arr ))
Continue;
If ($ nowhe + $ num_array [$ j])> $ total)
Break;
Else if ($ nowhe + $ num_array [$ j]) ==$ total ){
$ Savearr [] = $ savestr;
} Else {
$ Newarr [$ k] = $ savestr; $ k ++;
}
} // End for ($ j = 0; $ j <count ($ num_array)
} // End for ($ I = 0; $ I
$ Lian = $ newarr;
} // End while (! Empty ($ lian ))

If ($ count! = 0 ){
Foreach ($ savearr as $ key => $ number ){
$ Savearr_count = count (explode ('+', $ number ));
If ($ count! = $ Savearr_count)
Unset ($ savearr [$ key]);
}
}

// Generate the combination part and filter duplicates. The two arrays take one as a reference to see if the other can match by moving.

$ Isguolu = array (); // select 0 for storing the corresponding id to get 1
For ($ I = 0; $ I <count ($ savearr); $ I ++ ){
$ Isguolu [] = 0;
} // Initialize all 0

For ($ I = 0; $ I <count ($ savearr); $ I ++ ){
$ Arr1 = explode ('+', $ savearr [$ I]);
$ Len1 = count ($ arr1 );
For ($ j = $ I + 1; $ j <count ($ savearr); $ j ++ ){
$ Arr2 = explode ('+', $ savearr [$ j]);
$ Len2 = count ($ arr2 );
If ($ len1! = $ Len2)
Continue;
If ($ isguolu [$ j] = 1)
Continue;
// Compare $ arr1 and $ arr2
$ Jishu = 0;
For ($ i1 = 0; $ i1 <count ($ arr1); $ i1 ++ ){
$ A = $ arr1 [$ i1];
$ Isyou = 0;

For ($ i2 = $ i1; $ i2 <count ($ arr2); $ i2 ++ ){
If ($ a = $ arr2 [$ i2]) {
$ Jishu ++;
$ Isyou = 1;
$ T = $ arr2 [$ i1];
$ Arr2 [$ i1] = $ arr2 [$ i2];
$ Arr2 [$ I2] = $ t;
Break;
}
} // End for ($ i2 = 0

If ($ isyou = 0) break;

} // End for ($ i1 = 0; $ i1 <count ($ arr1 );

If ($ jishu = $ len1) $ isguolu [$ j] = 1;

} // End for ($ j = $ I + 1;

} // End for ($ I = 0; $ I <count ($ savearr); $ I ++)
// Print_r ($ isguolu );

// Select based on the filter array
$ Newarr = array ();
For ($ I = 0; $ I <count ($ savearr); $ I ++ ){
If ($ isguolu [$ I] = 0) $ newarr [] = $ savearr [$ I];
}
// Print_r ($ newarr );
Return $ newarr;
}

// The following is a test
// Use 1, 2, 3, 4, 5, 6, 7, 8, 9 to add all the combinations equal to 20.
$ Num_array = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
If ($ jieguo = huoqu_zhuhe (20, $ num_array, 4 )){
Echo '<PRE> ';
Print_r ($ jieguo );
}
 

?>

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.