📜  对字符的 ASCII 值总和等于素数或阿姆斯壮数的字符串进行计数

📅  最后修改于: 2021-09-04 09:38:41             🧑  作者: Mango

给定一个包含字符串的大小为N的数组arr[] ,任务是计算字符的 ASCII 值之和等于阿姆斯特朗数或质数的字符串的数量。

例子:

方法:这个问题可以通过计算每个字符串的ASCII值来解决。请按照以下步骤解决此问题:

  • 将两个变量countPrimecountArmstrong初始化为0 ,以存储 Prime 和 Armstrong 值字符串的计数。
  • 使用变量i迭代索引范围[0, N – 1]并执行以下步骤:
    • 将当前字符串arr[i]的字符的 ASCII 值总和存储在一个变量中,比如val
    • 如果数字val是阿姆斯壮数,则将 countArmstrong增加1
    • 如果数字val是质数,则将 countPrime增加1
  • 打印countPrimecountArmstrong的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if a
// number is prime number
bool isPrime(int num)
{
     
    // Define a flag variable
    bool flag = false;
 
    if (num > 1)
    {
         
        // Check for factors of num
        for(int i = 2; i < num; i++)
        {
             
            // If factor is found,
            // set flag to True and
            // break out of loop
            if ((num % i) == 0)
            {
                flag = true;
                break;
            }
        }
    }
 
    // Check if flag is True
    if (flag)
        return false;
    else
        return true;
}
 
// Function to calculate
// order of the number x
int order(int x)
{
    int n = 0;
    while (x != 0)
    {
        n = n + 1;
        x = x / 10;
    }
    return n;
}
 
// Function to check whether the given
// number is Armstrong number or not
bool isArmstrong(int x)
{
    int n = order(x);
    int temp = x;
    int sum1 = 0;
 
    while (temp != 0)
    {
        int r = temp % 10;
        sum1 = sum1 + pow(r, n);
        temp = temp / 10;
    }
     
    // If the condition satisfies
    return (sum1 == x);
}
 
// Function to count
// Armstrong valued strings
int count_armstrong(vector li)
{
     
    // Stores the count of
    // Armstrong valued strings
    int c = 0;
 
    // Iterate over the list
    for(string ele : li)
    {
         
        // Store the value
        // of the string
        int val = 0;
 
        // Find value of the string
        for(char che:ele)
            val += che;
 
        // Check if it an Armstrong number
        if (isArmstrong(val))
            c += 1;
    }
    return c;
}
 
// Function to count
// prime valued strings
int count_prime(vector li)
{
     
    // Store the count of
    // prime valued strings
    int c = 0;
 
    // Iterate over the list
    for(string ele:li)
    {
         
        // Store the value
        // of the string
        int val = 0;
 
        // Find value of the string
        for(char che : ele)
            val += che;
 
        // Check if it
        // is a Prime Number
        if (isPrime(val))
            c += 1;
    }
    return c;
}
 
// Driver code
int main()
{
    vector arr = { "geeksforgeeks", "a", "computer",
                           "science", "portal", "for", "geeks"};
     
    // Function Call
    cout << "Number of Armstrong Strings are: "
         << count_armstrong(arr) << endl;
    cout << "Number of Prime Strings are: "
         << count_prime(arr) << endl;
}
 
// This code is contributed by mohit kumar 29


Python3
# Python program for the above approach
 
# Function to check if a
# number is prime number
def isPrime(num):
 
    # Define a flag variable
    flag = False
 
    if num > 1:
 
        # Check for factors of num
        for i in range(2, num):
 
            # If factor is found,
            # set flag to True and
            # break out of loop
            if (num % i) == 0:
                flag = True
                break
 
    # Check if flag is True
    if flag:
        return False
    else:
        return True
 
# Function to calculate
# order of the number x
def order(x):
    n = 0
    while (x != 0):
        n = n + 1
        x = x // 10
    return n
 
# Function to check whether the given
# number is Armstrong number or not
def isArmstrong(x):
    n = order(x)
    temp = x
    sum1 = 0
 
    while (temp != 0):
 
        r = temp % 10
        sum1 = sum1 + r**n
        temp = temp // 10
 
    # If the condition satisfies
    return (sum1 == x)
 
# Function to count
# Armstrong valued strings
def count_armstrong(li):
 
    # Stores the count of
    # Armstrong valued strings
    c = 0
 
    # Iterate over the list
    for ele in li:
 
        # Store the value
        # of the string
        val = 0
         
        # Find value of the string
        for che in ele:
            val += ord(che)
             
        # Check if it an Armstrong number
        if isArmstrong(val):
            c += 1
    return c
 
# Function to count
# prime valued strings
def count_prime(li):
     
    # Store the count of
    # prime valued strings
    c = 0
     
    # Iterate over the list
    for ele in li:
       
        # Store the value
        # of the string
        val = 0
         
        # Find value of the string
        for che in ele:
            val += ord(che)
             
        # Check if it
        # is a Prime Number
        if isPrime(val):
            c += 1
    return c
 
 
# Driver code
arr = ["geeksforgeeks", "a", "computer",
       "science", "portal", "for", "geeks"]
 
# Function Call
print("Number of Armstrong Strings are:", count_armstrong(arr))
print("Number of Prime Strings are:", count_prime(arr))


时间复杂度: O(N*M),其中 M 是数组arr[] 中最长字符串长度
辅助空间: O(1)