📌  相关文章
📜  最大数目,允许一次交换

📅  最后修改于: 2021-06-26 20:11:56             🧑  作者: Mango

给定一个正整数,找到最多可以交换一次最多两位数字才能生成的最大数字。

例子:

Input: 2736
Output : 7236
Explanation:
If we swap the number 2 and the number
7 then the generated number would be 
the largest number.

Input : 432
Output : 432
Explanation:
Here, no swap is required. The given
number is already largest.

方法1(尝试每对):
我们将数字转换为字符串。对于数字的每个数字,我们将与位置(i,j)交换,并将其存储为temp,检查temp是否大于数字。如果是,则交换回来以恢复原始编号。

C++
// code to find largest number with
// given conditions.
  
#include 
using namespace std;
  
// function to find the largest number
// with given conditions.
int largestNum(int num)
{
    // converting the number to the string
    string num_in_str = to_string(num);
    string temp = num_in_str;
  
    // swamping each digit
    for (int i = 0; i < num_in_str.size(); i++) {
        for (int j = i + 1; j < num_in_str.size(); j++) {
  
            // Swapping and checking for the larger
            swap(num_in_str[i], num_in_str[j]);
            if (stoi(num_in_str) > stoi(temp))
                temp = num_in_str;
  
            // Reverting the changes
            swap(num_in_str[i], num_in_str[j]);
        }
    }
  
    return stoi(temp);
}
  
// driver function
int main()
{
    int num = 432;
    cout << largestNum(num) << endl;
    num = 2736;
    cout << largestNum(num) << endl;
    num = 4596;
    cout << largestNum(num) << endl;
    return 0;
}


Java
// Java code to find largest number
// with given conditions.
public class LargestNumber {
  
    static String swap(String str, int i, int j)
    {
        char ch[] = str.toCharArray();
        char temp = ch[i];
        ch[i] = ch[j];
        ch[j] = temp;
        String c = String.valueOf(ch);
        return c;
    }
  
    // function to find the largest number
    // with given conditions.
    static int largestNum(int num)
    {
        // converting the number to the string
        String num_in_str = "" + num;
        String temp = num_in_str;
  
        // swamping each digit
        for (int i = 0; i < num_in_str.length(); i++) {
            for (int j = i + 1; j < num_in_str.length();
                 j++) {
  
                // Swapping and checking for the larger
                num_in_str = swap(num_in_str, i, j);
                if (temp.compareTo(num_in_str) < 0)
                    temp = num_in_str;
  
                // Reverting the changes
                num_in_str = swap(num_in_str, i, j);
            }
        }
  
        return Integer.parseInt(temp);
    }
  
    // Driver code
    public static void main(String[] s)
    {
        int num = 423;
        System.out.println(largestNum(num));
        num = 2736;
        System.out.println(largestNum(num));
        num = 4596;
        System.out.println(largestNum(num));
    }
}
  
// This code is contributed by Prerena Saini


Python3
# python3 code to find the largest 
# number with given conditions.
  
# function to find the largest number
def largestNum(num):
  
    # converting the number to the list
    num_to_str = list(str(num))
    temp = num_to_str[:]
  
    # swaping each digit and check for
    # the largest number
    for i in range(len(num_to_str)):
        for j in range(i + 1, len(num_to_str)):
  
            // Swapping current pair
            num_to_str[i], num_to_str[j] = num_to_str[j], num_to_str[i]
            if num_to_str > temp:
                temp = num_to_str[:]
  
            # Reverting above change before next iteration 
            num_to_str[i], num_to_str[j] = num_to_str[j], num_to_str[i]
  
    # returning the largest number.
    return int("".join(temp))
  
# main function
def main():
    A = int(432)
    print(largestNum(A))
    A = int(2736)
    print(largestNum(A))
    A = int(4596)
    print(largestNum(A))
  
# calling main function
if __name__=="__main__":
    main()


C#
// C# code to find largest number
// with given conditions.
using System;
  
public class LargestNumber {
  
    static String swap(String str, int i, int j)
    {
        char[] ch = str.ToCharArray();
        char temp = ch[i];
        ch[i] = ch[j];
        ch[j] = temp;
        String c = String.Join("", ch);
        return c;
    }
  
    // function to find the largest number
    // with given conditions.
    static int largestNum(int num)
    {
        // converting the number to the string
        String num_in_str = "" + num;
        String temp = num_in_str;
  
        // swamping each digit
        for (int i = 0; i < num_in_str.Length; i++) {
            for (int j = i + 1; j < num_in_str.Length;
                 j++) {
  
                // Swapping and checking for the larger
                num_in_str = swap(num_in_str, i, j);
                if (temp.CompareTo(num_in_str) < 0)
                    temp = num_in_str;
  
                // Reverting the changes
                num_in_str = swap(num_in_str, i, j);
            }
        }
        return Convert.ToInt32(temp);
    }
  
    // Driver code
    public static void Main(String[] s)
    {
        int num = 423;
        Console.WriteLine(largestNum(num));
        num = 2736;
        Console.WriteLine(largestNum(num));
        num = 4596;
        Console.WriteLine(largestNum(num));
    }
}
  
// This code is contributed by 29AjayKumar


C++
// code to find largest number with
// given conditions.
#include 
using namespace std;
  
// function to find the largest number
// with given conditions.
int largestNum(int num)
{
    int max_digit = -1;
    int max_digit_indx = -1;
    int l_indx = -1;
    int r_indx = -1;
  
    // converting the number to string
    string num_in_str = to_string(num);
    for (int i = num_in_str.size() - 1; i >= 0; i--) {
  
        // current digit is the largest by far
        if (num_in_str[i] > max_digit) {
            max_digit = num_in_str[i];
            max_digit_indx = i;
            continue;
        }
  
        // best digit for swap if there is no more
        // such situation on the left side
        if (num_in_str[i] < max_digit) {
            l_indx = i;
            r_indx = max_digit_indx;
        }
    }
  
    // check for is number already in order
    if (l_indx == -1)
        return num;
  
    swap(num_in_str[l_indx], num_in_str[r_indx]);
  
    return stoi(num_in_str);
}
  
// driver function
int main()
{
    int num = 789;
    cout << largestNum(num) << endl;
    num = 49658;
    cout << largestNum(num) << endl;
    num = 2135;
    cout << largestNum(num) << endl;
    return 0;
}


Java
// Java to find largest number with
// given conditions.
class GFG {
  
    // function to find the largest number
    // with given conditions.
    static int largestNum(int num)
    {
        int max_digit = -1;
        int max_digit_indx = -1;
        int l_indx = -1;
        int r_indx = 1;
  
        // converting the number to string
        String num_in_str = String.valueOf(num);
        for (int i = num_in_str.length() - 1; i >= 0; i--) {
            // current digit is the largest by far
            if (num_in_str.charAt(r_indx) > max_digit) {
                max_digit = num_in_str.charAt(i);
                max_digit_indx = i;
                continue;
            }
  
            // best digit for swap if there is no more
            // such situation on the left side
            if (num_in_str.charAt(i) < max_digit) {
                l_indx = i;
                r_indx = max_digit_indx;
            }
        }
  
        // check for is number already in order
        if (l_indx == -1)
            return num;
  
        num_in_str = swap(num_in_str, l_indx, r_indx);
  
        return Integer.parseInt(num_in_str);
    }
    static String swap(String arr, int i, int j)
    {
        char temp[] = arr.toCharArray();
        char c = temp[i];
        temp[i] = temp[j];
        temp[j] = c;
        return String.valueOf(temp);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int num = 789;
        System.out.println(largestNum(num));
        num = 49658;
        System.out.println(largestNum(num));
        num = 2135;
        System.out.println(largestNum(num));
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 to find largest number with 
# given conditions.
  
# Function to find the largest number 
# with given conditions.
def largestNum(num):
    max_digit = -1
    max_digit_indx = -1
    l_indx = -1
    r_indx = 1
  
    # converting the number to string
    num_in_str = list(str(num))
    for i in range(len(num_in_str) - 1, -1, -1):
          
        # current digit is the largest by far
        if int(num_in_str[r_indx]) > int(max_digit):
            max_digit = num_in_str[i]
            max_digit_indx = i
            continue
  
        # best digit for swap if there is no more 
        # such situation on the left side
        if int(num_in_str[i]) < int(max_digit):
            l_indx = i
            r_indx = max_digit_indx
  
    # check for is number already in order
    if l_indx == -1:
        return num
  
    num_in_str[l_indx], num_in_str[r_indx] = \
    num_in_str[r_indx], num_in_str[l_indx] 
  
    return int(''.join(num_in_str))
  
# Driver Code
num = 789
print(largestNum(num))
num = 49658
print(largestNum(num))
num = 2135
print(largestNum(num))
  
# This code is contributed by Gowtham Yuvaraj


C#
// C# to find largest number with
// given conditions.
using System;
  
class GFG {
  
    // function to find the largest number
    // with given conditions.
    static int largestNum(int num)
    {
        int max_digit = -1;
        int max_digit_indx = -1;
        int l_indx = -1;
        int r_indx = 1;
  
        // converting the number to string
        String num_in_str = String.Join("", num);
        for (int i = num_in_str.Length - 1; i >= 0; i--) {
            // current digit is the largest by far
            if (num_in_str[r_indx] > max_digit) {
                max_digit = num_in_str[i];
                max_digit_indx = i;
                continue;
            }
  
            // best digit for swap if there is no more
            // such situation on the left side
            if (num_in_str[i] < max_digit) {
                l_indx = i;
                r_indx = max_digit_indx;
            }
        }
  
        // check for is number already in order
        if (l_indx == -1)
            return num;
  
        num_in_str = swap(num_in_str, l_indx, r_indx);
  
        return int.Parse(num_in_str);
    }
    static String swap(String arr, int i, int j)
    {
        char[] temp = arr.ToCharArray();
        char c = temp[i];
        temp[i] = temp[j];
        temp[j] = c;
        return String.Join("", temp);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int num = 789;
        Console.WriteLine(largestNum(num));
        num = 49658;
        Console.WriteLine(largestNum(num));
        num = 2135;
        Console.WriteLine(largestNum(num));
    }
}
  
/* This code contributed by PrinciRaj1992 */


输出:

432
7236
9546

方法2(有效):
我们将从反方向扫描数字。在扫描中,如果第i个数字是到目前为止最大的,存储它及其索引,或者如果当前数字小于到目前为止记录的最大数字,则该数字和最大的数字最适合交换。

下面是上述方法的实现。

C++

// code to find largest number with
// given conditions.
#include 
using namespace std;
  
// function to find the largest number
// with given conditions.
int largestNum(int num)
{
    int max_digit = -1;
    int max_digit_indx = -1;
    int l_indx = -1;
    int r_indx = -1;
  
    // converting the number to string
    string num_in_str = to_string(num);
    for (int i = num_in_str.size() - 1; i >= 0; i--) {
  
        // current digit is the largest by far
        if (num_in_str[i] > max_digit) {
            max_digit = num_in_str[i];
            max_digit_indx = i;
            continue;
        }
  
        // best digit for swap if there is no more
        // such situation on the left side
        if (num_in_str[i] < max_digit) {
            l_indx = i;
            r_indx = max_digit_indx;
        }
    }
  
    // check for is number already in order
    if (l_indx == -1)
        return num;
  
    swap(num_in_str[l_indx], num_in_str[r_indx]);
  
    return stoi(num_in_str);
}
  
// driver function
int main()
{
    int num = 789;
    cout << largestNum(num) << endl;
    num = 49658;
    cout << largestNum(num) << endl;
    num = 2135;
    cout << largestNum(num) << endl;
    return 0;
}

Java

// Java to find largest number with
// given conditions.
class GFG {
  
    // function to find the largest number
    // with given conditions.
    static int largestNum(int num)
    {
        int max_digit = -1;
        int max_digit_indx = -1;
        int l_indx = -1;
        int r_indx = 1;
  
        // converting the number to string
        String num_in_str = String.valueOf(num);
        for (int i = num_in_str.length() - 1; i >= 0; i--) {
            // current digit is the largest by far
            if (num_in_str.charAt(r_indx) > max_digit) {
                max_digit = num_in_str.charAt(i);
                max_digit_indx = i;
                continue;
            }
  
            // best digit for swap if there is no more
            // such situation on the left side
            if (num_in_str.charAt(i) < max_digit) {
                l_indx = i;
                r_indx = max_digit_indx;
            }
        }
  
        // check for is number already in order
        if (l_indx == -1)
            return num;
  
        num_in_str = swap(num_in_str, l_indx, r_indx);
  
        return Integer.parseInt(num_in_str);
    }
    static String swap(String arr, int i, int j)
    {
        char temp[] = arr.toCharArray();
        char c = temp[i];
        temp[i] = temp[j];
        temp[j] = c;
        return String.valueOf(temp);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int num = 789;
        System.out.println(largestNum(num));
        num = 49658;
        System.out.println(largestNum(num));
        num = 2135;
        System.out.println(largestNum(num));
    }
}
  
// This code contributed by Rajput-Ji

Python3

# Python3 to find largest number with 
# given conditions.
  
# Function to find the largest number 
# with given conditions.
def largestNum(num):
    max_digit = -1
    max_digit_indx = -1
    l_indx = -1
    r_indx = 1
  
    # converting the number to string
    num_in_str = list(str(num))
    for i in range(len(num_in_str) - 1, -1, -1):
          
        # current digit is the largest by far
        if int(num_in_str[r_indx]) > int(max_digit):
            max_digit = num_in_str[i]
            max_digit_indx = i
            continue
  
        # best digit for swap if there is no more 
        # such situation on the left side
        if int(num_in_str[i]) < int(max_digit):
            l_indx = i
            r_indx = max_digit_indx
  
    # check for is number already in order
    if l_indx == -1:
        return num
  
    num_in_str[l_indx], num_in_str[r_indx] = \
    num_in_str[r_indx], num_in_str[l_indx] 
  
    return int(''.join(num_in_str))
  
# Driver Code
num = 789
print(largestNum(num))
num = 49658
print(largestNum(num))
num = 2135
print(largestNum(num))
  
# This code is contributed by Gowtham Yuvaraj

C#

// C# to find largest number with
// given conditions.
using System;
  
class GFG {
  
    // function to find the largest number
    // with given conditions.
    static int largestNum(int num)
    {
        int max_digit = -1;
        int max_digit_indx = -1;
        int l_indx = -1;
        int r_indx = 1;
  
        // converting the number to string
        String num_in_str = String.Join("", num);
        for (int i = num_in_str.Length - 1; i >= 0; i--) {
            // current digit is the largest by far
            if (num_in_str[r_indx] > max_digit) {
                max_digit = num_in_str[i];
                max_digit_indx = i;
                continue;
            }
  
            // best digit for swap if there is no more
            // such situation on the left side
            if (num_in_str[i] < max_digit) {
                l_indx = i;
                r_indx = max_digit_indx;
            }
        }
  
        // check for is number already in order
        if (l_indx == -1)
            return num;
  
        num_in_str = swap(num_in_str, l_indx, r_indx);
  
        return int.Parse(num_in_str);
    }
    static String swap(String arr, int i, int j)
    {
        char[] temp = arr.ToCharArray();
        char c = temp[i];
        temp[i] = temp[j];
        temp[j] = c;
        return String.Join("", temp);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int num = 789;
        Console.WriteLine(largestNum(num));
        num = 49658;
        Console.WriteLine(largestNum(num));
        num = 2135;
        Console.WriteLine(largestNum(num));
    }
}
  
/* This code contributed by PrinciRaj1992 */

输出:

987
94658
5132

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。