- //从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最后出现次数由高到低输出字母和出现次数。
- #warning 根据题目的文字,没有很明白题目要我做什么。。。我做的是按从高到低的顺序输出ABCD的次数
-
- char str[1024];
- gets(str);
-
- int i = 0;
- int countA = 0;
- int countB = 0;
- int countC = 0;
- int countD = 0;
- //计算ABCD出现的个数
- while (str[i] != '\0') {
-
- if(str[i] == 'A')
- {
- countA++;
- }
- else if(str[i] == 'B')
- {
- countB++;
- }
- else if(str[i] == 'C')
- {
- countC++;
- }
- else if(str[i] == 'D')
- {
- countD++;
- }
-
- i++;
- }
-
- printf("%i, %i, %i, %i\n", countA, countB, countC, countD);
-
- //分别将字母和次数放到数组中,他们的关系是对应的。并且再排序的时候他们的位置都是要变得。
- int count[4] = {countA, countB, countC, countD};
- char cc[4] = {'A', 'B', 'C', 'D'};
- for(int i = 0; i<3; i++)
- {
- if(count[i] < count[i+1])
- {
- int temp = count[i];
- count[i] = count[i + 1];
- count[i + 1] = temp;
-
- char tempC = cc[i];
- cc[i] = cc[i + 1];
- cc[i + 1] = tempC;
- }
- }
-
- for(int i = 0; i<4; i++)
- {
- printf("%c : %i\n", cc[i], count[i]);
-
复制代码 |
|