📜  不使用条件/按位/三元运算符的数组中最大的

📅  最后修改于: 2022-05-13 01:57:48.887000             🧑  作者: Mango

不使用条件/按位/三元运算符的数组中最大的

定义一个函数int max(int a[], int n) 从包含 n 个元素的整数数组中返回最大整数(不使用条件/按位/三元运算符/库函数来查找最大
例子:

Input : arr[] = {16, 14, 15, 17, 9}
Output : 17

Input : arr[] = {10, 13, 11, 13}
Output : 13

这个想法类似于最多四个数字。
我们使用“(x – y + abs(x – y))”的值将是 0 的事实,即 x 小于或等于 y。我们将此值用作大小为 2 的数组中的索引来选择最大值。一旦我们找到了两个元素的最大值,我们就可以使用相同的技术来找到所有元素的最大值。

C++
// CPP program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
#include 
using namespace std;
 
int maxArray(int a[], int n)
{
    int tmp[2];
    tmp[0] = a[0];
    for (int i = 1; i < n; i++) {
        tmp[1] = a[i];
        swap(tmp[0], tmp[bool(abs(tmp[1] - tmp[0]) +
                                  tmp[1] - tmp[0])]);
    }
    return tmp[0];
}
 
// Driver code
int main()
{
    int a[] = { 15, 11, 17, 16, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << maxArray(a, n);
    return 0;
}


Java
// JAVA program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
import java.util.*;
class GFG
{
 
static int maxArray(int a[], int n)
{
    int []tmp = new int[2];
    tmp[0] = a[0];
    for (int i = 1; i < n-1; i++) {
        tmp[1] = a[i];
       
        int temp = tmp[0];
        tmp[0] = tmp[(Math.abs(tmp[1] - tmp[0]) +
                tmp[1] - tmp[0])%2+1];
        tmp[(Math.abs(tmp[1] - tmp[0]) +
                tmp[1] - tmp[0])%2+1] = temp;
    }
    return tmp[1];
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 15, 11, 17, 16, 10 };
    int n =a.length;
    System.out.print(maxArray(a, n));
}
}
 
// This code is contributed by umadevi9616


Python3
# Python 3 program to find largest in an array
# without conditional/bitwise/ternary/ operators
# and without library functions.
 
def maxArray(a, n):
    tmp = [a[i] for i in range(len(a))]
    tmp[0] = a[0]
    for i in range(1,n,1):
        tmp[1] = a[i]
        temp = tmp[int(bool(abs(tmp[1] - tmp[0]) +
                                tmp[1] - tmp[0]))]
        tmp[int(bool(abs(tmp[1] - tmp[0]) +
                         tmp[1] - tmp[0]))] = tmp[0]
        tmp[0] = temp
     
    return tmp[0]
 
# Driver code
if __name__ == '__main__':
    a = [15, 11, 17, 16, 10]
    n = len(a)
    print(maxArray(a, n))
         
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find largest in an array
// without conditional/bitwise/ternary/ operators
// and without library functions.
using System;
 
public class GFG {
 
    static int maxArray(int[] a, int n)
    {
        int[] tmp = new int[2];
        tmp[0] = a[0];
        for (int i = 1; i < n - 1; i++) {
            tmp[1] = a[i];
 
            int temp = tmp[0];
            tmp[0] = tmp[(Math.Abs(tmp[1] - tmp[0]) + tmp[1]
                          - tmp[0]) % 2 + 1];
            tmp[(Math.Abs(tmp[1] - tmp[0]) + tmp[1]
                 - tmp[0]) % 2 + 1] = temp;
        }
        return tmp[1];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] a = { 15, 11, 17, 16, 10 };
        int n = a.Length;
        Console.Write(maxArray(a, n));
    }
}
 
// This code is contributed by umadevi9616


Javascript


输出:

17