24h購物| | PChome| 登入
2011-12-17 10:03:40| 人氣2,654| 回應0 | 上一篇 | 下一篇

[UVA] 10789 - Prime Frequency

推薦 0 收藏 0 轉貼0 訂閱站台

  Prime Frequency 

Given a string containing only alpha-numerals (0-9, A-Z and a-z) you have to count the frequency (the number of times the character is present) of all the characters and report only those characters whose frequency is a prime number. A prime number is a number, which is divisible by exactly two different integers. Some examples of prime numbers are 2, 3, 5, 7, 11 etc.

Input 

epsfbox{p10789.eps}

The first line of the input is an integer T ( 0 < T < 201) that indicates how many sets of inputs are there. Each of the next T lines contains a single set of input.

The input of each test set is a string consisting alpha-numerals only. The length of this string is positive and less than 2001.

Output 

For each set of input produce one line of output. This line contains the serial of output followed by the characters whose frequency in the input string is a prime number. These characters are to be sorted in lexicographically ascending order. Here ``lexicographically ascending" means ascending in terms of the ASCII values. Look at the output for sample input for details. If none of the character frequency is a prime number, you should print `empty' (without the quotes) instead.

Sample Input 

3
ABCC
AABBBBDDDDD
ABCDFFFF

Sample Output 

Case 1: C
Case 2: AD
Case 3: empty


 
作法 : 找尋質數個數的字元

#include<stdio.h>
int main() {
    int prime[2002] = {0};
    int i, j, C = 0, t;
    prime[0] = prime[1] = 1;
    for(i = 2; i < 2002; i++)
        if(prime[i] == 0) {
            for(j = 2; i*j < 2002; j++)
                prime[i*j] = 1;
        }
    char s[2002];
    scanf("%d", &t);
    while(t--) {
        scanf("%s", s);
        int asci[128] = {0};
        for(i = 0; s[i]; i++)
            asci[s[i]]++;
        printf("Case %d: ", ++C);
        for(i = 0, j = 0; i < 128; i++)
            if(prime[asci[i]] == 0)
                printf("%c", i), j++;
        if(j == 0)    printf("empty");
        puts("");
    }
    return 0;
}

台長: Morris
人氣(2,654) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 136 - Ugly Numbers
此分類上一篇:[UVA] 1121 - Subsequence

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文