📜  最多使用两个数字之和来交换两个数字的和

📅  最后修改于: 2021-05-07 09:56:46             🧑  作者: Mango

给定两个自然数N1N2 ,任务是在它们之间交换一位数字后找到可能的最大和。
例子:

方法:

  1. 比较N1N2并将两者中较大的一个存储在数组arr1中,将较小的一个存储在数组arr2中
  2. 如果两个数字的长度不同,请在arr2中找到最大元素的索引,在arr1中找到最高有效的索引,并将它们交换以使总和最大化。
  3. 如果两个数字的长度相同
    • 同时迭代数组arr1arr2
    • 对于两个数组中索引i处的每个数字,找到当前数字与索引i剩余的最大数字之间的差。
    • 比较差异以查找需要交换其最高有效位和最高有效索引。
  4. arr1arr2恢复新数字并计算最大和。

下面的代码是上述方法的实现:

C++
// C++ program to maximise the sum of two
// Numbers using at most one swap between them
 
#include 
using namespace std;
 
#define MAX 100
 
// Function to maximize the sum
// by swapping only one digit
void findMaxSum(int n1, int n2)
{
 
    int arr1[MAX] = { 0 }, arr2[MAX] = { 0 };
    int l1 = 0, l2 = 0;
 
    int max1 = max(n1, n2);
    int min1 = min(n1, n2);
 
    // Store digits of max(n1, n2)
    for (int i = max1; i > 0; i /= 10)
        arr1[l1++] = (i % 10);
 
    // Store digits of min(n1, n2)
    for (int i = min1; i > 0; i /= 10)
        arr2[l2++] = (i % 10);
 
    int f = 0;
 
    // If length of the two numbers
    // are unequal
    if (l1 != l2) {
        // Find the most significant number
        // and the most significant index
        // for swapping
        int index = (max_element(arr2, arr2 + l2) - arr2);
        for (int i = l1 - 1; i > (l2 - 1); i--) {
            if (arr1[i] < arr2[index]) {
                swap(arr1[i], arr2[index]);
                f = 1;
                break;
            }
        }
    }
 
    // If both numbers are
    // of equal length
    if (f != 1) {
 
        int index1 = 0, index2 = 0;
        int diff1 = 0, diff2 = 0;
        for (int i = l2 - 1; i >= 0; i--) {
 
            // Fetch the index of current maximum
            // digit present in both the arrays
            index1 = (max_element(arr1, arr1 + i) - arr1);
            index2 = (max_element(arr2, arr2 + i) - arr2);
 
            // Compute the difference
            diff1 = (arr2[index2] - arr1[i]);
            diff2 = (arr1[index1] - arr2[i]);
 
            // Find the most significant index
            // and the most significant digit
            // to be swapped
            if (diff1 > 0 || diff2 > 0) {
 
                if (diff1 > diff2) {
                    swap(arr1[i], arr2[index2]);
                    break;
                }
 
                else if (diff2 > diff1) {
                    swap(arr2[i], arr1[index1]);
                    break;
                }
 
                else if (diff1 == diff2) {
 
                    if (index1 <= index2) {
                        swap(arr2[i], arr1[index1]);
                        break;
                    }
 
                    else if (index2 <= index1) {
                        swap(arr1[i], arr2[index2]);
                        break;
                    }
                }
            }
        }
    }
 
    // Restore the new numbers
    int f_n1 = 0, f_n2 = 0;
    for (int i = l1 - 1; i >= 0; i--) {
        f_n1 = (f_n1 * 10) + arr1[i];
        f_n2 = (f_n2 * 10) + arr2[i];
    }
 
    // Display the maximized sum
    cout << (f_n1 + f_n2) << "\n";
}
 
// Driver function
int main()
{
    int N1 = 9987;
    int N2 = 123;
 
    findMaxSum(N1, N2);
    return 0;
}


Java
// Java program to maximise the sum of two
// Numbers using at most one swap between them
import java.util.*;
 
class GFG{
 
static int MAX = 100;
 
static int max_element(int arr[], int pos)
{
    int tmp = arr[0];
    int ind = 0;
     
    for(int i = 1; i < pos; i++)
    {
        if (tmp < arr[i])
        {
            tmp = arr[i];
            ind = i;
        }
    }
    return ind;
}
 
// Function to maximize the sum
// by swapping only one digit
static void findMaxSum(int n1, int n2)
{
    int []arr1 = new int[MAX];
    int []arr2 = new int[MAX];
    int l1 = 0, l2 = 0;
 
    int max1 = Math.max(n1, n2);
    int min1 = Math.min(n1, n2);
 
    // Store digits of max(n1, n2)
    for(int i = max1; i > 0; i /= 10)
        arr1[l1++] = (i % 10);
 
    // Store digits of min(n1, n2)
    for(int i = min1; i > 0; i /= 10)
        arr2[l2++] = (i % 10);
 
    int f = 0;
 
    // If length of the two numbers
    // are unequal
    if (l1 != l2)
    {
         
        // Find the most significant number
        // and the most significant index
        // for swapping
        int index = (max_element(arr2, l2));
        for(int i = l1 - 1; i > (l2 - 1); i--)
        {
            if (arr1[i] < arr2[index])
            {
                int tmp = arr1[i];
                arr1[i] = arr2[index];
                arr2[index] = tmp;
                f = 1;
                break;
            }
        }
    }
 
    // If both numbers are
    // of equal length
    if (f != 1)
    {
        int index1 = 0, index2 = 0;
        int diff1 = 0, diff2 = 0;
         
        for(int i = l2 - 1; i >= 0; i--)
        {
             
            // Fetch the index of current maximum
            // digit present in both the arrays
            index1 = (max_element(arr1, i));
            index2 = (max_element(arr2, i));
 
            // Compute the difference
            diff1 = (arr2[index2] - arr1[i]);
            diff2 = (arr1[index1] - arr2[i]);
 
            // Find the most significant index
            // and the most significant digit
            // to be swapped
            if (diff1 > 0 || diff2 > 0)
            {
                if (diff1 > diff2)
                {
                    int tmp = arr1[i];
                    arr1[i] = arr2[index2];
                    arr2[index2] = tmp;
                    break;
                }
 
                else if (diff2 > diff1)
                {
                    int tmp = arr1[index1];
                    arr1[index1] = arr2[i];
                    arr2[i] = tmp;
                    break;
                }
 
                else if (diff1 == diff2)
                {
                    if (index1 <= index2)
                    {
                        int tmp = arr1[index1];
                        arr1[index1] = arr2[i];
                        arr2[i] = tmp;
                        break;
                    }
 
                    else if (index2 <= index1)
                    {
                        int tmp = arr1[i];
                        arr1[i] = arr2[index2];
                        arr2[index2] = tmp;
                        break;
                    }
                }
            }
        }
    }
 
    // Restore the new numbers
    int f_n1 = 0, f_n2 = 0;
    for(int i = l1 - 1; i >= 0; i--)
    {
        f_n1 = (f_n1 * 10) + arr1[i];
        f_n2 = (f_n2 * 10) + arr2[i];
    }
 
    // Display the maximized sum
    System.out.println(f_n1 + f_n2);
}
 
// Driver code
public static void main(String[] args)
{
    int N1 = 9987;
    int N2 = 123;
 
    findMaxSum(N1, N2);
}
}
 
// This code is contributed by grand_master


Python3
# Python program to maximise the sum of two
# Numbers using at most one swap between them
MAX = 100
 
# Function to maximize the sum
# by swapping only one digit
def findMaxSum(n1, n2):
 
    arr1 = [0]*(MAX)
    arr2 = [0]*(MAX)
    l1 = 0
    l2 = 0
 
    max1 = max(n1, n2);
    min1 = min(n1, n2);
 
    # Store digits of max(n1, n2)
    i = max1
    while i > 0:
        arr1[l1] = (i % 10)
        l1 += 1
        i //= 10
 
    # Store digits of min(n1, n2)
    i = min1
    while i > 0:
        arr2[l2] = (i % 10)
        l2 += 1
        i //= 10
 
    f = 0
     
    # If length of the two numbers
    # are unequal
    if (l1 != l2):
         
        # Find the most significant number
        # and the most significant index
        # for swapping
        index = arr2.index(max(arr2))
        for i in range ( l1 - 1, (l2 - 1), -1):
            if (arr1[i] < arr2[index]):
                (arr1[i], arr2[index]) = (arr2[index],arr1[i])
                f = 1
                break
 
    # If both numbers are
    # of equal length
    if (f != 1):
 
        index1 = 0
        index2 = 0
        diff1 = 0
        diff2 = 0
        for i in range( l2 - 1, -1,-1):
             
            # Fetch the index of current maximum
            # digit present in both the arrays
            index1 = arr1.index(max(arr1[:i]))
            index2 = arr2.index(max(arr2[:i]))
             
            # Compute the difference
            diff1 = (arr2[index2] - arr1[i]);
            diff2 = (arr1[index1] - arr2[i]);
             
 
            # Find the most significant index
            # and the most significant digit
            # to be swapped
            if (diff1 > 0 or diff2 > 0):
                if (diff1 > diff2):
                    arr1[i], arr2[index2] = arr2[index2],arr1[i]
                    break
 
                elif (diff2 > diff1):
                    arr2[i], arr1[index1] = arr1[index1],arr2[i]
                    break
 
                elif (diff1 == diff2):
                    if (index1 <= index2):
                        arr2[i], arr1[index1] = arr1[index1],arr2[i]
                        break
 
                    elif (index2 <= index1):
                        arr1[i], arr2[index2] = arr2[index2],arr1[i]
                        break;
 
    # Restore the new numbers
    f_n1 = 0
    f_n2 = 0
    for i in range (l1 - 1, -1,-1):
        f_n1 = (f_n1 * 10) + arr1[i]
        f_n2 = (f_n2 * 10) + arr2[i]
     
    # Display the maximized sum
    print(f_n1 + f_n2)
 
# Driver function
N1 = 9987
N2 = 123
 
findMaxSum(N1, N2)
 
# This code is contributed by ANKITKUMAR34


C#
// C# program to maximise the sum of two
// Numbers using at most one swap between them
using System;
 
class GFG{
 
static int MAX = 100;
 
static int max_element(int []arr, int pos)
{
    int tmp = arr[0];
    int ind = 0;
     
    for(int i = 1; i < pos; i++)
    {
        if (tmp < arr[i])
        {
            tmp = arr[i];
            ind = i;
        }
    }
    return ind;
}
 
// Function to maximize the sum
// by swapping only one digit
static void findMaxSum(int n1, int n2)
{
 
    int []arr1 = new int[MAX];
    int []arr2 = new int[MAX];
    int l1 = 0, l2 = 0;
 
    int max1 = Math.Max(n1, n2);
    int min1 = Math.Min(n1, n2);
 
    // Store digits of max(n1, n2)
    for(int i = max1; i > 0; i /= 10)
        arr1[l1++] = (i % 10);
 
    // Store digits of min(n1, n2)
    for(int i = min1; i > 0; i /= 10)
        arr2[l2++] = (i % 10);
 
    int f = 0;
 
    // If length of the two numbers
    // are unequal
    if (l1 != l2)
    {
         
        // Find the most significant number
        // and the most significant index
        // for swapping
        int index = (max_element(arr2,l2));
        for(int i = l1 - 1; i > (l2 - 1); i--)
        {
            if (arr1[i] < arr2[index])
            {
                int tmp = arr1[i];
                arr1[i] = arr2[index];
                arr2[index] = tmp;
                f = 1;
                break;
            }
        }
    }
 
    // If both numbers are
    // of equal length
    if (f != 1)
    {
        int index1 = 0, index2 = 0;
        int diff1 = 0, diff2 = 0;
         
        for(int i = l2 - 1; i >= 0; i--)
        {
             
            // Fetch the index of current maximum
            // digit present in both the arrays
            index1 = (max_element(arr1, i));
            index2 = (max_element(arr2, i));
 
            // Compute the difference
            diff1 = (arr2[index2] - arr1[i]);
            diff2 = (arr1[index1] - arr2[i]);
 
            // Find the most significant index
            // and the most significant digit
            // to be swapped
            if (diff1 > 0 || diff2 > 0)
            {
                if (diff1 > diff2)
                {
                    int tmp = arr1[i];
                    arr1[i] = arr2[index2];
                    arr2[index2] = tmp;
                    break;
                }
 
                else if (diff2 > diff1)
                {
                    int tmp = arr1[index1];
                    arr1[index1] = arr2[i];
                    arr2[i] = tmp;
                    break;
                }
 
                else if (diff1 == diff2)
                {
                    if (index1 <= index2)
                    {
                        int tmp = arr1[index1];
                        arr1[index1] = arr2[i];
                        arr2[i] = tmp;
                        break;
                    }
 
                    else if (index2 <= index1)
                    {
                        int tmp = arr1[i];
                        arr1[i] = arr2[index2];
                        arr2[index2] = tmp;
                        break;
                    }
                }
            }
        }
    }
 
    // Restore the new numbers
    int f_n1 = 0, f_n2 = 0;
    for(int i = l1 - 1; i >= 0; i--)
    {
        f_n1 = (f_n1 * 10) + arr1[i];
        f_n2 = (f_n2 * 10) + arr2[i];
    }
 
    // Display the maximized sum
    Console.Write(f_n1 + f_n2);
}
 
// Driver code
public static void Main(string[] args)
{
    int N1 = 9987;
    int N2 = 123;
 
    findMaxSum(N1, N2);
}
}
 
// This code is contributed by rutvik_56


输出:
10740