題目(連結):
Cryptanalysis is the process of breaking someone else’s cryptographic writing. This sometimes involves some kind of statistical analysis of a passage of (encrypted) text. Your task is to write a program which performs a simple analysis of a given text.
思路:
本題目主要目的是要記錄輸入字串中所有的英文符號的使用次數.
並且依照次數最高的顯示有使用到的字母與其使用次數.
由於已知英文字母最多為26個字,因此事先建置一個26格的一維陣列.
只需將輸入的英文字母轉換成陣列索引值即可進行每個字母的使用次數統計.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> #include <stdlib.h> int main(){ int n; scanf("%d",&n);//輸入第一個數字代表有幾行字串 char temp; char an[26]={0}; int i,idx,max=0,count=-1; while(count<n){ //如果換行計數等於輸入的n則結束迴圈 temp=getchar();//將讀入的字元處理判斷並將適當的字元+1 if(temp=='\n') //如果讀到換行則 count++; //計數+1 else{ //將字母轉換成陣列索引 if(temp<='z'&&temp>='a') idx = temp-'a'; else if(temp<='Z'&&temp>='A') idx = temp-'A'; else continue; //針對指定索引進行計數 an[idx]+=1; if(an[idx] > max) max = an[idx]; } } /*顯示*/ for(idx = max ; idx > 0 ; idx--) for(i=0;i<26;i++){ if(an[i] == idx) printf("%c %d\n",('A'+i),an[i]); } return 0; } |
文章標籤
全站熱搜
留言列表