FJ的字符串(字符串)

191 阅读1分钟

FJ的字符串

问题描述

  FJ在沙盘上写了这样一些字符串:
A1  =  “A”
A2  =  “ABA”
A3  =  “ABACABA”
A4  =  “ABACABADABACABA”
…  …
你能找出其中的规律并写所有的数列AN吗?
输入格式
仅有一个数:N  ≤  26。
输出格式
请输出相应的字符串AN,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。
样例输入
3
样例输出
ABACABA

字符串处理写法:

#include<stdio.h>
#include<string.h>
char str[10010],s[10010],s1[10];
 
int main()
{
    int i,j,n,t=1;
    scanf("%d",&n);
    s[0]='A';
    s[1]='\0';
    strcpy(str,s);
    while(n>1)
    {
        strcpy(str,s);
        s1[0]='A'+t;
        s1[1]='\0';
        t++;
        strcat(str,s1);
        strcat(str,s);
        strcpy(s,str);
        n--;
    }
    printf("%s\n",str);
    return 0;
}

递归写法:

#include<stdio.h>
#include<string.h>
void F(int n);
int main()
{
    int n;
    scanf("%d",&n);
    F(n);
    printf("\n");
    return 0;
}
void F(int n)
{
    if(n==1)
    {
        printf("A");
        return;
    }
    F(n-1);
    printf("%c",'A'+n-1);
    F(n-1);
}