- 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]);
- }
-
复制代码
|
|