#UVa:10924-Prime Words

灆洢 2012-09-14 00:30:57

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

C++(0.009)

/*******************************************************/
/* UVa 10924 Prime Words                               */
/* Author: Maplewing [at] knightzone.studio            */
/* 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;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料