数据范围:字符串长度满足
数据范围:字符串长度满足
一个只包含小写英文字母和数字的字符串。
一个字符串,为不同字母出现次数的降序表示。若出现次数相同,则按ASCII码的升序输出。
aaddccdc
cda
样例里,c和d出现3次,a出现2次,但c的ASCII码比d小,所以先输出c,再输出d,最后输出a.
#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"); } }