题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
#include <stdio.h> #include <stdlib.h> #include "string.h" int cmp(const void* p1, const void* p2) { return (*(char*) p1) - (*(char*)p2); } struct ListNode { struct ListNode* next; char a; }; typedef struct { struct ListNode* node_string; int count_string; } state_string; state_string res_nums[1000] = {0}; void add_node(state_string* node_string, struct ListNode* node , int nums ) { if ( node_string->count_string == 0 ) { node_string->count_string = nums; node_string->node_string = node; } else { struct ListNode *head =node_string->node_string; while (head->next != NULL) { head = head->next ; } head->next = node; } } int main() { char nums[1000]; scanf("%s", nums); int len = strlen(nums); qsort(nums, len, sizeof(char), cmp); for (int i = 0; i < len; i++) { // 从1开始计数 int count = 1; while (nums[i] == nums[i + 1]) { count++; i++; } struct ListNode* pdata = malloc(sizeof(struct ListNode)); pdata->next = NULL; pdata->a = nums[i]; add_node( &res_nums[count], pdata, count); } for (int i = 1000; i >=0; i--) { if ( res_nums[i].count_string !=0) { struct ListNode* pre =res_nums[i].node_string; // 这里循环检查下一个是否为空 while( pre != NULL) { printf("%c", pre->a); pre = pre->next; } } } }