求数组所有对上给定表达式的最小值
给定一个大小为 N 的数组 A,求所有对(I,j)上表达式的最小值:(其中 I!= j)。这里、和分别代表按位“与”、按位“或”和按位“异或”。 例:
Input: A = [1, 2, 3, 4, 5]
Output: 1
Explanation:
(A[1] and A[2]) xor (A[1] or A[2]) = 1,
which is minimum possible value.
Input : A = [12, 3, 14, 5, 9, 8]
Output : 1
天真方法: 遍历数组中所有索引不同的对,找到给定表达式在它们上面的最小可能值。 以下执行上述办法。
C++
// C++ program to find the minimum
// value of the given expression
// over all pairs of the array
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// value of the expression
int MinimumValue(int a[], int n)
{
int answer = INT_MAX;
// Iterate over all the pairs
// and find the minimum value
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
answer = min(answer,
((a[i] & a[j])
^ (a[i] | a[j])));
}
}
return answer;
}
// Driver code
int main()
{
int N = 6;
int A[N] = { 12, 3, 14, 5, 9, 8 };
cout << MinimumValue(A, N);
return 0;
}
Java 语言(一种计算机语言,尤用于创建网站)
// Java program to find the minimum
// value of the given expression
// over all pairs of the array
import java.io.*;
import java.util.Arrays;
class GFG
{
// Function to find the minimum
// value of the expression
static int MinimumValue(int a[], int n)
{
int answer = Integer.MAX_VALUE;
// Iterate over all the pairs
// and find the minimum value
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
answer = Math.min(answer, ((a[i] & a[j])
^ (a[i] | a[j])));
}
}
return answer;
}
// Driver code
public static void main(String[] args)
{
int N = 6;
int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
System.out.print(MinimumValue(A, N));
}
}
// This code is contributed by shivanisinghss2110
Python 3
# Python3 program to find the minimum
# value of the given expression
# over all pairs of the array
import sys
# Function to find the minimum
# value of the expression
def MinimumValue(a,n):
answer = sys.maxsize
# Iterate over all the pairs
# and find the minimum value
for i in range(n):
for j in range(i + 1, n, 1):
answer = min(answer,((a[i] & a[j])^(a[i] | a[j])))
return answer
# Driver code
if __name__ == '__main__':
N = 6
A = [12, 3, 14, 5, 9, 8]
print(MinimumValue(A, N))
# This code is contributed by Bhupendra_Singh
C
// C# program to find the minimum
// value of the given expression
// over all pairs of the array
using System;
class GFG{
// Function to find the minimum
// value of the expression
static int MinimumValue(int []a, int n)
{
int answer = Int32.MaxValue;
// Iterate over all the pairs
// and find the minimum value
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
answer = Math.Min(answer,
((a[i] & a[j]) ^
(a[i] | a[j])));
}
}
return answer;
}
// Driver Code
public static void Main()
{
int N = 6;
int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
Console.Write(MinimumValue(A, N));
}
}
// This code is contributed by shivanisinghss2110
java 描述语言
<script>
// JavaScript program to find the minimum
// value of the given expression
// over all pairs of the array
// Function to find the minimum
// value of the expression
function MinimumValue(a, n)
{
let answer = Number.MAX_SAFE_INTEGER;
// Iterate over all the pairs
// and find the minimum value
for (let i = 0; i < n; i++)
{
for (let j = i + 1; j < n; j++)
{
answer = Math.min(answer,
((a[i] & a[j])
^ (a[i] | a[j])));
}
}
return answer;
}
// Driver code
let N = 6;
let A = [ 12, 3, 14, 5, 9, 8 ];
document.write(MinimumValue(A, N));
// This code is contributed by Surbhi Tyagi.
</script>
Output:
1
版权属于:月萌API www.moonapi.com,转载请注明出处