📜  计算给定数字N中的数字,该数字除以N

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

给定一个可能为10 ^ 5位数长的数字N,任务是对N中除以N的所有数字进行计数。除数不允许为0。如果N中重复的任何数字除以N,则应计算该数字的所有重复,即N = 122324,此处2除以N并出现3次。因此,数字2的计数为3。
例子:

Input  : N = "35"
Output : 1
There are two digits in N and 1 of them
(5)divides it.

Input  : N = "122324"
Output : 5
N is divisible by 1, 2 and 4

如何检查数字N是否存储为字符串的大N?
这个想法是使用mod操作的分配属性。
(x + y)%a =((x%a)+(y%a))%a。

// This function returns true if digit divides N, 
// else false
bool divisible(string N, int digit)
{
    int ans = 0;
    for (int i = 0; i < N.length(); i++)
    {     
        // (N[i]-'0') gives the digit value and
        // form the number       
        ans  = (ans*10 + (N[i]-'0'));

        // We use distributive property of mod here. 
        ans %= digit;
    }
    return (ans == 0);
}

解决此问题的一种简单方法是读取字符串形式的数字,并按N中出现的每个数字一一检查除数。此方法的时间复杂度为O(N 2 )。
解决此问题的有效方法是使用大小为10的额外数组除法[]。由于我们只有10位数字,因此请运行从1到9的循环,并检查N与每个数字从1到9的可除性。 N然后在divit []数组中将数字标记为“真”作为索引。现在遍历数字字符串,并在当前数字i的divide [i]为true的情况下递增结果。

C++
// C++ program to find number of digits in N that
// divide N.
#include
using namespace std;
 
// Utility function to check divisibility by digit
bool divisible(string N, int digit)
{
    int ans = 0;
    for (int i = 0; i < N.length(); i++)
    {
        // (N[i]-'0') gives the digit value and
        // form the number
        ans  = (ans*10 + (N[i]-'0'));
        ans %= digit;
    }
    return (ans == 0);
}
 
// Function to count digits which appears in N and
// divide N
// divide[10]  --> array which tells that particular
//                 digit divides N or not
// count[10]   --> counts frequency of digits which
//                 divide N
int allDigits(string N)
{
    // We initialize all digits of N as not divisible
    // by N.
    bool divide[10] = {false};
    divide[1] = true;  // 1 divides all numbers
 
    // start checking divisibility of N by digits 2 to 9
    for (int digit=2; digit<=9; digit++)
    {
        // if digit divides N then mark it as true
        if (divisible(N, digit))
            divide[digit] = true;
    }
 
    // Now traverse the number string to find and increment
    // result whenever a digit divides N.
    int result = 0;
    for (int i=0; i


Java
// Java program to find number of digits in N that
// divide N.
import java.util.*;
 
class solution
{
 
// Utility function to check divisibility by digit
static boolean divisible(String N, int digit)
{
    int ans = 0;
    for (int i = 0; i < N.length(); i++)
    {
        // (N[i]-'0') gives the digit value and
        // form the number
        ans = (ans*10 + (N.charAt(i)-'0'));
        ans %= digit;
    }
    return (ans == 0);
}
 
// Function to count digits which appears in N and
// divide N
// divide[10] --> array which tells that particular
//                 digit divides N or not
// count[10] --> counts frequency of digits which
//                 divide N
static int allDigits(String N)
{
    // We initialize all digits of N as not divisible
    // by N.
    Boolean[] divide = new Boolean[10];
    Arrays.fill(divide, Boolean.FALSE);
    divide[1] = true; // 1 divides all numbers
 
    // start checking divisibility of N by digits 2 to 9
    for (int digit=2; digit<=9; digit++)
    {
        // if digit divides N then mark it as true
        if (divisible(N, digit))
            divide[digit] = true;
    }
 
    // Now traverse the number string to find and increment
    // result whenever a digit divides N.
    int result = 0;
    for (int i=0; i


Python3
# Python3 program to find number of
# digits in N that divide N.
 
# Utility function to check
# divisibility by digit
def divisible(N, digit):
  
    ans = 0;
    for i in range(len(N)):
        # (N[i]-'0') gives the digit
        # value and form the number
        ans = (ans * 10 + (ord(N[i]) - ord('0')));
        ans %= digit;
    return (ans == 0);
 
# Function to count digits which
# appears in N and divide N
# divide[10] --> array which tells
# that particular digit divides N or not
# count[10] --> counts frequency of
#                 digits which divide N
def allDigits(N):
  
    # We initialize all digits of N
    # as not divisible by N.
    divide =[False]*10;
    divide[1] = True; # 1 divides all numbers
 
    # start checking divisibility of
    # N by digits 2 to 9
    for digit in range(2,10):
        # if digit divides N then
        # mark it as true
        if (divisible(N, digit)):
            divide[digit] = True;
 
    # Now traverse the number string to
    # find and increment result whenever
    # a digit divides N.
    result = 0;
    for i in range(len(N)):
      
        if (divide[(ord(N[i]) - ord('0'))] == True):
            result+=1;
 
    return result;
 
# Driver Code
N = "122324";
print(allDigits(N));
 
# This code is contributed by mits


C#
// C# program to find number of digits
// in N that divide N.
using System;
 
class GFG {
     
// Utility function to
// check divisibility by digit
static bool divisible(string N, int digit)
{
    int ans = 0;
    for (int i = 0; i < N.Length; i++)
    {
         
        // (N[i]-'0') gives the digit value and
        // form the number
        ans = (ans * 10 + (N[i] - '0'));
        ans %= digit;
    }
    return (ans == 0);
}
 
// Function to count digits which
// appears in N and divide N
// divide[10] --> array which
// tells that particular
// digit divides N or not
// count[10] --> counts
// frequency of digits which
// divide N
static int allDigits(string N)
{
     
    // We initialize all digits
    // of N as not divisible by N
    bool[] divide = new bool[10];
     
    for (int i = 0; i < divide.Length; i++)
    {
        divide[i] = false;
    }
     
    // 1 divides all numbers
    divide[1] = true;
 
    // start checking divisibility
    // of N by digits 2 to 9
    for (int digit = 2; digit <= 9; digit++)
    {
         
        // if digit divides N
        // then mark it as true
        if (divisible(N, digit))
            divide[digit] = true;
    }
 
    // Now traverse the number
    // string to find and increment
    // result whenever a digit divides N.
    int result = 0;
     
    for (int i = 0; i < N.Length; i++)
    {
        if (divide[N[i] - '0'] == true)
            result++;
    }
 
    return result;
}
 
// Driver Code
public static void Main()
{
    string N = "122324";
    Console.Write(allDigits(N));
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP
 array which tells
// that particular digit divides N or not
// count[10] --> counts frequency of
//                 digits which divide N
function allDigits($N)
{
    // We initialize all digits of N
    // as not divisible by N.
    $divide = array_fill(0, 10, false);
    $divide[1] = true; // 1 divides all numbers
 
    // start checking divisibility of
    // N by digits 2 to 9
    for ($digit = 2; $digit <= 9; $digit++)
    {
        // if digit divides N then
        // mark it as true
        if (divisible($N, $digit))
            $divide[$digit] = true;
    }
 
    // Now traverse the number string to
    // find and increment result whenever
    // a digit divides N.
    $result = 0;
    for ($i = 0; $i < strlen($N); $i++)
    {
        if ($divide[(int)($N[$i] - '0')] == true)
            $result++;
    }
 
    return $result;
}
 
// Driver Code
$N = "122324";
echo allDigits($N);
 
// This code is contributed by mits
?>


Javascript


输出 :

5