# UVa 10789 ### 題目連結:[UVa10789](http://domen111.github.io/UVa-Easy-Viewer/?10789) ### 題述: 給你一個只有數字及英文字母(``0 - 9``、``A - Z`` 及 ``a - z``)的字串,你需要計算每一個字元出現的頻率,並且判斷哪一個字元出現的頻率是 **``質數``**。所謂的質數是指可以被 1 和自己整除的數,例如: 2 、 3 、 5 、 7 、 11 ...。 --- 輸入的第一列有一個整數 T ( 1 < T < 201 ),代表以下有多少組測試資料。 每組測試資料一列。每組測試資料只有數字及英文字母,測試資料的長度大於 0 且 **小於 ``2001``**。 --- 對於每組測資請輸出一列,內容是出現頻率是質數的字元;輸出的順序必需依照 ASCII 值由小到大排列。請參考 ``sample input`` 及 ``sample output``。 如果**沒有**任何字元的出現頻率是質數,請輸出 **``empty``** ### c++ code: ```cpp= #include<bits/stdc++.h> #include<memory.h> using namespace std; // 定義在全域 -> bool 初始為 false bool prime[2002] ; int main () { //------------------------------------------------ #ifdef local freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //------------------------------------------------ //---------------建質數表(埃式篩法)--------------- prime[0] = prime[1] = true ; for ( int i = 2 ; i < 2002 ; i++ ) { if ( !prime[i] ) { for ( int j = i * i ; j < 2002 ; j += i ) { prime[j] = true ; } } } //------------------------------------------------ int n = 1 ; int m ; cin >> m ; int count[127] ; int ifempty ; string a ; while ( m != 0 ) { cin >> a ; cout << "Case " << n << ": " ; n += 1 ; memset ( count , 0 , sizeof(count) ) ; ifempty = 0 ; for ( int i = 0 ; i < a.size() ; i++ ) { count[ (int)a[i] ] += 1 ; } for ( int i = 48 ; i < 127 ; i++ ) { if ( count[i] != 0 && count[i] != 1 && prime[count[i]] == false ) { cout << (char)i ; ifempty += 1 ; } } if ( !ifempty ){ cout << "empty" ; } cout << endl ; } } ``` :::success **``sample input``** 3 ABCC AABBBBDDDDD ABCDFFFF ::: :::success **``sample output``** Case 1: C Case 2: AD Case 3: empty ::: #### [返回首頁](https://hackmd.io/@fkleofk/APCS#10789) ###### tags: `APCS選修` `C++` `UVa`