#UVa:10924-Prime Words

建質數表及使用ASCII碼即可得解。

C++(0.009)

/*******************************************************/
/* UVa 10924 Prime Words                               */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/09/13                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;

int main(){
  bool composite[1100] = {true, false, false};
  string L;
  int total;

  for( int i = 2 ; i < 1100 ; i++ )
    if( !composite[i] )
      for( int j = i+i ; j < 1100 ; j += i )
        composite[j] = true;

  while( getline( cin, L ) ){
    total = 0;
    for( int i = 0 ; i < L.length() ; i++ )
      if( isupper(L[i]) )
        total += L[i]-'A'+27;
      else if( islower(L[i]) )
        total += L[i]-'a'+1;

    if( composite[total] ) printf( "It is not a prime word.\n" );
    else printf( "It is a prime word.\n");
  }
  return 0;
}

發表迴響