함수 max()

Jmnote (토론 | 기여)님의 2018년 8월 27일 (월) 17:25 판 (→‎C)
MAX
GREATEST

1 C

#include <stdio.h>
#define ARRAYSIZE(A) sizeof(A)/sizeof((A)[0])
int array_max(int a[], int size) {
    int max = a[0];
    for(int i=1; i<size; i++) if(a[i]>max) max=a[i];
    return max;
}
void main() {
   int nums[] = {3, 6, 2, 8, 1};
   int size = ARRAYSIZE(nums);
   printf("%d", array_max(nums,size)); // 8
}

2 C++

#include <iostream>
#include <algorithm>
#define ARRAYSIZE(A) sizeof(A)/sizeof((A)[0])
int main() {
	int nums[] = {3, 6, 2, 8, 1};
	int max = *std::max_element(nums,nums+ARRAYSIZE(nums)-1);
	std::cout << max; // 8
}
#include <iostream>
#define ARRAYSIZE(A) sizeof(A)/sizeof((A)[0])
int array_max(int a[], int size) {
    int max = a[0];
    for(int i=1; i<size; i++) if(a[i]>max) max=a[i];
    return max;
}
int main() {
    int nums[] = {3, 6, 2, 8, 1};
    int size = ARRAYSIZE(nums);
    int max = array_max(nums, size);
    std::cout << max; // 8
}
#include <iostream>
#include <array>
#include <algorithm>
int main() {
    std::array<int,5> arr = {3, 6, 2, 8, 1};
    int max = *std::max_element(arr.begin(),arr.end());
    std::cout << max; // 8
}

3 C#

using System;
using System.Linq;
class Program {
    static void Main() {
        int[] nums = {3, 6, 2, 8, 1};
        Console.WriteLine( nums.Max() );
        // 8
    }
}
using System;
using System.Linq;
class Program {
    static void Main() {
        double[] nums = {3.14, 77.7, 5, -10.5, -4.4};
        Console.WriteLine( nums.Max() );
        // 77.7
    }
}

4 Excel

=MAX(3.14, 77.7, 5, -10.5, -4.4)
// 77.7

5 Java

import java.util.Arrays;
import java.util.Collections;
public class Main {
    public static void main(String args[]) {
        Double[] nums = {3.14, 77.7, 5.0, -10.5, -4.4};
        Double max = Collections.max(Arrays.asList(nums));
        System.out.println( max );
        // 77.7
    }
}
public class Main {
    public static void main(String args[]) {
        double[] nums = {3.14, 77.7, 5.0, -10.5, -4.4};
        double max = nums[0];
        for(double e:nums) if(max<e)max=e;
        System.out.println( max );
        // 77.7
    }
}

6 JavaScript

console.log( Math.max(3.14, 77.7, 5, -10.5, -4.4) );
// 77.7

7 PHP

echo max(3.14, 77.7, 5, -10.5, -4.4);
# 77.7

8 Python

print( max(3.14, 77.7, 5, -10.5, -4.4) )
# 77.7

9 Perl

use List::Util qw( max );
print max(3.14, 77.7, 5, -10.5, -4.4) . "\n";
# returns 77.7

10 SQL

10.1 MySQL/MariaDB

SELECT GREATEST(3.14, 77.7, 5, -10.5, -4.4);
# 77.7

10.2 Oracle

SELECT GREATEST(3.14, 77.7, 5, -10.5, -4.4) FROM DUAL;
-- returns 77.7

11 같이 보기