Blue Bridge practice field constantly encountered similar problems, are a recursive search routines. Algorithm to increase the number of permutations time limit: 1.0s memory limit: 256.0MBProblem description 0, 1, 23 digits of the full array of six, according to alphabetical order as follows:
012, 021, 102, 120, 201, 210
Enter a number n
0~9 the nth (1th 0123456789) in the full array of 10 numbers. An input format line containing an integer n output format row containing a set of 10 numbers of the full array sample input 1 sample output 0123456789 data size and conventions 0 < n <= 10! Author's note: The standard recursive search question, which is now a routine.
1#include <stdio.h>2#include <string.h>3 intn,sum=0; 4 BOOLuse[Ten];//used to mark whether the number I was used, that is, whether it has been placed in the arrangement5 inta[Ten];6 voidDfsintbegin) { 7 if(begin==Ten){//indicates that there are 10 numbers in the current array a8sum++; 9 if(sum==N) {Ten for(intI=0; i<Ten; i++) Oneprintf"%d", A[i]); A } - } - for(intI=0; i<=9; i++) {//Enumeration number 0 to number 9 the if(!Use[i]) { -use[i]=true; -A[begin]=i;//the first element of the array is 0 -DFS (begin+1); +use[i]=false; - } + } A } at intMain () { -memset (use,false,sizeof(use)); -scanf"%d",&n); - if(n==0){ - return 0; - } inDfs0); - return 0; to}
Algorithm improves arrangement time limit: 1.0s memory limit: 256.0MBThe problem description 7254 is an unusual number, because it can be expressed as 7254 = 186, in this equation 1~9 each number appears exactly once
Output all such different formulas (the multiplier Exchange is considered to be the same formula)
The result is small first output, the result is the same, the smaller multiplier is the smaller the first output. Output format each line output a formula, the equation before and after the space, multiplication sign (with the letter X represents) before and after the space
The smaller multiplier written in the previous sample output problem will appear in the result line as follows:
7254 = 186 x Method One: Violent enumeration chant, pay attention to the conditions of judgment. (Run timeout)
1#include <stdio.h>2 voidMeiju () {//Problem Solving function3 intCount=0, m,n,x;4 intp,q; 5 for(intA=1; a<Ten; a++)6 for(intb=1; b<Ten; b++) 7 for(intC=1; c<Ten; C++) 8 for(intD=1; d<Ten; d++) 9 for(intE=1; e<Ten; e++) Ten for(intf=1; f<Ten; f++) One for(intg=1; g<Ten; g++) A for(intI=1; i<Ten; i++) - for(intj=1; j<Ten; J + +){ - //guaranteed 1-9 only one occurrence the if(a!=b&&a!=c&&a!=d&&a!=e&&a!=f&&a!=g&&a!=i&&a!=j& &b!=c&&b!=d&&b!=e&&b!=f&&b!=g&&b!=i&&b!=j&&c!=d &&c!=e&&c!=f&&c!=g&&c!=i&&c!=j&&d!=e&&d!=f&&d! =g&&d!=i&&d!=j&&e!=f&&e!=g&&e!=i&&e!=j&&f!=g&& f!=i&&f!=j&&g!=i&&g!=j&&i!=j) { -m=a* ++b* -+c*Ten+D; -n=e*Ten+F; -x=g* -+i*Ten+J; +p=e; -q=f* ++g* -+i*Ten+K; + if(m==n*x) { Acount++; atprintf"%d=%dx%d\n", m,n,x); - } - if(m==p*q) { -count++; -printf"%d=%dx%d\n", m,p,q); - } in } - } toprintf"there are%d types. ", count); + } - intMain () { the Meiju (); * return 0; $}
Method Two: Recursive search
1#include <stdio.h>2#include <string.h>3 intsum=0; 4 BOOLuse[Ten];5 inta[Ten];6 voidDfsintbegin) { 7 if(begin==9){//indicates that there are 9 numbers in the current array a8 intnum1=a[0]* ++a[1]* -+a[2]*Ten+a[3];9 intnum2=a[4]*Ten+a[5];Ten intnum3=a[6]* -+a[7]*Ten+a[8]; One intnum4=a[4]; A intnum5=a[5]* ++a[6]* -+a[7]*Ten+a[8]; - if(num1==num2*num3) { -printf"%d =%d x%d\n", num1,num2,num3); the } - if(num1==num4*NUM5) { -printf"%d =%d x%d\n", NUM1,NUM4,NUM5); - } + return; - } + for(intI=1; i<=9; i++) {//enumeration number 1 to number 9 A if(!Use[i]) { atuse[i]=true; -A[begin]=i;//the first element of the array is 0 -DFS (begin+1); -use[i]=false; - } - } in } - intMain () { tomemset (use,false,sizeof(use)); +Dfs0);//for array A, a search starts at the 1th position - return 0; the}
C language · Number of permutations · Arrangement type