在一行上输入一个长度为
、由小写字母和数字构成的字符串
。
在一行上输出一个字符串,代表按频次统计后的结果。
aaddccdc
cda
在这个样例中,字符
和
出现的次数最多,均为
次,而前者 ASCII 码更小,因此先输出。
#include <stdio.h> #include <string.h> //定义哈希表元素的结构体 typedef struct hash { char letter; int quantity; } harsh; void insert_sort(harsh arr[], int len); void print_hash(harsh arr[], int len); int main() { char arr[1001]; int m = 0, n = 0; harsh hash[36]; //定义哈希数组 char letter_t = 'a'; for (int i = 0; i < 26; i++) { hash[i].letter = letter_t; hash[i].quantity = 0; letter_t ++; } //给哈希表初始化:字符为英文字符,个数为0. letter_t = '0'; for (int i = 26; i < 36; i++) { hash[i].letter = letter_t; hash[i].quantity = 0; letter_t ++; } //给哈希表继续初始化:字符为字符0-9,个数为0. scanf("%s", arr); arr[strcspn(arr, "\n")] = '\0'; //将输入的字符串统计字符数量并写入哈希表 while (arr[m] != '\0') { for (int n = 0; n < 36; n++) { if (hash[n].letter == arr[m]) { hash[n].quantity++; break; } } m++; } insert_sort(hash, 36); //排序哈希表 print_hash(hash, 36); //打印哈希表 return 0; } void insert_sort(harsh arr[], int len) { for (int i = 1; i < len; i++) { int temp_quantity = arr[i].quantity; char temp_letter = arr[i].letter; int j = i; // 根据数量降序和字母升序进行排序。当数量相等时,比较字符顺序。 while (j > 0 && (arr[j - 1].quantity < temp_quantity || (arr[j - 1].quantity == temp_quantity && arr[j - 1].letter > temp_letter))) { arr[j].quantity = arr[j - 1].quantity; arr[j].letter = arr[j - 1].letter; j--; } arr[j].quantity = temp_quantity; arr[j].letter = temp_letter; } } void print_hash(harsh arr[], int len) { for (int i = 0; i < len; i++) { if (arr[i].quantity != 0) { printf("%c", arr[i].letter); } } }
#include <stdio.h> #include <string.h> int main() { int i, j, max = 0; char arr[1000]; char count[256] = {0}; gets(arr); int len = strlen(arr); for (i = 0; i < len; i++) { count[arr[i]]++; } while (1) { for (i = 1; i < 256; i++) { if (count[i] > count[max]) { max = i; } else if (count[i] == count[max]) { if (i < max) { max = i; } } } if (count[max] > 0) { count[max] = 0; }else{ break; } printf("%c", max); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char a; int num; }list; int cmp(list *a,list *b) { return b->num-a->num; } int main() { list dict[37]; char str[1001]={}; scanf("%s",str); for(int i=0;i<36;i++)//初始化 { if(i<10) {dict[i].a=i+'0';dict[i].num=0;} else {dict[i].a=i-10+'a';dict[i].num=0;} } for(int i=0;i<strlen(str);i++) { if(str[i]>='a' && str[i]<='z') dict[str[i]-'a'+10].num++; else dict[str[i]-'0'].num++; } qsort(dict,36,sizeof(list),cmp); for(int i=0;i<36;i++) { if(dict[i].num!=0) printf("%c",dict[i].a); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int maxnum=0,tempmax=-1; int num[37]={0}; void max(int a[], int len) { for(int i=0; i<len; i++) { if(a[i]>maxnum){ maxnum = a[i]; tempmax = i; } } if(tempmax<10 && tempmax>=0) printf("%c", tempmax+'0'); else if(tempmax >= 10) printf("%c", tempmax - 10 + 'a'); a[tempmax] = 0; if(tempmax >= 0){ maxnum=0;tempmax=-1; max(a, len); } else { return; } } int main() { char input[1000]={0}; scanf("%s", input); int len=0; len = '0' - 'a'; for(int i=0; i<strlen(input); i++) { if(islower(input[i])) num[input[i] - 'a' + 10] ++; else if(isdigit(input[i])) num[input[i] - '0'] ++; } max(num, 37); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void* a, const void* b) { return *(int*)b - *(int*)a; } int main() { //对照组 char sdb[36]; for (int i = 0; i < 10; i++) { sdb[i] = '0' + i; } for (int i = 10; i < 36; i++) { sdb[i] = 'a' + i - 10; } char str[1200]; int nm[36] = {0}; int lenth = 0; int tmp[36] = {0}; if (scanf("%s", str) != EOF) { lenth = strlen(str); for (int i = 0; i < lenth; i++) { for (int j = 0; j < 36; j++) { if (str[i] == sdb[j]) { nm[j]++; } } } for (int i = 0; i < 36; i++) { tmp[i] = nm[i]; } qsort(tmp, 36, sizeof(int), cmp); for (int i = 0; i < 35; i++) { if ((tmp[i] == tmp[i + 1]) && tmp[i] != 0) { tmp[i] = 0; } } for (int i = 0; i < 36; i++) { for (int j = 0; j < 36; j++) { if (nm[j] > 0 && tmp[i] > 0) { if (tmp[i] == nm[j]) printf("%c", sdb[j]); } } } printf("\n"); } return 0; }
#include <stdio.h> #include <string.h> int findMax(int Arr1[], int Arr2[]) { int max = 0; for (int i = 0; i < 10; i++) { if (Arr1[i] > max) { max = Arr1[i]; } } for (int i = 0; i < 26; i++) { if (Arr2[i] > max) { max = Arr2[i]; } } return max; } int main() { char buffer[1024]; int cntNumArr[10] = {0}; int cntCharArr[26] = {0}; int len; scanf("%s", buffer); len = strlen(buffer); for (int i = 0; i < len; i++) { if ((buffer[i] >= '0') && (buffer[i] <= '9')) { cntNumArr[buffer[i] - '0'] ++; } if ((buffer[i] >= 'a') && (buffer[i] <= 'z')) { cntCharArr[buffer[i] - 'a'] ++; } } int max; while (max = findMax(cntNumArr, cntCharArr)) { for (int i = 0; i < 10; i++) { if (max == cntNumArr[i]) { printf("%d", i); cntNumArr[i] = 0; } } for (int i = 0; i < 26; i++) { if (max == cntCharArr[i]) { printf("%c", 'a'+i); cntCharArr[i] = 0; } } } return 0; }
#include <stdio.h> #define N 36 #define M 1000 int find_max(int arr[]) { int i,max=0; for(i=1;i<N;i++) { if(arr[max]<arr[i]) max=i; } if(arr[max]>0) { arr[max]=0; return max; } else return -1; } int main() { int arr[N]={0},i=0,max; char str[M]; scanf("%s",str); while(str[i]!='\0') { if(str[i]>='0'&&str[i]<='9') arr[str[i]-'0']++; else arr[str[i]-'a'+10]++; i++; } for(i=0;i<N;i++) { max=find_max(arr); if(max>=0&&max<=9) printf("%c",max+'0'); else if(max>=10&&max<=35) printf("%c",max-10+'a'); } return 0; }
C 一直嵌套 觉得不错点个赞 #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { char arr[1001]={0}; int cnt[128]={0}; int snt[128]={0}; int i,j; scanf("%s",arr); int len=strlen(arr); for(i=0;i<len;i++) { cnt[arr[i]]++; } for(i=0;i<128;i++) { if(cnt[i]!=0) snt[cnt[i]]=1; } for(i=len;i>0;i--) { if(snt[i]==1) { for(j='0';j<='z';j++) { if(cnt[j]==i) printf("%c",j); } } } return 0; }
/* 思路:用一个表count记录次数,项目按ascii顺序,前10个记录数字次数,后26个记录小写字母 再用一个表ch存放0-9和a-z,与count位置一一对应 然后对count和ch表一起采用稳定的排序方法降序排序 最后只输出ch表中count不为0的部分 复杂度:冒泡排序时间复杂度O(n^2); 只有数字和小写字母的不变长度表,空间复杂度O(1) */ #include <stdio.h> #include <string.h> #include <ctype.h> void bubbleSort(int count[36], char ch[36]) { int temp; int flag = 1; for(int i=0; i<35-1; i++){ flag = 1; for(int j=35; j>i; j--){ if( count[j] > count[j-1] ){ temp = count[j]; count[j] = count[j-1]; count[j-1] = temp; temp = ch[j]; ch[j] = ch[j-1]; ch[j-1] = temp; flag = 0; } } if(flag){ break; } } } int main(void) { char in[1000]; while(scanf("%s", in) != EOF){ int count[36] = {0}; char ch[36] = "0123456789abcdefghijklmnopqrstuvwxyz"; for(int i=0; i<strlen(in); i++){ if( isdigit(in[i]) ){ count[ in[i]-'0' ]++; }else if( isalpha(in[i]) ){ count[ in[i]-'a'+10 ]++; } } bubbleSort(count, ch); for(int i=0; i<36; i++){ if(count[i] > 0){ printf("%c", ch[i]); } } putchar('\n'); } return 0; }
#include<stdio.h> #include<string.h> #include<stdlib.h> #define Size 1000 typedef struct table{ char *elem; int *count; }table; void swapch(char *a,char *b){ char t=*a; *a=*b; *b=t; } void swapint(int *a,int *b){ int t=*a; *a=*b; *b=t; } int main() { char str[Size]={'\0'}; while(scanf("%[^\n]",str)!=EOF){ scanf("%*[^\n]");scanf("%*c"); table t; int len=36; t.elem=(char*)malloc(len*sizeof(char)); t.count=(int*)malloc(len*sizeof(int)); for(int i=0;i<10;i++){ t.elem[i]=i+48; t.count[i]=0; } for(int i=10;i<len;i++){ t.elem[i]=i+87; t.count[i]=0; } for(int i=0;i<len;i++){ for(int j=0;j<strlen(str);j++){ if(t.elem[i]==str[j]){ t.count[i]++; } } } for(int i=0;i<len;i++){ for(int j=i+1;j<len;j++){ if(t.count[i]<t.count[j]){ swapch(&t.elem[i],&t.elem[j]); swapint(&t.count[i],&t.count[j]); } } } for(int i=0;i<len;i++){ for(int j=i+1;j<len;j++){ if(t.count[i]==t.count[j]){ if(t.elem[i]>t.elem[j]){ swapch(&t.elem[i],&t.elem[j]); swapint(&t.count[i],&t.count[j]); } } } } for(int i=0;i<len;i++){ if(t.count[i]!=0){ printf("%c",t.elem[i]); } } printf("\n"); free(t.elem); free(t.count); for(int i=0;i<strlen(str);i++){ str[i]='\0'; } } return 0; }
// hash + qsort排序 #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { char c; int count; } HashMap; HashMap g_hashMap[36] = {0}; int g_index = 0; int GetIndex(char c) { for (int i = 0; i < sizeof(g_hashMap) / sizeof(HashMap); i++) { if (g_hashMap[i].c == c) { return i; } } return -1; } void InsertHashMap(char c) { int i = GetIndex(c); if (i >= 0) { g_hashMap[i].count++; return; } g_hashMap[g_index].c = c; g_hashMap[g_index].count = 1; g_index++; } int Cmp(const void *p1, const void *p2) { HashMap *a = (HashMap *)p1; HashMap *b = (HashMap *)p2; if (a->count != b->count) { return b->count - a->count; } else { return a->c - b->c; } } int main(void) { char str[1024] = {'\0'}; while (gets(str)) { memset(g_hashMap, 0, sizeof(g_hashMap)); g_index = 0; for (int i = 0; i < strlen(str); i++) { InsertHashMap(str[i]); } qsort(g_hashMap, g_index, sizeof(HashMap), Cmp); for (int i = 0; i < g_index; i++) { printf("%c", g_hashMap[i].c); } printf("\n"); } }