📜  检查给定数字是否包含一个数字,该数字是所有其他数字的平均值

📅  最后修改于: 2021-04-24 19:43:09             🧑  作者: Mango

给定一个整数N ,任务是检查N是否包含数字D ,使其为N中所有其他数字的平均值。

例子:

方法:
要解决此问题,请按照以下步骤操作:

  • 存储和的N个位数
  • 将数字存储在Set中。
  • 如果sum%count0,并且Set中存在整数sum / count ,即sum / countN的数字,则打印Yes 。否则打印No。

下面是上述方法的实现:

C++
// C++ Program to check whether a
// given number contains a digit
// which is the average of all
// other digits
#include 
using namespace std;
  
// Function which checks if a
// digits exists in n which
// is the average of all other digits
void check(int n)
{
    set digits;
  
    int temp = n;
    int sum = 0;
    int count = 0;
    while (temp > 0) {
        // Calculate sum of
        // digits in n
        sum += temp % 10;
        // Store the digits
        digits.insert(temp % 10);
        // Increase the count
        // of digits in n
        count++;
        temp = temp / 10;
    }
  
    // If average of all digits
    // is an integer
    if (sum % count == 0
        // If the average is a digit
        // in n
        && digits.find(sum / count)
               != digits.end())
        cout << "Yes" << endl;
    // Otherwise
    else
        cout << "No" << endl;
}
  
// Driver Code
int main()
{
    int n = 42644;
    check(n);
}


Java
// Java program to check whether a
// given number contains a digit
// which is the average of all
// other digits
import java.util.*;
  
class GFG{
  
// Function which checks if a
// digits exists in n which
// is the average of all other digits
static void check(int n)
{
    HashSet digits = new HashSet();
  
    int temp = n;
    int sum = 0;
    int count = 0;
    while (temp > 0)
    {
          
        // Calculate sum of
        // digits in n
        sum += temp % 10;
          
        // Store the digits
        digits.add(temp % 10);
          
        // Increase the count
        // of digits in n
        count++;
        temp = temp / 10;
    }
  
    // If average of all digits
    // is an integer
    if (sum % count == 0 &&
        digits.contains(sum / count))
        System.out.print("Yes" + "\n");
          
    // Otherwise
    else
        System.out.print("No" + "\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 42644;
      
    check(n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to check whether a given 
# number contains a digit which is
# the average of all other digits
  
# Function which checks if a 
# digits exists in n which is 
# the average of all other digits 
def check (n):
  
    digits = set()
  
    temp = n
    Sum = 0
    count = 0
      
    while(temp > 0):
  
        # Calculate sum of 
        # digits in n
        Sum += temp % 10
  
        # Store digits
        digits.add(temp % 10)
  
        # Increase the count of 
        # digits in n
        count += 1
        temp = temp // 10
  
    # If average of all digits is integer
    if ((Sum % count == 0) and 
        ((int)(Sum / count) in digits)):
        print("Yes")
    else:
        print("No")
  
# Driver code
n = 42644
  
# Function calling
check(n) 
  
# This code is contributed by himanshu77


C#
// C# program to check whether a given 
// number contains a digit which is the 
// average of all other digits
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function which checks if a
// digits exists in n which
// is the average of all other digits
static void check(int n)
{
    HashSet digits = new HashSet();
  
    int temp = n;
    int sum = 0;
    int count = 0;
    while (temp > 0)
    {
          
        // Calculate sum of
        // digits in n
        sum += temp % 10;
          
        // Store the digits
        digits.Add(temp % 10);
          
        // Increase the count
        // of digits in n
        count++;
        temp = temp / 10;
    }
  
    // If average of all digits
    // is an integer
    if (sum % count == 0 &&
        digits.Contains(sum / count))
        Console.Write("Yes" + "\n");
          
    // Otherwise
    else
        Console.Write("No" + "\n");
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 42644;
      
    check(n);
}
}
  
// This code is contributed by Rajput-Ji


输出:
Yes

时间复杂度: O(log 10 N)