|
发表于 2023-11-25 10:49:07
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- int main() {
- char str[10000];
- int i, num = 0, max = 0, ch[256];
- while (fgets(str, sizeof(str), stdin)) {
- int a = strlen(str);
- memset(ch, 0, sizeof(ch)); // 初始化字符计数数组
- for (i = 0; i < a; i++) {
- if (str[i] != ' ' && str[i] != '\n') {
- ch[str[i]]++;
- }
- }
- num = 0, max = 0;
- for (i = 0; i < 256; i++) {
- if (ch[i] > max) {
- max = ch[i];
- num = i;
- } else if (ch[i] == max && i < num) {
- num = i;
- }
- }
- printf("%c %d\n", num, max);
- }
- return 0;
- }
复制代码 |
|