📌  相关文章
📜  ASCII 值可以由 N 的数字组成的字母数

📅  最后修改于: 2021-10-26 02:33:10             🧑  作者: Mango

给定一个整数N 。您可以从这个数字中选择任意两位数字(数字可以相同,但它们的位置应该不同)并以两种可能方式中的任何一种对它们进行排序。对于这些方法中的每一种,您都可以从中创建一个两位数(可能包含前导零)。然后,将选择对应于ASCII值的字符等于该数字,即数字65个对应于“A”,66〜“B”等等。任务是计算可以通过这种方式选择的英文字母(小写或大写)的数量。
例子:

方法:想法是观察可能的字符总数为(26 个小写 + 26 个大写 = 52)。因此,与其从 N 生成两个数字的所有可能组合,不如检查这 52 个字符的出现次数。
因此,计算N中每个数字的出现次数,然后为每个字符(小写或大写)找到它的 ASCII 值并检查它是否可以从给定的数字中挑选出来。最后打印这些字母的数量。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
 
using namespace std;
 
// Function that returns true if num
// can be formed with the digits in
// digits[] array
bool canBePicked(int digits[], int num)
{
 
    int copyDigits[10];
 
    // Copy of the digits array
    for(int i =0 ; i < 10;i++)
        copyDigits[i]=digits[i];
         
    while (num > 0)
    {
        // Get last digit
        int digit = num % 10;
 
        // If digit array doesn't contain
        // current digit
        if (copyDigits[digit] == 0)
            return false;
 
        // One occurrence is used
        else
            copyDigits[digit] -= 1;
 
        // Remove the last digit
        num = floor(num / 10);
    }
 
    return true;
}
 
// Function to return the count of
// required alphabets
int countAlphabets(long n)
{
 
    int count = 0;
 
    // To store the occurrences of
    // digits (0 - 9)
    int digits[10]= {0};
    while (n > 0)
    {
 
        // Get last digit
        int digit = n % 10;
 
        // Update the occurrence of the digit
        digits[digit] += 1;
 
        // Remove the last digit
        n = floor(n / 10);
    }
 
    // If any lowercase character can be
    // picked from the current digits
    for(int i = 97; i <= 122 ;i ++)
        if (canBePicked(digits, i))
            count += 1;
 
    // If any uppercase character can be
    // picked from the current digits
    for(int i = 65; i < 91;i++)
        if (canBePicked(digits, i))
            count += 1;
 
    // Return the required count
    // of alphabets
    return count;
}
 
// Driver code
int main()
{
 
    long n = 1623455078;
    cout<<(countAlphabets(n));
}
 
// This code is contributed by chitranayal


Java
// Java implementation of the approach
class GFG {
 
    // Function that returns true if num can be formed
    // with the digits in digits[] array
    static boolean canBePicked(int digits[], int num)
    {
        // Copy of the digits array
        int copyDigits[] = digits.clone();
        while (num > 0) {
 
            // Get last digit
            int digit = num % 10;
 
            // If digit array doesn't contain current digit
            if (copyDigits[digit] == 0)
                return false;
 
            // One occurrence is used
            else
                copyDigits[digit]--;
 
            // Remove the last digit
            num /= 10;
        }
 
        return true;
    }
 
    // Function to return the count of required alphabets
    static int countAlphabets(int n)
    {
        int i, count = 0;
 
        // To store the occurrences of digits (0 - 9)
        int digits[] = new int[10];
        while (n > 0) {
 
            // Get last digit
            int digit = n % 10;
 
            // Update the occurrence of the digit
            digits[digit]++;
 
            // Remove the last digit
            n /= 10;
        }
 
        // If any lowercase character can be picked
        // from the current digits
        for (i = 'a'; i <= 'z'; i++)
            if (canBePicked(digits, i))
                count++;
 
        // If any uppercase character can be picked
        // from the current digits
        for (i = 'A'; i <= 'Z'; i++)
            if (canBePicked(digits, i))
                count++;
 
        // Return the required count of alphabets
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 1623455078;
        System.out.println(countAlphabets(n));
    }
}


Python3
# Python3 implementation of the approach
import math
 
# Function that returns true if num
# can be formed with the digits in
# digits[] array
def canBePicked(digits, num):
 
    copyDigits = [];
     
    # Copy of the digits array
    for i in range(len(digits)):
        copyDigits.append(digits[i]);
         
    while (num > 0):
 
        # Get last digit
        digit = num % 10;
 
        # If digit array doesn't contain
        # current digit
        if (copyDigits[digit] == 0):
            return False;
 
        # One occurrence is used
        else:
            copyDigits[digit] -= 1;
 
        # Remove the last digit
        num = math.floor(num / 10);
 
    return True;
 
# Function to return the count of
# required alphabets
def countAlphabets(n):
 
    count = 0;
 
    # To store the occurrences of
    # digits (0 - 9)
    digits = [0] * 10;
    while (n > 0):
 
        # Get last digit
        digit = n % 10;
 
        # Update the occurrence of the digit
        digits[digit] += 1;
 
        # Remove the last digit
        n = math.floor(n / 10);
 
    # If any lowercase character can be
    # picked from the current digits
    for i in range(ord('a'), ord('z') + 1):
        if (canBePicked(digits, i)):
            count += 1;
 
    # If any uppercase character can be
    # picked from the current digits
    for i in range(ord('A'), ord('Z') + 1):
        if (canBePicked(digits, i)):
            count += 1;
 
    # Return the required count
    # of alphabets
    return count;
 
# Driver code
n = 1623455078;
print(countAlphabets(n));
 
# This code is contributed by mits


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function that returns true
    // if num can be formed with
    // the digits in digits[] array
    static bool canBePicked(int []digits, int num)
    {
        // Copy of the digits array
        int []copyDigits = (int[]) digits.Clone();
        while (num > 0)
        {
 
            // Get last digit
            int digit = num % 10;
 
            // If digit array doesn't
            // contain current digit
            if (copyDigits[digit] == 0)
                return false;
 
            // One occurrence is used
            else
                copyDigits[digit]--;
 
            // Remove the last digit
            num /= 10;
        }
 
        return true;
    }
 
    // Function to return the count
    // of required alphabets
    static int countAlphabets(int n)
    {
        int i, count = 0;
 
        // To store the occurrences
        // of digits (0 - 9)
        int[] digits = new int[10];
        while (n > 0)
        {
 
            // Get last digit
            int digit = n % 10;
 
            // Update the occurrence of the digit
            digits[digit]++;
 
            // Remove the last digit
            n /= 10;
        }
 
        // If any lowercase character can be 
        // picked from the current digits
        for (i = 'a'; i <= 'z'; i++)
            if (canBePicked(digits, i))
                count++;
 
        // If any uppercase character can be 
        // picked from the current digits
        for (i = 'A'; i <= 'Z'; i++)
            if (canBePicked(digits, i))
                count++;
 
        // Return the required count of alphabets
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 1623455078;
        Console.WriteLine(countAlphabets(n));
    }
}
 
// This code is contributed by
// tufan_gupta2000


PHP
 0)
    {
 
        // Get last digit
        $digit = $num % 10;
 
        // If digit array doesn't contain
        // current digit
        if ($copyDigits[$digit] == 0)
            return false;
 
        // One occurrence is used
        else
            $copyDigits[$digit]--;
 
        // Remove the last digit
        $num = floor($num / 10);
    }
 
    return true;
}
 
// Function to return the count of
// required alphabets
function countAlphabets($n)
{
    $count = 0;
 
    // To store the occurrences of
    // digits (0 - 9)
    $digits = array_fill(0, 10, 0);
    while ($n > 0)
    {
 
        // Get last digit
        $digit = $n % 10;
 
        // Update the occurrence of the digit
        $digits[$digit]++;
 
        // Remove the last digit
        $n = floor($n / 10);
    }
 
    // If any lowercase character can be
    // picked from the current digits
    for ($i = ord('a'); $i <= ord('z'); $i++)
        if (canBePicked($digits, $i))
            $count++;
 
    // If any uppercase character can be 
    // picked from the current digits
    for ($i = ord('A');
         $i <= ord('Z'); $i++)
        if (canBePicked($digits, $i))
            $count++;
 
    // Return the required count
    // of alphabets
    return $count;
}
 
// Driver code
$n = 1623455078;
echo countAlphabets($n);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
27