Problem Description:
Please write a program that prints the following characters:
(a) (b) (c). (z) (A,B) (a,c) ... (a,z) ... (B,C) (b,d) ... (b,z) ... (y,z) ... (A,B,C) (a,b,d) ... (a,b,z) ... (x,y,z) ... (A,B,C,D...Z)
Problem Analysis:
Just start to see this problem, feel very complex, there seems to be no idea, can only think of recursion, but how to write it.
But after a careful analysis, we can divide the above characters into the following categories:
There is only one character: (a), (b), (c) ....
There are two characters: (A,b), (a,c) ... (b,d) ... (y,z)
......
There are 26 characters: (A,B,C...Z)
A closer look at each situation, is not to ask (A-Z) combination of numbers. So the question becomes simple.
Specific code:
1 #include <stdio.h>
2 #include <stdlib.h>
3
4CharArray[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5
6 #define N 26
7
8//a Simple Queue
9intQueue[n] = {0};
10inttop = 0;
11
//output the combination of array, whose letter size is CNT.
13voidCombination (intStartintCnt
14 {
15intI
16
//reach the end of an array, return
18if(Start > N)
19 return;
20
//the queue has CNT letters, output it
22if(top = CNT)
23 {
printf ("{");
25 for(I =0 i < cnt-1; i++)
26 {
printf ("%c,", Queue[i]);
28}
printf ("%c}", Queue[i]);
30 return;
31}
32
queue[top++] = Array[start];
Combination (start+1, CNT);
top--;
Combination (start+1, CNT);
37}
38
39voidMagic_print ()
40 {
41intI
42
43 for(I =1 i <=; i++)
44 {
printf ("--------------cnt =%d------------------\ n", i);
Combination (0, i);
GetChar ();
48}
49}
50
51intMain ()
52 {
Magic_print ();
54 return0;
55}
56
57
58