The following program is my ACcepted code for UVA-10924 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UVA 10924 的AC code!
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw)
If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UVA 10924 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This program is for UVa 10924 Prime Words | |
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=21&page=show_problem&problem=1865 | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#define MAX 1050// the max value is 20 * 52 = 1040 | |
bool isprime[MAX]; | |
void testPrime(){ | |
for(int i = 1; i < MAX; i++) | |
if(isprime[i]) | |
printf("%d ", i); | |
printf("\n"); | |
} | |
void calPrime(){ | |
memset(isprime, true, sizeof(isprime)); | |
for(int i = 2; i < MAX; i++){ | |
if(isprime[i]){ | |
for(int j = 2; j * i < MAX; j++){ | |
isprime[j*i] = false; | |
} | |
} | |
} | |
} | |
int decode(char ch){ | |
if(ch >= 'a' && ch <= 'z') | |
return ch - 'a' + 1; | |
else if(ch >= 'A' && ch <= 'Z') | |
return ch - 'A' + 27; | |
} | |
int main(){ | |
calPrime(); | |
//testPrime(); | |
char str[21]; | |
int len, sum; | |
while(scanf("%s", str) != EOF){ | |
sum = 0; | |
len = strlen(str); | |
for(int i = 0; i < len; i++) | |
sum += decode(str[i]); | |
if(isprime[sum]) | |
printf("It is a prime word.\n"); | |
else | |
printf("It is not a prime word.\n"); | |
} | |
return 0; | |
} |
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!