📌  相关文章
📜  由数组的数字组成的两个数字的最小和

📅  最后修改于: 2021-05-07 05:33:19             🧑  作者: Mango

给定一个数字数组(值从0到9),找到由该数组的数字形成的两个数字的最小和。给定数组的所有数字必须用于形成两个数字。
例子 :

Input: [6, 8, 4, 5, 2, 3]
Output: 604
The minimum sum is formed by numbers 
358 and 246

Input: [5, 3, 0, 7, 4]
Output: 82
The minimum sum is formed by numbers 
35 and 047 

当最小位数出现在最高有效位置而下一个最小位数出现在下一个最高有效位置时,将由一组数字形成一个最小值。
想法是按递增顺序对数组进行排序,并通过交替从数组中选取数字来构建两个数字。因此,第一个数字由数组中奇数个位置中的数字组成,第二个数字由数组中偶数个位置中的数字组成。最后,我们返回第一个和第二个数字的总和。
以下是上述想法的实现。

C++
// C++ program to find minimum sum of two numbers
// formed from digits of the array.
#include 
using namespace std;
 
// Function to find and return minimum sum of
// two numbers formed from digits of the array.
int solve(int arr[], int n)
{
    // sort the array
    sort(arr, arr + n);
 
    // let two numbers be a and b
    int a = 0, b = 0;
    for (int i = 0; i < n; i++)
    {
        // fill a and b with every alternate digit
        // of input array
        if (i & 1)
            a = a*10 + arr[i];
        else
            b = b*10 + arr[i];
    }
 
    // return the sum
    return a + b;
}
 
// Driver code
int main()
{
    int arr[] = {6, 8, 4, 5, 2, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Sum is " << solve(arr, n);
    return 0;
}


Java
// Java program to find minimum sum of two numbers
// formed from digits of the array.
import java.util.Arrays;
 
class GFG {
     
    // Function to find and return minimum sum of
    // two numbers formed from digits of the array.
    static int solve(int arr[], int n)
    {
         
        // sort the array
        Arrays.sort(arr);
     
        // let two numbers be a and b
        int a = 0, b = 0;
        for (int i = 0; i < n; i++)
        {
             
            // fill a and b with every alternate
            // digit of input array
            if (i % 2 != 0)
                a = a * 10 + arr[i];
            else
                b = b * 10 + arr[i];
        }
     
        // return the sum
        return a + b;
    }
     
    //driver code
    public static void main (String[] args)
    {
        int arr[] = {6, 8, 4, 5, 2, 3};
        int n = arr.length;
         
        System.out.print("Sum is "
                          + solve(arr, n));
    }
}
 
//This code is contributed by Anant Agarwal.


Python3
# Python3 program to find minimum sum of two
# numbers formed from digits of the array.
 
# Function to find and return minimum sum of
# two numbers formed from digits of the array.
def solve(arr, n):
 
    # sort the array
    arr.sort()
 
    # let two numbers be a and b
    a = 0; b = 0
    for i in range(n):
     
        # Fill a and b with every alternate
        # digit of input array
        if (i % 2 != 0):
            a = a * 10 + arr[i]
        else:
            b = b * 10 + arr[i]
 
    # return the sum
    return a + b
 
# Driver code
arr = [6, 8, 4, 5, 2, 3]
n = len(arr)
print("Sum is ", solve(arr, n))
 
# This code is contributed by Anant Agarwal.


C#
// C# program to find minimum
// sum of two numbers formed
// from digits of the array.
using System;
 
class GFG
{
    // Function to find and return
    // minimum sum of two numbers
    // formed from digits of the array.
    static int solve(int []arr, int n)
    {
        // sort the array
        Array.Sort(arr);
      
        // let two numbers be a and b
        int a = 0, b = 0;
        for (int i = 0; i < n; i++)
        {
            // fill a and b with every alternate digit
            // of input array
            if (i % 2 != 0)
                a = a * 10 + arr[i];
            else
                b = b * 10 + arr[i];
        }
      
        // return the sum
        return a + b;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr = {6, 8, 4, 5, 2, 3};
        int n = arr.Length;
        Console.WriteLine("Sum is " + solve(arr, n));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出 :

Sum is 604