Home > database >  The question of combination algorithm and circular manner
The question of combination algorithm and circular manner

Time:09-25

Has N compounds, each material has a value, the value of N substances, the sum is equal to 100, is the value of N substance, how many kinds of change
For example: N=3, the material, respectively. A, B, C
The first group (A=1, B=1, C=98 second set A=1, B=2, C=97 such A, A, B, C value is equal to the sum of 100, how many kinds of accessor methods, how to achieve in the program? Since N is uncertain, this item cannot be fixed is three kinds of material, can't use three layers of circulation, with N, N uncertain substances out of the results, thank you first,

CodePudding user response:

2 d array structure is used to deal with dynamic,,,

CodePudding user response:

A=1, B=1, C=98
With the
A=98, B=1, C=1
Calculate 1 or two groups

CodePudding user response:

Dealing with this calculation, it is estimated that can cause data overflow errors,

CodePudding user response:

Using recursive
/====================================================================
//function: w.w f_digui ()
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//description: recursive output qualified combination (in the case of not consider numerical repeat)
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//parameters:
//value long al_sum//material the sum of the value of the
//value string as//so far, has been a combination of Numbers, such as: 1, 2, 3
//value long al_num//material species N
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//return: (None)
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//====================================================================
Long ll [], I, j, k
Long ll_deal
String ls
//to take the number of or will be taken and to 0, it returns
If al_num=0 or al_sum=0 then return

If al_num=1 then//the last one, it is al_sum value, and the output
If the as="' then
Ls=string (al_num)
The else
Ls=as + ', '+ string (al_sum)
End the if
Il_count + +
If mle_1. Text & lt;> "' then mle_1. Text +='~ r ~ n'
Mle_1. Text +=string (il_count) + + ls' ~ t: '
Return
End the if

//starting from 1 cycle until al_sum - 1 ()
For j=1 to al_sum - 1
Taking 1//in a loop number, and add the Numbers to the back of the "has combined the Numbers"
If the as="' then
Ls=string (j)
The else
Ls=as + ', '+ string (j)
End the if
Wf_digui (al_sum - j, ls, al_num - 1)
Next





Calls the method is as follows:
//instance variables, said the total number of combinations
Il_count=0
//parameter 1: material the sum of the value of the
2://parameter out of digital string prefix
//parameter 3: material species N
Wf_digui (5, ' ', 3)

CodePudding user response:

This problem convert a point of view is easy to understand, this is putting 100 goals in N baskets, each at least 1 ball basket, how many kind of placement problem,

The analysis process is as follows:
First one each basket ball, ensure minimum requirements, and then the remaining 100 - N, remaining the balls, each with N kind of placement method,

The answer is:
N ^ (100 - N)

Don't write code an

CodePudding user response:

This is the problem of algorithm!

CodePudding user response:

Dingding!!!!!!!!!!!!!!

CodePudding user response:

A post every day, to help

CodePudding user response:

dingding

CodePudding user response:

Three layers of circulation, nested loops in the loop

CodePudding user response:

N certain circumstances, can be used to the tenth floor of multilayer circulation.

N uncertain, generally using a recursive more convenient.

Pb can't directly to save a direct a recursive function. (also is unable to write a f (), followed by direct in f () and call f ())

You must save a f (), don't write the code inside, direct RETURN.

Save and then write the real code (it must be saved before a, can call itself)
  • Related