- #include <stdio.h>
- void maopao(int a[],int len){
- int temp = 0;
- for (int i=0; i<len-1; i++) {
- for (int j=0; j<len-1-i; j++) {
- if (a[j]<a[j+1]) {
- temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- }
- }
- }
- }
- int main(int argc, const char * argv[]) {
- char s[100];
- printf("请输入字符串:");
- scanf("%s",s);
-
- int i=0;//延长生命周期
-
-
- int countA = 0;
- int countB = 0;
- int countC = 0;
- int countD = 0;
- while (s[i]!='\0') {
- if (s[i]=='A') {
- countA++;
- }else if (s[i]=='B'){
- countB++;
- }else if (s[i]=='C'){
- countC++;
- }else if(s[i]=='D'){
- countD++;
- }
- i++;
- }
- int a[4] = {countA,countB,countC,countD};
- maopao(a, 4);
- for (int i=0; i<4; i++) {
- if (a[i]==countA) {
- printf("A出现的次数是%d\n",a[i]);
- }else if (a[i]==countB){
- printf("B出现的次数是%d\n",a[i]);
- }else if (a[i]==countC){
- printf("C出现的次数是%d\n",a[i]);
- }else if(a[i]==countD){
- printf("D出现的次数是%d\n",a[i]);
- }
- }
- return 0;
- }
复制代码
|
|