- #import <Foundation/Foundation.h>
- #import <stdio.h>
- int main() {
-
- int a = 0, b = 0, c = 0, d = 0 ,i = 0;
- char sentence[100] = "";
- puts("请输入\n");
- gets(sentence);
- while (sentence[i] != '\0') {
-
- switch (sentence[i]) {
- case 'A':
- a++;
- break;
-
- case 'B':
- b++;
- break;
-
- case 'C':
- c++;
- break;
-
- case 'D':
- d++;
- break;
-
- default:
- break;
- }
- i++;
- }
- //printf("%d%d%d%d\n",a,b,c,d);
- int count[4] = {a, b, c, d};// 做排序法要用到
- int k = 0;
- char letter[4] = "ABCD";// 输出对应的字母要用到
- int temp;
- char cemp;
- for (int j = 0; j<3; j++) {// 冒泡排序法
- for ( k = 0; k < 3; k++) {
- if (count[k] <= count[k + 1]) {// 如果小则向上(右)冒泡
-
-
- temp = count[k + 1];
- count[k + 1] = count[k];
- count[k] = temp;
-
-
- cemp = letter[k + 1];// 如果执行了冒泡,对应的字母也跟着换位置,因为abcd 对应ABCD。
- letter[k + 1] = letter[k];
- letter[k] = cemp;
- //printf("%c",letter[k]);
-
- }
- }
-
- }
- for (int l = 0; l < 4; l++) {// 输出结果
- printf("%c",letter[l]);
- printf("%d\n",count[l]);
- }
- return 0;
- }
复制代码 |