Recursion, enumeration

Source: Internet
Author: User

1. Hanoi Tower Problem

#include <stdio.h>voidHanint,Char,Char,Char );intMain () {intN; CharA='A', b='B', c='C'; scanf ("%d",&N); Han (n,a,b,c);}voidmovCharACharBB) {printf ("%c=>%c\n", A, b);}voidHanintNCharACharBCharc) { if(n==1) mov (A,C); Else{han (n-1, a,c,b); MOV (A,C); Han (n-1, B,a,c); }}

2. Conversion problems in the system

Enter two numbers, one representing the existing number of eg, and another that represents the few binary ch to convert. Use the EG mode ch and save it with an array.

#include <stdio.h>intMain () {inta[10000]; intEg,ch; inti,j; scanf ("%d%d",&eg,&ch);  for(i=0; i<10000; i++) A[i]=0;  for(i=0; i<10000; i++) {A[i]=eg%ch; Eg=eg/ch; }     for(i=9999; i>-1; i--)        if(a[i]!=0) Break;  for(J=i; j>-1; j--) printf ("%d", A[j]);}

3. Eight Queen's question

Define a a[8] array to save, determine if the condition is met, a[j]==i| available | i-t==a[j]-j| | T+i==a[j]+j

motioned as follows

0-1-2-3-4-5-6-7
1 0-1-2-3-4-5-6
2 1 0-1-2-3-4-5
3 2 1 0-1-2-3-4
4 3 2 1 0-1-2-3
5 4 3 2 1 0-1-2
6 5 4 3 2 1 0-1
7 6 5 4 3 2 1 0

0 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10 11
5 6 7 8 9 10 11 12
6 7 8 9 10 11 12 13
7 8 9 10 11 12 13 14

As long as you judge I-t==a[j]-j and T+i==a[j]+j, then proceed to the next column.

#include <stdio.h>inta[8];intCnt=0;voidEtinta[8],intt) {    inti,j; if(t==8) cnt++; Else    {         for(i=0; i<8; i++)        {            intok=1; A[t]=i;  for(j=0; j<t; J + +)            {                if(a[j]==i| | i-t==a[j]-j| | t+i==a[j]+j) {OK=0;  Break; }            }            if(OK) et (a,t+1); }    }}intMain () {ET (A,0); printf ("%d", CNT);}

4. Two points Search

Press semi-search to

#include <stdio.h>a[Ten]={6,8, -, -, $, $, the, -, About,102};intMain () {intErintTintUpintDown ); intt,ant; scanf ("%d",&t); Ant=er (T,9,0); printf ("%d", ant);}intErintTintUpintDown ) {    intm;  while(up!=Down ) {m=down+ (Up-down)/2; if(a[m]==t)returnm+1; if(a[m]<t) down=m+1; Else up=m; }    return-1;}

5. Enumeration arrangement

According to the eight Queen's method, the following

A[1000] is used to save the result, the IF statement within the PL function determines whether it is marginal, else is used to loop 1 to N and backtrack.

#include <stdio.h>inta[ +];intN;intMain () {voidPlintTinta[ +]); scanf ("%d",&N); PL (0, a);}voidPlintTinta[ +]){    inti,j; if(t==N) { for(i=0; i<n;i++) printf ("%d", A[i]); printf ("\ n"); }    Else{         for(j=1; j<=n;j++)        {            intK; intok=1;  for(k=0; k<t;k++)            {                if(A[K]==J) {ok=0; Break;} }            if(OK) {A[T]=J;PL (t+1, a);} }    }}

6, Match stick problem

Direct enumeration, first enumerate the first two, calculate the third, judge the three required match stick number of sum plus 4 is equal to N, can.

#include <stdio.h>intnum[]={6,2,5,5,4,5,6,3,7,6};intCnt=0;intDeintd) {    intsum=0; if(d==0)return 6;  while(d) {sum+=num[d%Ten]; D=d/Ten; }    returnsum;}intMain () {intN; inti,j; scanf ("%d",&N);  for(i=0;i< +; i++)    {         for(j=0;j< +; j + +)        {            if(De (i) +de (j) +de (I+J) +4==N) CNT++; }} printf ("%d", CNT);}

7

Recursion, enumeration

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.