给定一对正数x和y。我们从一个较大的整数中重复减去两个整数中的较小一个,直到整数之一变为0。我们的任务是在停止之前计算到的步数(其中一个变为0)。
例子 :
Input : x = 5, y = 13
Output : 6
Explanation : There are total 6 steps before
we reach 0:
(5,13) --> (5,8) --> (5,3) --> (2,3)
--> (2,1) --> (1,1) --> (1,0).
Input : x = 3, y = 5
Output : 4
Explanation : There are 4 steps:
(5,3) --> (2,3) --> (2,1) --> (1,1) --> (1,0)
Input : x = 100, y = 19
Output : 13
一个简单的解决方案是实际遵循该过程并计算步骤数。
更好的解决方案是使用以下步骤。令y为两个数字中的较小者
1)如果y除以x然后返回(x / y)
2)else return((x / y)+ solve(y,x%y))
插图 :
如果我们以(x,y)开头并且y除以x,则答案将是(x / y),因为我们可以精确地(x / y)乘以x形式的y。
对于另一种情况,我们举一个例子来看看它是如何工作的:(100,19)
我们可以精确地从100减去19来获得[100/19] = 5倍,得到(19,5)。
我们可以精确地从19的[19/5] = 3乘以5以得到(5,4)。
我们可以从5精确地[5/4] = 1乘以4得到(4,1)。
我们可以从4中精确地减去1 [4/1] = 4倍以获得(1,0)
因此总共5 + 3 + 1 + 4 = 13步。
下面是基于以上思想的实现。
C++
// C++ program to count of steps until one
// of the two numbers become 0.
#include
using namespace std;
// Returns count of steps before one
// of the numbers become 0 after repeated
// subtractions.
int countSteps(int x, int y)
{
// If y divides x, then simply return
// x/y.
if (x%y == 0)
return x/y;
// Else recur. Note that this function
// works even if x is smaller than y because
// in that case first recursive call exchanges
// roles of x and y.
return x/y + countSteps(y, x%y);
}
// Driver code
int main()
{
int x = 100, y = 19;
cout << countSteps(x, y);
return 0;
}
Java
// Java program to count of
// steps until one of the
// two numbers become 0.
import java.io.*;
class GFG
{
// Returns count of steps
// before one of the numbers
// become 0 after repeated
// subtractions.
static int countSteps(int x,
int y)
{
// If y divides x, then
// simply return x/y.
if (x % y == 0)
return x / y;
// Else recur. Note that this
// function works even if x is
// smaller than y because
// in that case first recursive
// call exchanges roles of x and y.
return x / y + countSteps(y, x % y);
}
// Driver code
public static void main (String[] args)
{
int x = 100, y = 19;
System.out.println(countSteps(x, y));
}
}
// This code is contributed by aj_36
Python3
# Python3 program to count of steps until
# one of the two numbers become 0.
import math
# Returns count of steps before one of
# the numbers become 0 after repeated
# subtractions.
def countSteps(x, y):
# If y divides x, then simply
# return x/y.
if (x % y == 0):
return math.floor(x / y);
# Else recur. Note that this function
# works even if x is smaller than y
# because in that case first recursive
# call exchanges roles of x and y.
return math.floor((x / y) +
countSteps(y, x % y));
# Driver code
x = 100;
y = 19;
print(countSteps(x, y));
# This code is contributed by mits
C#
// C# program to count of
// steps until one of the
// two numbers become 0.
using System;
class GFG
{
// Returns count of steps
// before one of the numbers
// become 0 after repeated
// subtractions.
static int countSteps(int x,
int y)
{
// If y divides x, then
// simply return x/y.
if (x % y == 0)
return x / y;
// Else recur. Note that this
// function works even if x is
// smaller than y because
// in that case first recursive
// call exchanges roles of x and y.
return x / y + countSteps(y, x % y);
}
// Driver Code
static public void Main ()
{
int x = 100, y = 19;
Console.WriteLine(countSteps(x, y));
}
}
// This code is contributed by m_kit
PHP
Javascript
输出 :
13
时间复杂度: O(log(n))