当前位置:网站首页>Research on balloon problem

Research on balloon problem

2022-06-26 14:09:00 fe11953264

subject

Give you a string text, You need to use text To piece together as many words as possible “balloon”( balloon ).

character string text Each letter in can only be used once at most . Please return the maximum number of words you can piece together “balloon”.

About c

Realization of balloon problem

Find the number of each letter in the given string for each target string , Divide the number of letters in the target string by the number of letters in the target string , The last decimal is the number of balloons

Code

#define min(a , b) ((a) < (b) ? (a) : (b))

int maxnumberofballoons( char * text)
{
    
	int cnt[5];
	int n = strlen (text);
	memset(cnt , 0 , sizeof(int)*5);
	for (int i = 0 ; i < n ; i++ )
	{
    
    	if (text[ i ] == 'b' )
    		cnt[ 0 ]++;
    	else if (text[ i ] == 'a' )
    		cnt[ 1 ]++;
    	else if (text[ i ] == 'l' )
    		cnt[ 2 ]++;
    	else if (text[ i ] == 'o' )
    		cnt[ 3 ]++;
    	else if (text[ i ] == 'n' )
    		cnt[ 4 ]++;
    }
    cnt[ 2 ] /= 2;
    cnt[ 3 ] /=2;
    int res = int_max;
    for (int i = 0 ; i < 5 ;i++)
    	res=min( res, cnt[ i ] );
    return res;
}

Key code

Find the length of the string
strlen(text)
C++ Medium constant INT_MAX and INT_MIN Each represents the maximum 、 Minimum integer , Definition in header file limits.h in .

#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
 int res = int_max;

memset: The function is to fill a given value in a block of memory , It is the fastest way to clear a large structure or array

memset(cnt , 0 , sizeof(int)*5);
原网站

版权声明
本文为[fe11953264]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170510349726.html