📜  带有两个整数的奇偶回合游戏

📅  最后修改于: 2021-04-23 18:39:52             🧑  作者: Mango

给定三个正整数X,Y和P。此处P表示匝数。每当匝数为奇数时,X就会乘以2,每偶数匝数中Y都会乘以2。任务是在完整的P匝后找到max(X,Y)÷min(X,Y)的值。
例子 :

Input : X = 1, Y = 2, P = 1
Output : 1
As turn is odd, X is multiplied by
2 and becomes 2. Now, X is 2 and Y is also 2. 
Therefore, 2 ÷ 2 is 1.

Input : X = 3, Y = 7, p = 2
Output : 2
Here we have 2 turns. In the 1st turn which is odd
X is multiplied by 2. And the values are 6 and 7. 
In the next turn which is even Y is multiplied by 2.
Now the final values are 6 and 14. Therefore, 14 ÷ 6 is 2.

让我们玩上面的游戏8回合:

| i    | 0 | 1  | 2  | 3  | 4  | 5  | 6  | 7   | 8   |
|------|---|----|----|----|----|----|----|-----|-----|
| X(i) | X | 2X | 2X | 4X | 4X | 8X | 8X | 16X | 16X |
| Y(i) | Y | Y  | 2Y | 2Y | 4Y | 4Y | 8Y | 8Y  | 16Y |

在这里,我们可以轻松发现一个模式:

if i is even, then X(i) = z * X and Y(i) = z * Y.
if i is odd, then X(i) = 2*z * X and Y(i) = z * Y.

这里的z实际上是2的幂。因此,我们可以简单地说-

If P is even output will be max(X, Y) ÷ min(X, Y) 
else output will be max(2*X, Y) ÷ min(2*X, Y).

下面是实现:

C++
// CPP program to find max(X, Y) / min(X, Y)
// after P turns
#include 
using namespace std;
 
int findValue(int X, int Y, int P)
{
    if (P % 2 == 0)
        return (max(X, Y) / min(X, Y));
 
    else
        return (max(2 * X, Y) / min(2 * X, Y));
}
 
// Driver code
int main()
{
    // 1st test case
    int X = 1, Y = 2, P = 1;
    cout << findValue(X, Y, P) << endl;
 
    // 2nd test case
    X = 3, Y = 7, P = 2;
    cout << findValue(X, Y, P) << endl;
}


Java
// Java program to find max(X, Y) / min(X, Y)
// after P turns
import java.util.*;
 
class Even_odd{
    public static int findValue(int X, int Y,
                                        int P)
    {
        if (P % 2 == 0)
            return (Math.max(X, Y) /
                            Math.min(X, Y));
 
        else
            return (Math.max(2 * X, Y) /
                            Math.min(2 * X, Y));
    }
     
    public static void main(String[] args)
    {
        // 1st test case
        int X = 1, Y = 2, P = 1;
        System.out.println(findValue(X, Y, P));
         
        // 2nd test case
        X = 3;
        Y = 7;
        P = 2;
        System.out.print(findValue(X, Y, P));
    }
}
 
//This code is contributed by rishabh_jain


Python3
# Python3 code to find max(X, Y) / min(X, Y)
# after P turns
 
def findValue( X , Y , P ):
    if P % 2 == 0:
        return int(max(X, Y) / min(X, Y))
 
    else:
        return int(max(2 * X, Y) / min(2 * X, Y))
 
# Driver code
# 1st test case
X = 1
Y = 2
P = 1
print(findValue(X, Y, P))
 
# 2nd test case
X = 3
Y = 7
P = 2
print((findValue(X, Y, P)))
 
# This code is contribted by "Sharad_Bhardwaj".


C#
// C# program to find max(X, Y) / min(X, Y)
// after P turns
using System;
 
class GFG
{
    public static int findValue(int X, int Y,
                                        int P)
    {
        if (P % 2 == 0)
            return (Math.Max(X, Y) /
                    Math.Min(X, Y));
 
        else
            return (Math.Max(2 * X, Y) /
                    Math.Min(2 * X, Y));
    }
     
    // Driver code
    public static void Main()
    {
        // 1st test case
        int X = 1, Y = 2, P = 1;
        Console.WriteLine(findValue(X, Y, P));
         
        // 2nd test case
        X = 3;
        Y = 7;
        P = 2;
        Console.WriteLine(findValue(X, Y, P));
    }
}
 
//This code is contributed by vt_m


PHP


Javascript


输出:

1
2

时间复杂度: O(1)