📌  相关文章
📜  将元素数组连接成单个元素

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

将元素数组连接成单个元素

给定一个由N个整数组成的数组arr[] ,任务是打印通过连接数组元素获得的单个整数值。

例子

方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0,以存储结果值。
  • 使用变量i遍历数组arr[] ,然后在每次迭代中将ans乘以10的整数arr[i]中数字的计数的幂,并将ans递增arr[i]。
  • 最后,经过上述步骤,打印ans中得到的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the integer value
// obtained by joining array elements
// together
int ConcatenateArr(int arr[], int N)
{
    // Stores the resulting integer value
    int ans = arr[0];
 
    // Traverse the array arr[]
    for (int i = 1; i < N; i++) {
 
        // Stores the count of digits of
        // arr[i]
        int l = floor(log10(arr[i]) + 1);
 
        // Update ans
        ans = ans * pow(10, l);
 
        // Increment ans by arr[i]
        ans += arr[i];
    }
    // Return the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Input
    int arr[] = { 1, 23, 456 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << ConcatenateArr(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the integer value
// obtained by joining array elements
// together
static int ConcatenateArr(int[] arr, int N)
{
     
    // Stores the resulting integer value
    int ans = arr[0];
 
    // Traverse the array arr[]
    for(int i = 1; i < N; i++)
    {
         
        // Stores the count of digits of
        // arr[i]
        int l = (int)Math.floor(Math.log10(arr[i]) + 1);
 
        // Update ans
        ans = ans * (int)Math.pow(10, l);
 
        // Increment ans by arr[i]
        ans += arr[i];
    }
     
    // Return the ans
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
     
    // Input
    int arr[] = { 1, 23, 456 };
    int N = arr.length;
 
    // Function call
    System.out.println(ConcatenateArr(arr, N));
}
}
 
// This code is contributed by avijitmondal1998


Python3
# Python3 program for the above approach
import math
 
# Function to find the integer value
# obtained by joining array elements
# together
def ConcatenateArr(arr,  N):
 
    # Stores the resulting integer value
    ans = arr[0]
 
    # Traverse the array arr[]
    for i in range(1,  N):
 
        # Stores the count of digits of
        # arr[i]
        l = math.floor(math.log10(arr[i]) + 1)
 
        # Update ans
        ans = ans * math.pow(10, l)
 
        # Increment ans by arr[i]
        ans += arr[i]
 
    # Return the ans
    return int( ans)
 
# Driver Code
if __name__ == "__main__":
 
    # Input
    arr = [1, 23, 456]
    N = len(arr)
 
    # Function call
    print(ConcatenateArr(arr, N))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the integer value
// obtained by joining array elements
// together
static int ConcatenateArr(int[] arr, int N)
{
     
    // Stores the resulting integer value
    int ans = arr[0];
 
    // Traverse the array arr[]
    for(int i = 1; i < N; i++)
    {
         
        // Stores the count of digits of
        // arr[i]
        int l = (int)Math.Floor(Math.Log10(arr[i]) + 1);
 
        // Update ans
        ans = ans * (int)Math.Pow(10, l);
 
        // Increment ans by arr[i]
        ans += arr[i];
    }
     
    // Return the ans
    return ans;
}
 
// Driver Code
public static void Main()
{
    // Input
    int[] arr = { 1, 23, 456 };
    int N = arr.Length;
 
    // Function call
    Console.Write(ConcatenateArr(arr, N));
 
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
123456

时间复杂度: O(N*log(M)),其中 M 是数组的最大元素。
辅助空间: O(1)