#53649: 解題報告


013570jack@gmail.com (宋佳曄)

學校 : 不指定學校
編號 : 162888
來源 : [203.69.221.204]
最後登入時間 :
2022-01-06 15:23:04

//Tell me the frequencies!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
    char c[1000];
    while(fgets(c, sizeof(c), stdin)){
        int a[128]={0};
        int first;
        for(int i = 0; c[i]!='\0'; i++){
            for(int j = 32; j<=127; j++){
                if(c[i]==j){
                    a[j] = a[j]+1;
                }
            }
        }

        int chars[128], count = 0;
        for(int k=32; k<128; k++){
            if(a[k]>0){
                chars[count++]=k;
            }
        }

        for(int i = 0;i<count;i++){
            for(int j=0;j<count-1-i;j++){
                if(a[chars[j]]>a[chars[j+1]] || (a[chars[j]]==a[chars[j+1]] && chars[j]<chars[j+1])){
                    int temp = chars[j];
                    chars[j]=chars[j+1];
                    chars[j+1]=temp;
                }
            }
        }
        for(int i =0;i<count;i++){
            printf("%d %d\n",chars[i],a[chars[i]]);
        }

        if(!first)printf("\n");
        first = 0;
       
    }
    return 0;
}