[UVA] 10789 - Prime Frequency
Prime Frequency
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
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;
}