通过在每个步骤中添加 1 或 A * 10^c 来最小化将 K 从 0 转换为 B 的操作
给定三个数字A、B和K ,其中 K 最初为0 。任务是使用以下操作找到将K转换为B的最小操作:
- 将1加到K即K = K + 1
- 在K中添加A * 10 c即K = K + A * 10 c ,其中 c 是任何整数( c >= 0 )。
例子:
Input: A = 2, B = 7
Output: 4
Explanation: Initially K = 0, following operations are done on K:
- K = K + A * 100 => K = 0 + 2 * 1 => K= 2
- K = K + A * 100 => K = 2 + 2 * 1 => K= 4
- K = K + A * 100 => K = 4 + 2 * 1 => K= 6
- Add 1 to K => K = 7
Therefore, minimum operations needed are 4
Input: A = 25, B = 1337
Output: 20
方法:一个数可以表示为B = X * A + Y ,其中 A 是乘以 X 的最大数,它们的乘积最接近 B,Y 是小于 A 的数。按照以下步骤操作解决这个问题:
- 初始化一个变量K并将X赋值给它。
- 将 K 乘以10直到它大于B 。
- 用 0 初始化变量 ans。
- 使用模运算符ans = B % A 将Y部分存储在 ans 中。
- 从 B中减去ans 说B = B – ans 。
- 现在模B 乘 K 直到 K 大于或等于 A。
- 并将B/K的划分存储在 ans 中。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum operations
int minOperations(int A, int B)
{
// Initialize K and assign A into K
int K = A;
// Calculate the nearest
// smaller number with B
while (K < B) {
K = K * 10;
// If K is larger than B divide
// by 10 and break the loop
// We can get the nearest number
if (K > B) {
K = K / 10;
break;
}
}
// Now according to approach
// Y part is B % A
// which is smaller than X
int ans = B % A;
// Subtract Y part from B
B = B - ans;
// Iterate until K is
// Greater than or equal to A
while (K >= A) {
// store ans which is division number
ans = ans + B / K;
// Modulus B by K
B = B % K;
// Divide K by 10
K = K / 10;
}
return ans;
}
// Driver Code
int main()
{
int A = 25, B = 1337;
int ans = minOperations(A, B);
cout << ans;
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to find the minimum operations
static int minOperations(int A, int B)
{
// Initialize K and assign A into K
int K = A;
// Calculate the nearest
// smaller number with B
while (K < B) {
K = K * 10;
// If K is larger than B divide
// by 10 and break the loop
// We can get the nearest number
if (K > B) {
K = K / 10;
break;
}
}
// Now according to approach
// Y part is B % A
// which is smaller than X
int ans = B % A;
// Subtract Y part from B
B = B - ans;
// Iterate until K is
// Greater than or equal to A
while (K >= A) {
// store ans which is division number
ans = ans + B / K;
// Modulus B by K
B = B % K;
// Divide K by 10
K = K / 10;
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int A = 25, B = 1337;
int ans = minOperations(A, B);
System.out.print(ans);
}
}
// This code is contributed by sanjoy_62.
Python
# Python program for the above approach
# Function to find the minimum operations
def minOperations(A, B):
# Initialize K and assign A into K
K = A
# Calculate the nearest
# smaller number with B
while (K < B):
K = K * 10
# If K is larger than B divide
# by 10 and break the loop
# We can get the nearest number
if (K > B):
K = K // 10
break
# Now according to approach
# Y part is B % A
# which is smaller than X
ans = B % A
# Subtract Y part from B
B = B - ans
# Iterate until K is
# Greater than or equal to A
while (K >= A):
# store ans which is division number
ans = ans + B // K
# Modulus B by K
B = B % K
# Divide K by 10
K = K // 10
return ans
# Driver Code
A = 25
B = 1337
ans = minOperations(A, B)
print(ans)
# This code is contributed by Samim Hossain Mondal.
C#
// C# code for the above approach
using System;
class GFG
{
// Function to find the minimum operations
static int minOperations(int A, int B)
{
// Initialize K and assign A into K
int K = A;
// Calculate the nearest
// smaller number with B
while (K < B)
{
K = K * 10;
// If K is larger than B divide
// by 10 and break the loop
// We can get the nearest number
if (K > B)
{
K = K / 10;
break;
}
}
// Now according to approach
// Y part is B % A
// which is smaller than X
int ans = B % A;
// Subtract Y part from B
B = B - ans;
// Iterate until K is
// Greater than or equal to A
while (K >= A)
{
// store ans which is division number
ans = ans + B / K;
// Modulus B by K
B = B % K;
// Divide K by 10
K = K / 10;
}
return ans;
}
// Driver Code
public static void Main()
{
int A = 25, B = 1337;
int ans = minOperations(A, B);
Console.Write(ans);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
20
时间复杂度: O(1)
辅助空间: O(1)