The 8th Blue Bridge Cup_Question 5_letter string


Title: Alphabet

Strings A, B, C can form many strings.
For example: "A", "AB", "ABC", "ABA", "AACBB" ....

Now, Xiao Ming is thinking about a problem:
if the number of each letter is limited, how many known lengths can be formed What about the string?

He asked a good friend to help and got the code quickly, the
solution was super simple, but the most important part was vague.

Please analyze the source code carefully and fill in the missing content in the underlined part.

#include <stdio.h>

// How many different strings of length n can be formed by a letter A, b letter B, and c letter C.
int f(int a, int b, int c, int n)
{
    if(a<0 || b<0 || c<0) return 0;
    if(n==0) return 1;
    
    return ______________________________________ ; // Fill in the blanks
}

int main()
{
    printf("%d\n", f(1,1,1,2));
    printf("%d\n", f(1,2,3,3));
    return 0;







Note: Only fill in the missing code in the underlined part, do not submit any extra or explanatory text.

解答:f(a-1,b,c,n-1)+f(a,b-1,c,n-1)+f(a,b,c-1,n-1)

Remarks: f(a-1,b,c,n-1) is to arrange a first, and then arrange n-1 for the remaining numbers;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326015240&siteId=291194637