Using C + + to realize: FJ string printing

Keywords: C++ C

Problem description
FJ writes the following strings on the sandbox:
  A1 = "A"
  A2 = "ABA"
  A3 = "ABACABA"
  A4 = "ABACABADABACABA"
  ... ...
Can you find out the rules and write all the series AN?
Input format
Only one number: N ≤ 26.
Output format
Please output the corresponding string AN, ending with a line break. The output must not contain extra spaces or line breaks or carriage returns.
sample input
3
sample output
ABACABA
 
Thinking: observe the examples in the questions, and find that every string has symmetry, n=1, take A as the symmetry center, n=2, take B as the symmetry center, n=3, take C as the symmetry center... And every symmetry is composed of the last string to form the two sides of symmetry, so use recursive function to solve this problem.
 
 1 #include<iostream>
 2 using namespace std;
 3 
 4 class print_string
 5 {
 6 public:
 7     int get_n()
 8     {
 9         cin>>n;
10         return n;
11     }
12 
13     void recursion(int n)    //Recursive function
14     {
15         if(n==1)
16         {
17             cout<<"A";
18         }
19         else
20         {
21             recursion(n-1);
22             t='A'+n-1;
23             cout<<t;
24             recursion(n-1);
25         }
26     }
27 private:
28     int n;
29     char t;        //Used to control the output of each recursion
30 };
31 
32 int main(void)
33 {
34     print_string x;
35     int a;
36     a=x.get_n();
37     x.recursion(a);
38     return 0;
39 }

 

Looking at a code written in C language:

 1 # include <stdio.h>
 2 
 3 int main()
 4 
 5 {
 6 
 7     int i,j;
 8 
 9     char c[50][1000];
10 
11     int n;
12 
13     char cc='A';
14 
15     int count=1;
16 
17     int temp;
18 
19     scanf("%d", &n);
20 
21     c[1][1] = 'A';
22 
23     c[1][2] = '\0';
24 
25     for (i=2; i<=n; i++)
26 
27     {
28 
29         temp = count;
30 
31         count = count*2+1;
32 
33         for (j=1; c[i-1][j]!='\0'; j++)
34 
35         {
36 
37             c[i][j] = c[i-1][j];
38 
39             c[i][j+temp+1] = c[i-1][j];
40 
41             
42 
43         }
44 
45         c[i][temp+1] = ++cc;
46 
47         c[i][j+temp+1] = '\0';
48 
49     }   
50 
51     
52 
53     for (i=1; c[n][i]!='\0'; i++)
54 
55         printf("%c", c[n][i]);
56 
57     printf("\n");
58 
59             
60 
61     return 0;
62 
63 }

Original link: https://blog.csdn.net/a237653639/article/details/21323641

Posted by chrislive on Sat, 04 Apr 2020 16:01:01 -0700