📌  相关文章
📜  检查通过连接所有数组元素形成的数字是否为Harshad数字

📅  最后修改于: 2021-04-17 17:49:02             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是检查将所有数组元素串联而成的数是否为Harshad数。

例子:

方法:想法是将所有数组元素转换为它们的等效字符串,然后将这些字符串连接起来。请按照以下步骤解决问题:

  1. 遍历数组arr []并将每个数组元素转换为其等效的字符串。
  2. 将所有字符串连接到一个变量中,例如S。
  3. 初始化一个变量,例如sum,以存储所生成数字的位数之和。
  4. 遍历字符串S并将总和更新为sum + = int(s [i])。
  5. 初始化一个变量,例如N = 0,以存储通过连接字符串S mod sum的所有字符形成的数字。
  6. 遍历字符串S并将N更新为N =(N * 10 + int(S [i]))%sum。
  7. 如果N = 0 则打印
  8. 否则,打印No。

下面是上述方法的实现:

C++
// CPP implementation
// of above approach
#include
using namespace std;
 
// Function to check whether n
// is a harshad number or not
int checkHarshad(string n)
{
 
  // Stores the sum of digits
  int sum = 0;
 
  // Stores the remainder
  int N = 0;
 
  // Increment sum
  for (int c = 0; c < n.length(); c++)
    sum += (n);
 
  for (int c = 0; c < n.length(); c++)
  {
    N = N + (N * 10 + (n));
    N %= sum;
  }
  return (N != 0);
}
 
// Function to check if conctenation
// of elements from the array arr[]
// is a harshad number or not
bool combineArray(vector lis)
{
 
  // Stores the concatenated number
  string st = "";
 
  // Traverse the array
  for(auto el: lis)
  {
 
    // Concatenate the string
    st += to_string(el);
  }
 
  if(checkHarshad(st))
    return true;
  else
    return false;
}
 
// Driver Code
int main()
{
 
  // Input
  vectorarr{1, 35, 69, 60};
 
  // Function call to check if
  // concatenation of elements of
  // arr[] is a harshad number
  if(combineArray(arr))
    cout << "Yes";
  else
    cout << "No";
}
 
// This code is contributed by ipg2016107.


Java
/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.ArrayList;
 
class GFG {
 
  // Function to check whether n
  // is a harshad number or not
  public static boolean checkHarshad(String n)
  {
 
    // Stores the sum of digits
    int sum = 0;
 
    // Stores the remainder
    int N = 0;
 
    // Increment sum
    for (int c = 0; c < n.length(); c++) {
      sum += Character.getNumericValue(n.charAt(c));
    }
    for (int c = 0; c < n.length(); c++) {
      N = N
        + (N * 10
           + (Character.getNumericValue(
             n.charAt(c))));
      N %= sum;
    }
    if (N == 0) {
      return true;
    }
    return false;
  }
 
  // Function to check if conctenation
  // of elements from the array arr[]
  // is a harshad number or not
  public static boolean
    combineArray(ArrayList list)
  {
 
    // Stores the concatenated number
    String st = "";
 
    // Traverse the array
    for (int i = 0; i < list.size(); i++)
    {
 
      // Concatenate the string
      st += Integer.toString(list.get(i));
    }
 
    if (checkHarshad(st)) {
      return true;
    }
    return false;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Input
    ArrayList list = new ArrayList<>();
    list.add(1);
    list.add(35);
    list.add(69);
    list.add(60);
     
    // Function call to check if
    // concatenation of elements of
    // arr[] is a harshad number
    if (combineArray(list))
      System.out.println("Yes");
    else
      System.out.println("No");
  }
}
 
// This code is contributed by aditya7409


Python3
# Python implementation
# of above approach
 
# Function to check whether n
# is a harshad number or not
def checkHarshad(n):
   
    # Stores the sum of digits
    sum = 0
 
    # Stores the remainder
    N = 0
     
    # Increment sum
    for c in n:
        sum += int(c)
     
    for c in n:
        N = N + (N*10+int(c))
        N %= sum
 
    return N == 0
 
# Function to check if conctenation
# of elements from the array arr[]
# is a harshad number or not
def combineArray(lis):
 
    # Stores the concatenated number
    string=""
 
    # Traverse the array
    for el in lis:
 
        # Convert to equivalent string
        el = str(el)
 
        # Concatenate the string
        string = string + el
 
    if(checkHarshad(string)):
        return True
    else:
        return False
 
 
# Driver Code
 
# Input
arr=[1, 35, 69, 60]
 
# Function call to check if
# concatenation of elements of
# arr[] is a harshad number
if(combineArray(arr)):
    print("Yes")
else:
    print("No")


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
  // Function to check whether n
  // is a harshad number or not
  public static bool checkHarshad(string n)
  {
 
    // Stores the sum of digits
    int sum = 0;
 
    // Stores the remainder
    int N = 0;
 
    // Increment sum
    for (int c = 0; c < n.Length; c++) {
      sum += (int)Char.GetNumericValue(n);
    }
    for (int c = 0; c < n.Length; c++) {
      N = N
        + (N * 10
           + (int)(Char.GetNumericValue(
             n)));
      N %= sum;
    }
    if (N == 0) {
      return true;
    }
    return false;
  }
 
  // Function to check if conctenation
  // of elements from the array arr[]
  // is a harshad number or not
  static bool
    combineArray(List list)
  {
 
    // Stores the concatenated number
    string st = "";
 
     
     st += string.Join("", list);
 
    if (checkHarshad(st)) {
      return true;
    }
    return false;
  }
 
// Driver code
static void Main()
{
    List list = new List();
    list.Add(1);
    list.Add(35);
    list.Add(69);
    list.Add(60);
     
    // Function call to check if
    // concatenation of elements of
    // arr[] is a harshad number
    if (combineArray(list))
      Console.WriteLine("Yes");
    else
      Console.WriteLine("No");
}
}
 
// This code is contributed susmitakundugoaldanga.


输出:
Yes

时间复杂度: O(N)
辅助空间: O(N)