UVa 10924 Prime Words
質數題大屠殺的時候解的
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
/* | |
* UVa 10924 Prime Words (AC) | |
* Author: chchwy | |
* Last Modified: 2009.11.22 | |
*/ | |
#include<iostream> | |
#include<sstream> | |
#include<cctype> | |
using namespace std; | |
enum {MAX = 52 * 20 + 2}; | |
char p[MAX]; //0=prime, 1=not prime | |
void shieve() | |
{ | |
p[0] = p[1] = 0; | |
for (int i = 2; i * i < MAX; ++i) | |
{ | |
if (p[i] == 1) continue; | |
for (int j = i * i; j < MAX; j += i) | |
p[j] = 1; | |
} | |
} | |
int main() | |
{ | |
shieve(); | |
string line; | |
while (getline(cin, line)) | |
{ | |
int sum = 0; | |
//for each char | |
for (int i = 0; i < line.length(); ++i) | |
{ | |
if ( islower(line[i]) ) | |
sum += line[i] - 'a' + 1; | |
else | |
sum += line[i] - 'A' + 27; | |
} | |
if (p[sum] == 0) | |
puts("It is a prime word."); | |
else | |
puts("It is not a prime word."); | |
} | |
return 0; | |
} | |
留言
張貼留言