題目連結: https://zerojudge.tw/ShowProblem?problemid=d117
# 解題思路
輸入的部分用字串,將各位的值轉換為數字,最後再判斷加總的值是否為質數即可
# 程式碼
#include <bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
string s; | |
while(cin>>s){ | |
int sum=0; | |
for(int i=0;i<s.size();i++){ | |
if(s[i]>='A'&&s[i]<='Z') sum+=s[i]-'A'+27; | |
else if(s[i]>='a'&&s[i]<='z') sum+=s[i]-'a'+1; | |
} | |
bool prime = true; | |
for(int i=2;i<=sqrt(sum);i++){ | |
if(sum%i==0){ | |
cout<<"It is not a prime word."<<endl; | |
prime = false; | |
break; | |
} | |
} | |
if(prime)cout<<"It is a prime word."<<endl; | |
} | |
} |