题目大意\(:\)给你一个单词,每个单词仅由\(a-z\)和\(A-Z\)范围内的字母构成。对于每个字母,赋予其一个值,\(a->1,b->2……z->26,A->27,B->28……Z->52.\)对于一个单词,它的价值为各个字母的值的和,若它的价值是一个素数,那么输出\("It~is~a~prime~word."\)否则输出\("It~is~not~a~prime~word."\)
按照题意模拟即可,将这\(52\)个字母用\(map\)映射成数字,对于每个单词,求出它的价值,判断是否为素数即可。
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
using namespace std;
map<char, int>rhs; //将char映射为int
bool is_prime(int a) //判素数
{
for (int i = 2; i <= sqrt(a); i++)
if (a % i == 0) return false;
return true;
}
int main()
{
for (int i = 97; i <= 122; i++)
rhs[char(i)] = i - 96;
for (int i = 65; i <= 90; i++)
rhs[char(i)] = i - 38; //映射
string s;
while (cin >> s) {
int temp = 0;
for (int i = 0; i <= s.length(); i++)
temp += rhs[s[i]];
if (is_prime(temp))
puts("It is a prime word.");
else puts("It is not a prime word.");
}
return 0;
}
完结撒花(っ•̀ω•́)っ✎⁾⁾