int main(int argc, const char * argv[]) {
//1.实现输入
char str[100] = {0};
printf("请输入字符");
gets(str);
//2.实现计数
// 定义一个数组,记录ABCD出现的次数
int times[4] = {0};
for (int i = 0 ; i < strlen(str); i++)
{
char c = str[i];
if (c=='A')
{
times[0]++;
}
else if(c=='B')
{
times[1]++;
}
else if(c=='C')
{
times[2]++;
}
else if(c=='D')
{
times[3]++;
}
}
int jiaoBiao[4] = {0,1,2,3};
int temp = -1;
for (int i = 0; i < 3; i++) {
for (int j = 0 ; j < 3-i; j++) {
if (times[j] < times[j+1]) {
temp = times[j];
times[j] = times[j+1];
times[j+1] = temp;
temp = jiaoBiao[j];
jiaoBiao[j] = jiaoBiao[j+1];
jiaoBiao[j+1] = temp;
}
}
}
for (int i = 0; i<4; i++)
{
printf("%c出现了%d次\n",jiaoBiao[i]+65,times[i]);
}
return 0;
}
|
|