📜  检查字符串的平均字符是否存在

📅  最后修改于: 2022-05-13 01:57:06.758000             🧑  作者: Mango

检查字符串的平均字符是否存在

给定字符串字母数字字符,任务是检查字符串的平均字符是否存在。平均字符是指ASCII值对应的字符,它是字符串中所有字符的ASCII值的平均值的下限。

例子:

Input: abcdef
Output: d 
Yes
Explanation:
    string = "abcdef"
    ASCII values of a = 97, b=98, 
    c=99, d=100, e=101, f=101
    Sum of these values is 597
    Average is 99.5 ~ 100 
    Character of ASCII value 100 = d
    Hence d is present in the string.

Input: MNFGH
Output: J
No

方法:解决这个问题的方法很简单。可以通过以下步骤解决:

  • 求给定字符串中所有字符的 ASCII 值之和。
  • 求 ASCII 值的平均值为 average = (sum / numberOfCharacters)
  • 获取这个平均 ASCII 值的字符。打印这个字符
  • 检查该字符是否存在于字符串中。相应地打印是或否。

下面是上述方法的实现:

执行:

C++
// CPP program to check if the average character
// is present in the string or not
  
#include 
#include 
using namespace std;
  
// Checks if the character is present
bool check_char(char* st, char ch)
{
  
    // Get the length of string
    int l = strlen(st);
  
    // Iterate from i=0 to
    // the length of the string
    // to check if the character
    // is present in the string
    for (int i = 0; i < l; i++) {
        if (st[i] == ch)
            return true;
    }
    return false;
}
  
// Finds the average character of the string
char find_avg(char* st)
{
    int i, sm = 0;
    int l = strlen(st);
    char ch;
  
    for (i = 0; i < l; i++) {
        ch = st[i];
  
        // Calculate the sum of ASCII
        // values of each character
        sm = sm + (int)(ch);
    }
  
    // Calculate average of ascii values
    int avg = (int)(floor(sm / l));
  
    // Convert the ASCII value to character
    // and return it
    return ((char)(avg));
}
  
// Driver code
int main()
{
    char st[] = "ag23sdfa";
  
    // Get the average character
    char ch = find_avg(st);
    cout << ch << endl;
  
    // Check if the average character
    // is present in string or not
    if (check_char(st, ch) == true)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to check if the average character
// is present in the string or not
  
import java.math.*;
  
class GFG {
  
    // Checks if the character is present
    static boolean check_char(String st, char ch)
    {
  
        // Get the length of string
        int l = st.length();
  
        // Iterate from i=0 to
        // the length of the string
        // to check if the character
        // is present in the string
        for (int i = 0; i < l; i++) {
            if (st.charAt(i) == ch)
                return true;
        }
        return false;
    }
  
    // Finds the average character of the string
    static char find_avg(String st)
    {
        int i, sm = 0;
        int l = st.length();
        char ch;
  
        for (i = 0; i < l; i++) {
            ch = st.charAt(i);
  
            // Calculate the sum of ASCII
            // values of each character
            sm = sm + (int)(ch);
        }
  
        // Calculate the average of ASCII values
        int avg = (int)(Math.floor(sm / l));
  
        // Convert the ASCII value to character
        // and return it
        return ((char)(avg));
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String st = "ag23sdfa";
  
        // Get the average character
        char ch = find_avg(st);
        System.out.println(ch);
  
        // Check if the average character
        // is present in string or not
        if (check_char(st, ch) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
              
    }
}


Python3
# Python 3 program to check if the average 
# character is present in the string or not
from math import floor
  
# Checks if the character is present
def check_char(st, ch):
      
    # Get the length of string
    l = len(st)
  
    # Iterate from i=0 to
    # the length of the string
    # to check if the character
    # is present in the string
    for i in range(l):
        if (st[i] == ch):
            return True
  
    return False
  
# Finds the average character
# of the string
def find_avg(st):
    sm = 0
    l = len(st)
  
    for i in range(l):
        ch = st[i]
  
        # Calculate the sum of ASCII
        # values of each character
        sm = sm + ord(ch)
  
    # Calculate average of ascii values
    avg = int(floor(sm / l))
  
    # Convert the ASCII value to character
    # and return it
    return (chr(avg))
  
# Driver code
if __name__ == '__main__':
    st = "ag23sdfa"
  
    # Get the average character
    ch = find_avg(st)
    print(ch)
      
    # Check if the average character
    # is present in string or not
    if (check_char(st, ch) == True):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to check if the average character
// is present in the string or not
using System;
  
class GFG 
{
  
    // Checks if the character is present
    static bool check_char(string st, char ch)
    {
  
        // Get the length of string
        int l = st.Length;
  
        // Iterate from i=0 to
        // the length of the string
        // to check if the character
        // is present in the string
        for (int i = 0; i < l; i++) 
        {
            if (st[i] == ch)
                return true;
        }
        return false;
    }
  
    // Finds the average character of the string
    static char find_avg(string st)
    {
        int i, sm = 0;
        int l = st.Length;
        char ch;
  
        for (i = 0; i < l; i++)
        {
            ch = st[i];
  
            // Calculate the sum of ASCII
            // values of each character
            sm = sm + (int)(ch);
        }
  
        // Calculate the average of ASCII values
        int avg = (int)(Math.Floor((double)(sm / l)));
  
        // Convert the ASCII value to character
        // and return it
        return ((char)(avg));
    }
  
    // Driver code
    public static void Main()
    {
        string st = "ag23sdfa";
  
        // Get the average character
        char ch = find_avg(st);
        Console.WriteLine(ch);
  
        // Check if the average character
        // is present in string or not
        if (check_char(st, ch) == true)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
              
    }
}
  
// This code is contributed by Akanksha Rai


输出:
Y
No