这里有一个字符串 S,由于 "a" 是一个可爱的字符,我们希望 "a" 在字符串 S 中数量超过一半,这样的字符串 S 是我们想要的。
比如,"aaabb"、"axaa" 是我们想要的字符串,而 "baca"、"awwwa"、""(空串)不是我们想要的。
为了获得我们想要的字符串,可以从 S 中删除某些字符。那么在删除之后,我们想要的串最长可以保留多少个字符。 //题目要求
模糊知识点:1. 判断字符串的长度;
用#include<string.h>头文件的num = strlen();函数判断字符串的长度;
2. 统计一个字符串中的某个字符的数量
用字符数组储存字符串,然后用循环从0到num-1的数组下标与需要判别的字符比较;
#include<stdio.h>
#include<string.h>
int main()
{
char ch[50];
int num;//记录输入数组的字母个数;
int count = 0;//记录字符串中'a'的个数;
int i,half;
scanf("%s", ch);
num = strlen(ch);
for(i = 0;i < num;i++)
{
if(ch[i] == 'a')
count++;
}
half = num / 2;
if(count > half)
printf("%d",num);
else
{
while(num / 2 >= count)
num--;
printf("%d\n", num);
}
}