给定一个数字N ,任务是根据以下条件将给定的数字转换为 Cypher字符串:
- 如果N是半素数,则将 N 偶数位的每个数字更改为其对应的匹配字母表,如下所示。
- 如果N可以写成两个素数之和,则将 N 奇数位置的每个数字更改为其对应的匹配字母表,如下所示。
- 如果两个条件都满足concatenate形成的上述两个字符串。
- 如果 N 不能满足以上三个条件,则打印“-1”。
以下是匹配字符列表:
例子:
Input: N = 61
Output: 6B
Explanation:
Since 61 can be expressed as a sum of two primes: 61 = 2 + 59
Therefore, the resultant string after changing the character at even index is “6B”.
Input: N = 1011243
Output: B0B1C4D
Explanation:
Since 1011243 is Semiprime number: 1011243 = 3 * 337081
Therefore, the resultant string after change the character at even index is “B0B1C4D”.
方法:
- 使用本文中讨论的方法检查给定数字N是否为半素数。如果是,则执行以下操作:
- 使用 to_string()函数将给定的数字N转换为字符串(比如str )。
- 遍历上面形成的字符串并将偶数索引处的字符更改为:
str[i] = char((str[i] - '0') + 65)
- 打印形成的新字符串。
- 使用本文中讨论的方法检查给定的数字N 是否可以表示为两个素数的和。如果是,则执行以下操作:
- 使用 to_string()函数将给定的数字N转换为字符串(比如str )。
- 遍历上面形成的字符串并将奇数索引处的字符更改为:
str[i] = char((str[i] - '0') + 65)
- 打印形成的新字符串。
- 如果以上两个条件不满足,就不能形成 Cypher String。打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to check whether a number
// is prime or not
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
bool isPossibleSum(int N)
{
// If the N && (N-2) is Prime
if (isPrime(N)
&& isPrime(N - 2)) {
return true;
}
else {
return false;
}
}
// Function to check semiPrime
bool checkSemiprime(int num)
{
int cnt = 0;
// Loop from 2 to sqrt(num)
for (int i = 2; cnt < 2
&& i * i <= num;
++i) {
while (num % i == 0) {
num /= i,
// Increment the count of
// prime numbers
++cnt;
}
}
// If num is greater than 1, then
// add 1 to it
if (num > 1) {
++cnt;
}
// Return '1' if count is 2 else
// return '0'
return cnt == 2;
}
// Function to make the Cypher string
void makeCypherString(int N)
{
// Resultant string
string semiPrime = "";
string sumOfPrime = "";
// Make string for the number N
string str = to_string(N);
// Check for semiPrime
if (checkSemiprime(N)) {
// Traverse to make Cypher string
for (int i = 0; str[i]; i++) {
// If index is odd add the
// current character
if (i & 1) {
semiPrime += str[i];
}
// Else current character is
// changed
else {
semiPrime
+= char(
str[i] - '0' + 65);
}
}
}
// Check for sum of two primes
if (isPossibleSum(N)) {
// Traverse to make Cypher string
for (int i = 0; str[i]; i++) {
// If index is odd then
// current character is
// changed
if (i & 1) {
sumOfPrime
+= char(
str[i] - '0' + 65);
}
// Else add the current
// character
else {
sumOfPrime += str[i];
}
}
}
// If the resultant string is ""
// then print -1
if (semiPrime + sumOfPrime == "") {
cout << "-1";
}
// Else print the resultant string
else {
cout << semiPrime + sumOfPrime;
}
}
// Driver Code
int main()
{
// Given Number
int N = 1011243;
// Function Call
makeCypherString(N);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to check
// whether a number
// is prime or not
static boolean isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
static boolean isPossibleSum(int N)
{
// If the N && (N-2) is Prime
if (isPrime(N) && isPrime(N - 2))
{
return true;
}
else
{
return false;
}
}
// Function to check semiPrime
static boolean checkSemiprime(int num)
{
int cnt = 0;
// Loop from 2 to Math.sqrt(num)
for (int i = 2; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0)
{
num /= i;
// Increment the count of
// prime numbers
++cnt;
}
}
// If num is greater than 1, then
// add 1 to it
if (num > 1)
{
++cnt;
}
// Return '1' if count is 2 else
// return '0'
return cnt == 2;
}
// Function to make the Cypher String
static void makeCypherString(int N)
{
// Resultant String
String semiPrime = "";
String sumOfPrime = "";
// Make String for the number N
String str = String.valueOf(N);
// Check for semiPrime
if (checkSemiprime(N))
{
// Traverse to make Cypher String
for (int i = 0; i < str.length(); i++)
{
// If index is odd add the
// current character
if (i % 2 == 1)
{
semiPrime += str.charAt(i);
}
// Else current character is
// changed
else
{
semiPrime += (char)(str.charAt(i) -
'0' + 65);
}
}
}
// Check for sum of two primes
if (isPossibleSum(N))
{
// Traverse to make Cypher String
for (int i = 0; i < str.length(); i++)
{
// If index is odd then
// current character is
// changed
if (i % 2 == 1)
{
sumOfPrime += (char)(str.charAt(i) -
'0' + 65);
}
// Else add the current
// character
else
{
sumOfPrime += str.charAt(i);
}
}
}
// If the resultant String is ""
// then print -1
if (semiPrime + sumOfPrime == "")
{
System.out.print("-1");
}
// Else print the resultant String
else
{
System.out.print(semiPrime +
sumOfPrime);
}
}
// Driver Code
public static void main(String[] args)
{
// Given Number
int N = 1011243;
// Function Call
makeCypherString(N);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
import math
# Function to check whether a number
# is prime or not
def isPrime(n):
if (n <= 1):
return False
sqt = (int)(math.sqrt(n))
for i in range(2, sqt):
if (n % i == 0):
return False
return True
# Function to check if a prime number
# can be expressed as sum of
# two Prime Numbers
def isPossibleSum(N):
# If the N && (N-2) is Prime
if (isPrime(N) and isPrime(N - 2)):
return True
else:
return False
# Function to check semiPrime
def checkSemiprime(num):
cnt = 0
# Loop from 2 to sqrt(num)
i = 2
while cnt < 2 and i * i <= num:
while (num % i == 0):
num //= i
# Increment the count of
# prime numbers
cnt += 1
i += 1
# If num is greater than 1, then
# add 1 to it
if (num > 1):
cnt += 1
# Return '1' if count is 2 else
# return '0'
return cnt == 2
# Function to make the Cypher string
def makeCypherString(N):
# Resultant string
semiPrime = ""
sumOfPrime = ""
# Make string for the number N
st = str(N)
# Check for semiPrime
if (checkSemiprime(N)):
# Traverse to make Cypher string
for i in range(len(st)):
# If index is odd add the
# current character
if (i & 1):
semiPrime += st[i]
# Else current character is
# changed
else:
semiPrime += chr(ord(st[i]) -
ord('0') + 65)
# Check for sum of two primes
if (isPossibleSum(N)):
# Traverse to make Cypher string
for i in range(len(st)):
# If index is odd then
# current character is
# changed
if (i & 1):
sumOfPrime += chr(ord(st[i]) -
ord('0') + 65)
# Else add the current
# character
else:
sumOfPrime += st[i]
# If the resultant string is ""
# then print -1
if (semiPrime + sumOfPrime == ""):
print("-1")
# Else print the resultant string
else:
print(semiPrime + sumOfPrime)
# Driver Code
if __name__ == "__main__":
# Given number
N = 1011243
# Function call
makeCypherString(N)
# This code is contributed by chitranayal
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to check
// whether a number
// is prime or not
static bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2;
i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
static bool isPossibleSum(int N)
{
// If the N && (N-2) is Prime
if (isPrime(N) && isPrime(N - 2))
{
return true;
}
else
{
return false;
}
}
// Function to check semiPrime
static bool checkSemiprime(int num)
{
int cnt = 0;
// Loop from 2 to Math.Sqrt(num)
for (int i = 2; cnt < 2 &&
i * i <= num; ++i)
{
while (num % i == 0)
{
num /= i;
// Increment the count of
// prime numbers
++cnt;
}
}
// If num is greater than 1, then
// add 1 to it
if (num > 1)
{
++cnt;
}
// Return '1' if count is 2 else
// return '0'
return cnt == 2;
}
// Function to make the Cypher String
static void makeCypherString(int N)
{
// Resultant String
String semiPrime = "";
String sumOfPrime = "";
// Make String for the number N
String str = String.Join("", N);
// Check for semiPrime
if (checkSemiprime(N))
{
// Traverse to make Cypher String
for (int i = 0; i < str.Length; i++)
{
// If index is odd add the
// current character
if (i % 2 == 1)
{
semiPrime += str[i];
}
// Else current character is
// changed
else
{
semiPrime += (char)(str[i] -
'0' + 65);
}
}
}
// Check for sum of two primes
if (isPossibleSum(N))
{
// Traverse to make Cypher String
for (int i = 0; i < str.Length; i++)
{
// If index is odd then
// current character is
// changed
if (i % 2 == 1)
{
sumOfPrime += (char)(str[i] -
'0' + 65);
}
// Else add the current
// character
else
{
sumOfPrime += str[i];
}
}
}
// If the resultant String is ""
// then print -1
if (semiPrime + sumOfPrime == "")
{
Console.Write("-1");
}
// Else print the resultant String
else
{
Console.Write(semiPrime +
sumOfPrime);
}
}
// Driver Code
public static void Main(String[] args)
{
// Given Number
int N = 1011243;
// Function Call
makeCypherString(N);
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
B0B1C4D
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live