在对 A 和 B 进行交替 AND-OR 操作后找到 A 的最终值
给定 3 个整数A 、 B和N ,任务是对A和B执行交替的 AND-OR运算,然后将每个运算的结果分配给A 。找到A的最终值
例子:
Input: A = 4, B = 5, N = 1
Output: 4
Explanation: Perform 1 operation i.e A = A & B, therefore A = 4 & 5 = 4
Input: A = 4, B = 5, N = 1000
Output: 5
天真的方法:可以使用观察来解决任务。可以观察到,如果数字N是Odd ,答案将是A AND B 。如果数字 N 是偶数,答案将是A OR B。这是因为 AND-OR 操作交替运行。
下面是上述方法的实现:
C++
/*package whatever //do not write package name here */
#include
using namespace std;
int main()
{
int A = 4;
int B = 5;
int n = 1000;
int N = 1;
for (N = 1; N <= n; N++) {
if ((N % 2) != 0) {
A = A & B;
}
else {
A = A | B;
}
}
cout << "Output is:" << A;
return 0;
}
// This code is contributed by rakeshsahni
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int A=4;
int B=5;
int n =1000;
int N = 1;
for(N=1;N<=n;N++)
{
if ((N % 2) != 0) {
A = A & B;
} else {
A = A | B;
}}
System.out.println("Output is:" + A);
}
}
Python3
# Python program for above approach
A = 4
B = 5
n = 1000
N = 1
for N in range(1, n+1):
if ((N % 2) != 0):
A = A & B
else:
A = A | B
print("Output is:", end="")
print(A)
# This code is contributed by ninja_hattori.
C#
// C# program to implement the approach
using System;
class GFG {
public static void Main()
{
int A=4;
int B=5;
int n =1000;
int N = 1;
for(N=1;N<=n;N++)
{
if ((N % 2) != 0) {
A = A & B;
} else {
A = A | B;
}}
Console.Write("Output is:" + A);
}
}
// This code is contributed by sanjoy_62.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the resultant value of A
int find(int A, int B, int N)
{
if (N == 1)
return A & B;
else
return B;
}
// Driver Code
int main()
{
cout << find(4, 5, 1000);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find the resultant value of A
public static int find(int A, int B, int N)
{
if (N == 1)
return A & B;
else
return B;
}
// Driver Code
public static void main(String[] args)
{
System.out.print(find(4, 5, 1000));
}
}
// This code is contributed by Taranpreet
Python3
# Python code for the above approach
# Function to find the resultant value of A
def find(A, B, N):
if (N == 1):
return A & B;
else:
return B;
# Driver Code
print(find(4, 5, 1000));
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the resultant value of A
static int find(int A, int B, int N)
{
if (N == 1)
return A & B;
else
return B;
}
// Driver Code
public static void Main()
{
Console.Write(find(4, 5, 1000));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Output is:5
方法:可以使用观察来解决任务。可以观察到答案将是A ,仅当N为1时,对于其余N值,答案将为B 。这是因为,在第一次操作之后,两个数字都将等于B
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the resultant value of A
int find(int A, int B, int N)
{
if (N == 1)
return A & B;
else
return B;
}
// Driver Code
int main()
{
cout << find(4, 5, 1000);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find the resultant value of A
public static int find(int A, int B, int N)
{
if (N == 1)
return A & B;
else
return B;
}
// Driver Code
public static void main(String[] args)
{
System.out.print(find(4, 5, 1000));
}
}
// This code is contributed by Taranpreet
Python3
# Python code for the above approach
# Function to find the resultant value of A
def find(A, B, N):
if (N == 1):
return A & B;
else:
return B;
# Driver Code
print(find(4, 5, 1000));
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the resultant value of A
static int find(int A, int B, int N)
{
if (N == 1)
return A & B;
else
return B;
}
// Driver Code
public static void Main()
{
Console.Write(find(4, 5, 1000));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
5
输出
5
时间复杂度:O(1)
辅助空间:O(1)