朱色虫居
Pages
Home
Featured Posts
2006/09/26
10789-Prime Frequency
/*
"Prime Frequency"
Level:3.0
Date:2006/9/25
技巧: 製作一質數表, mask掉出現過的完元, bubble sort質數set.
*/
#include
#include
#define N 2001
#define MAX_CHAR 256
int prime[303]={0};
void getPrime(void);
main(){
int n=0,round=1,len=0,i=0,j=0,k=0,z=0,count=0;
char temp;
char input[N+1]={};
char set[MAX_CHAR]={}; //記錄出現的字元集合set.
bool check[N+1]={false};
getPrime();
scanf("%d",&n);
while(n-- > 0 ){
scanf("%s",&input);
len = strlen(input);
printf("Case %d: ",round++);
for(i=0;i < len;i++){ if(check[i]) //被mask掉的字元,便跳過不檢查. continue; temp=input[i]; count=1; for(j=i+1;j < len;j++){ if(temp==input[j]){ check[j]=true; //將相同的字元mask掉. count++; //累計字元出現的次數. } } while(count >= prime[k]){
if(count == prime[k++]){ //如果字元出現次數是質數,便加入set.
set[z++] = input[i];
}
}
k=0;
}
set[z]='\0';
if(*set == '\0'){
printf("empty\n");
}
else{
len = strlen(set);
for(i=0;i < len;i++){ //依ASCII做bubble sort. for(j=i+1;j < len;j++){ if(set[i] > set[j]){
temp = set[i];
set[i]=set[j];
set[j]=temp;
}
}
}
for(i=0;i < len;i++){ printf("%c",set[i]); } set[z=0]='\0'; printf("\n"); } for(i=0;i < N;i++) check[i]=false; } } void getPrime(void){ int n[N+1]={0},i=0,j=0; for(i=2;i*i <= N;i++){ for(j=i*2;j <= N;j++){ if(j%i == 0) n[j]=1; // 1,if n[j] is not a prime. } } j=0; for(i=2;i < N;i++){ if(n[i] == 0){ prime[j++] = i; //Collect all of prime. } } }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment