Home > Back-end >  A programming problem solving: zhang SAN has a magical instrument, can use any atoms synthesis of ne
A programming problem solving: zhang SAN has a magical instrument, can use any atoms synthesis of ne

Time:10-24

Zhang SAN has a magical instrument, can use any atomic synthesis of new molecular
Now a total of 10 kinds of selection of atoms. Each atom must use
1, 2, 3 timesThe total number of rules for the n atom, zhang want to know what can make the total different molecular
Input: an integer n, said new molecular total number of atoms
(10 & lt;=n<=30), said the new molecular total number of atoms.
Output: the first line of an integer m, represent different schemes for
Cut down m lines, each row of a solution, contains 10 integers, said the number of each atom
The sample input:
29
The sample output:
10
2, 3, 3 3 3 3 3 3 3 3
3 2, 3, 3 3 3 3 3 3 3
3 3 2, 3, 3 3 3 3 3 3
2 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3
23 3 3 3 3 3 3 3 3
23 3 3 3 3 3 3 3 3
23 3 3 3 3 3 3 2, 3, 3
3 3 3 3 3 3 3 3 2, 3,
3 3 3 3 3 3 3 3 and 2

CodePudding user response:

Permutation and combination problem, use the method of simulation base can
For example, the following from right to left, with 2 carry
111
112
121
122
211
212
221
222
See rule? Like binary carry 01, 001010011100101110111
So, find out each combination of the total number of meet the combination of the total number of atoms, such as the total number of atoms is 5, that is 122=1 + 2 + 2=5212=2 + 1 + 2=5221=2 + 2 + 1=5, 3 kinds of accord with
 public class Sample {
Public static void main (String [] args) {
Try {
Scanner sc=new Scanner(System.in);
Int n=sc. NextInt ();
Int elem=new int [] [10].//the number of each atom
The Arrays. The fill (elem, 1);
Int Max=n/10 + 1;
List Result=new ArrayList<> (a);
While (elem [0] <=Max) {
int sum=0;
for(int i=0; i<10; I++) sum +=elem [I];//the total number of atoms
If (sum==n) {//atomic number and total n equal to save the results
Result. The add (Arrays. ToString (elem). ReplaceAll ("/\ \ [\ \], ", ""));
}
Elem [9] + +;//the total number of atoms change
For (int I=9. I> 0; I -) {
If (elem [I] & gt; Max) {
Elem [I - 1] + +;
Elem [I]=1;
}
}
}
System. The out. Println (result. The size ());
For (String s: result) {
System.out.println(s);
}

} the catch (Throwable e) {
e.printStackTrace();
}
}
}

CodePudding user response:

Thank you big!!!!!!!!!!
  • Related