📌  相关文章
📜  在给定条件下改变时找到最终的X和Y

📅  最后修改于: 2021-04-22 04:08:46             🧑  作者: Mango

给定两个正整数XY的初始值,请根据以下更改找到XY的最终值:

例子:

Input: X=12, Y=5
Output: X=0, Y=1
Explanation: 
    Initially X = 12, Y = 5 
    --> X = 2, Y = 5 (as X = X-2*Y)
    --> X = 2, Y = 1 (as Y = Y-2*X) 
    --> X = 0, Y = 1 (as X = X-2*Y)
    --> Stop         (as X = 0)

Input: X=31, Y=12
Output: X=7, Y=12
Explanation: 
    Initially X = 31, Y = 12 
    --> X = 7, Y = 12 (as X = X-2*Y)
    --> Stop          (as (Y - 2*X) < 0)

方法:由于X和Y的初始值可以高达10 ^ 18。简单的暴力破解方法将行不通。
如果我们仔细观察,问题陈述只不过是一种欧几里得算法,我们将用模替换所有减法。
执行:

C++
// CPP tp implement above approach
 
#include 
using namespace std;
 
// Function to get final value of X and Y
void alter(long long int x, long long int y)
{
    // Following the sequence
    // but by replacing minus with modulo
    while (true) {
 
        // Step 1
        if (x == 0 || y == 0)
            break;
 
        // Step 2
        if (x >= 2 * y)
            x = x % (2 * y);
 
        // Step 3
        else if (y >= 2 * x)
            y = y % (2 * x);
 
        // Otherwise terminate
        else
            break;
    }
 
    cout << "X=" << x << ", "
         << "Y=" << y;
}
 
// Driver function
int main()
{
 
    // Get the initial X and Y values
    long long int x = 12, y = 5;
 
    // Find the result
    alter(x, y);
 
    return 0;
}


Java
// Java tp implement above approach
import java.io.*;
 
class GFG
{
     
// Function to get final value of X and Y
static void alter(long x, long y)
{
    // Following the sequence but by
    // replacing minus with modulo
    while (true)
    {
 
        // Step 1
        if (x == 0 || y == 0)
            break;
 
        // Step 2
        if (x >= 2 * y)
            x = x % (2 * y);
 
        // Step 3
        else if (y >= 2 * x)
            y = y % (2 * x);
 
        // Otherwise terminate
        else
            break;
    }
 
    System.out.println("X = " + x + ", " +
                       "Y = " + y);
}
 
// Driver Code
public static void main (String[] args)
{
    // Get the initial X and Y values
    long x = 12, y = 5;
     
    // Find the result
    alter(x, y);
}
}
 
// This code is contributed by
// shk


Python3
# Python3 tp implement above approach
import math as mt
 
# Function to get final value of X and Y
def alter(x, y):
 
    # Following the sequence but by
    # replacing minus with modulo
    while (True):
 
        # Step 1
        if (x == 0 or y == 0):
            break
 
        # Step 2
        if (x >= 2 * y):
            x = x % (2 * y)
 
        # Step 3
        elif (y >= 2 * x):
            y = y % (2 * x)
 
        # Otherwise terminate
        else:
            break
     
    print("X =", x, ", ", "Y =", y)
 
 
# Driver Code
 
# Get the initial X and Y values
x, y = 12, 5
 
# Find the result
alter(x, y)
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to get final value of X and Y
    static void alter(long x, long y)
    {
    // Following the sequence but by
    // replacing minus with modulo
    while (true)
    {
     
        // Step 1
        if (x == 0 || y == 0)
            break;
         
        // Step 2
        if (x >= 2 * y)
            x = x % (2 * y);
         
        // Step 3
        else if (y >= 2 * x)
            y = y % (2 * x);
         
        // Otherwise terminate
        else
            break;
        }
     
        Console.WriteLine("X = " + x + ", " + "Y = " + y);
    }
     
    // Driver Code
    public static void Main ()
    {
        // Get the initial X and Y values
        long x = 12, y = 5;
         
        // Find the result
        alter(x, y);
    }
}
 
// This code is contributed by aishwarya.27


PHP
= 2 * $y)
            $x = $x % (2 * $y);
 
        // Step 3
        else if ($y >= 2 * $x)
            $y = $y % (2 * $x);
 
        // Otherwise terminate
        else
            break;
    }
 
    echo "X = ", $x, ", ", "Y = ", $y;
}
 
// Driver Code
 
// Get the initial X and Y values
$x = 12 ;
$y = 5 ;
 
// Find the result
alter($x, $y);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
X=0, Y=1