题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
#include <stdio.h> #include<string.h> #define N 1000 int main() { char c[N], tmp; int dp[N]={0}, i = 0, j = 0, k = 0, len,cnt=0; scanf("%s",c); len = strlen(c); for (i = 0; i < len; i++) { for (j = i; j < len+1; j++) { if (c[j] == c[i]) { dp[i]++; if (dp[i] != 1) { for (k = j; k < len; k++) { c[k] = c[k + 1]; //所有元素前移 } j--; len = len - 1; } } } } k = 0; //冒泡排序 for(i=0;i<=len;i++) { for (j = 0; j <= len; j++) { if (dp[j] < dp[j + 1]) { tmp = c[j]; c[j] = c[j + 1]; c[j + 1] = tmp; //交换字符位置 k = dp[j]; dp[j] = dp[j + 1]; dp[j + 1] = k; //交换统计数量位置 } } } //再次冒泡排序排列相同出现次数字符 for (i = 0; i < len; i++) { for (j = 0; j <= len; j++) { if (dp[j] == dp[j + 1]) { if (c[j] > c[j + 1]) { tmp = c[j]; c[j] = c[j + 1]; c[j + 1] = tmp; } } } } for (i = 0; i < len; i++) { printf("%c", c[i]); } return 0; }