Problem Description
FJ in the sand table to write some string like this:
A1 = "A"
A2 = "ABA"
A3 = "ABACABA"
A4 = "ABACABADABACABA"
... ...
you can find out the rules and write them all columns AN it?
A1 = "A"
A2 = "ABA"
A3 = "ABACABA"
A4 = "ABACABADABACABA"
... ...
you can find out the rules and write them all columns AN it?
Input Format
Only a few: N ≤ 26.
Output Format
Please output corresponding string AN, to a newline character. Output must not contain extra space or line feed, carriage return.
Sample input
3
Sample Output
Spearheading
Thinking: Examples of the title is observed, are found each having a string symmetry, n = 1, A is the center of symmetry to it, n = 2, B is a center of symmetry on, n = 3, it is the center of symmetry with C ...... symmetrical and each time, are made on both sides of a string consisting of symmetry, so use a 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) //递归函数 14 { 15 if(n==1) 16 { 17 cout<<"A " ; 18 is } . 19 the else 20 is { 21 is recursion (N- . 1 ); 22 is T = ' A ' + N- . 1 ; 23 is COUT << T; 24 recursion (N- . 1 ); 25 } 26 is } 27 Private : 28 int n-; 29 char T; // for each recursive control output 30 }; 31 is 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