📜  由给定三个数字的数字组成的最大数字

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

由给定三个数字的数字组成的最大数字

给定3个四位整数ABC ,任务是打印从给定数字中相同位置的所有数字中取最大数字所形成的数字。

例子:

方法:这个问题可以通过迭代给定整数的数字来解决。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0P1以存储可能的最大数字和数字的位置值。
  • 迭代直到A、BC大于0并执行以下步骤:
    • 找出数字A、BC的单位位置的数字,并将它们分别存储在变量a、bc中。
    • 将 A 更新为A /10 ,将B更新为B/10 ,将C更新为C/10
    • ans增加P*max(a, b, c) ,然后将 P 更新为P *10。
  • 最后,完成上述步骤后,打印存储在ans中的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum number
// formed by taking the maximum digit
// at the same position from each number
int findkey(int A, int B, int C)
{
    // Stores the result
    int ans = 0;
 
    // Stores the position value of a
    // digit
    int cur = 1;
 
    while (A > 0) {
 
        // Stores the digit at the unit
        // place
        int a = A % 10;
        // Stores the digit at the unit
        // place
        int b = B % 10;
        // Stores the digit at the unit
        // place
        int c = C % 10;
 
        // Update A, B and C
        A = A / 10;
        B = B / 10;
        C = C / 10;
 
        // Stores the maximum digit
        int m = max(a, max(c, b));
 
        // Increment ans cur*a
        ans += cur * m;
 
        // Update cur
        cur = cur * 10;
    }
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int A = 3521, B = 2452, C = 1352;
 
    // Function call
    cout << findkey(A, B, C);
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
 
// Function to find the maximum number
// formed by taking the maximum digit
// at the same position from each number
static int findkey(int A, int B, int C)
{
   
    // Stores the result
    int ans = 0;
 
    // Stores the position value of a
    // digit
    int cur = 1;
 
    while (A > 0) {
 
        // Stores the digit at the unit
        // place
        int a = A % 10;
       
        // Stores the digit at the unit
        // place
        int b = B % 10;
       
        // Stores the digit at the unit
        // place
        int c = C % 10;
 
        // Update A, B and C
        A = A / 10;
        B = B / 10;
        C = C / 10;
 
        // Stores the maximum digit
        int m = Math.max(a, Math.max(c, b));
 
        // Increment ans cur*a
        ans += cur * m;
 
        // Update cur
        cur = cur * 10;
    }
    // Return ans
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    // Given Input
    int A = 3521, B = 2452, C = 1352;
 
    // Function call
    System.out.println(findkey(A, B, C));
    }
}
 
// This code is contributed by SoumikMondal


Python3
# Py program for the above approach
 
# Function to find the maximum number
# formed by taking the maximum digit
# at the same position from each number
def findkey(A, B, C):
    # Stores the result
    ans = 0
 
    # Stores the position value of a
    # digit
    cur = 1
 
    while (A > 0):
 
        # Stores the digit at the unit
        # place
        a = A % 10
        # Stores the digit at the unit
        # place
        b = B % 10
        # Stores the digit at the unit
        # place
        c = C % 10
 
        # Update A, B and C
        A = A // 10
        B = B // 10
        C = C // 10
 
        # Stores the maximum digit
        m = max(a, max(c, b))
 
        # Increment ans cur*a
        ans += cur * m
 
        # Update cur
        cur = cur * 10
 
    # Return ans
    return ans
 
# Driver Code
if __name__ == '__main__':
    # Given Input
    A = 3521
    B = 2452
    C = 1352
 
    # Function call
    print (findkey(A, B, C))
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum number
// formed by taking the maximum digit
// at the same position from each number
static int findkey(int A, int B, int C)
{
   
    // Stores the result
    int ans = 0;
 
    // Stores the position value of a
    // digit
    int cur = 1;
 
    while (A > 0) {
 
        // Stores the digit at the unit
        // place
        int a = A % 10;
       
        // Stores the digit at the unit
        // place
        int b = B % 10;
       
        // Stores the digit at the unit
        // place
        int c = C % 10;
 
        // Update A, B and C
        A = A / 10;
        B = B / 10;
        C = C / 10;
 
        // Stores the maximum digit
        int m = Math.Max(a, Math.Max(c, b));
 
        // Increment ans cur*a
        ans += cur * m;
 
        // Update cur
        cur = cur * 10;
    }
    // Return ans
    return ans;
}
 
// Driver Code
static public void Main ()
{
     
    // Given Input
    int A = 3521, B = 2452, C = 1352;
 
    // Function call
    Console.Write(findkey(A, B, C));
}
}
 
// This code is contributed by sanjoy_62.


Javascript



输出
3552

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