2011 NetEase Company Pen Test

Source: Internet
Author: User
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

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.