题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <stdio.h> #include <string.h> typedef struct record { char ch; int num; }record; int main() { char ch; char input_str[21]; int str_len = 0; record re_str[21]; int re_len = 0; int i = 0; int sig = 0; int min = 0; int j; memset(input_str, 0, sizeof(input_str)); memset(re_str, 0, 21 * sizeof(struct record)); while ((ch = getchar()) != '\n') { if (str_len > 19) { break; } input_str[str_len] = ch; str_len++; for (i = 0; i < re_len; i++) { if (re_str[i].ch == ch) { re_str[i].num++; sig = 1; } } if (!sig) { re_str[re_len].ch = ch; re_str[re_len].num = 1; re_len++; } sig = 0; } min = re_str[0].num; for (i = 0; i < re_len - 1; i++) { if (re_str[i + 1].num < min) { min = re_str[i + 1].num; } } for (i = 0; i < str_len; i++) { for (j = 0; j < re_len; j++) { if ((input_str[i] == re_str[j].ch) && (min != re_str[j].num)) { printf("%c", input_str[i]); } } } return 0; }