由给定三个数字的数字组成的最大数字
给定3个四位整数A 、 B和C ,任务是打印从给定数字中相同位置的所有数字中取最大数字所形成的数字。
例子:
Input: A = 3521, B = 2452, C = 1352
Output: 3552
Explanation:
- The maximum of the digits that are at 1th place is equal to max(A[3] = 1, B[3] = 2, C[3] = 2) 2.
- The maximum of the digits that are at 10th place is equal to max(A[2] = 2, B[2] = 5, C[2] = 5) 5.
- The maximum of the digits that are at 100th place is equal to max(A[1] = 5, B[1] = 4, C[1] = 3) 5.
- The maximum of the digits that are at 1000th place is equal to max(A[0] = 3, B[0] = 3, C[0] = 1) 3.
Therefore, the number formed is 3552.
Input: A = 11, B = 12, C = 22
Output: 22
方法:这个问题可以通过迭代给定整数的数字来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如ans为0和P为1以存储可能的最大数字和数字的位置值。
- 迭代直到A、B 、 C大于0并执行以下步骤:
- 找出数字A、B和C的单位位置的数字,并将它们分别存储在变量a、b和c中。
- 将 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)