2012/02/28

[zerjudge] d074 電腦教室

題目:https://zerojudge.tw/ShowProblem?problemid=d074
說明:找出最大值,逐一檢視,複雜度為 O(n)

Java 版
import java.util.Scanner;

public class D074 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = Integer.parseInt(in.nextLine());
            String [] str = in.nextLine().split(" ");
            int max = Integer.MIN_VALUE;
            for(int a = 0; a < n; a++) {
                if(max < Integer.parseInt(str[a]))
                    max = Integer.parseInt(str[a]);
            }
            System.out.println(max);
        }
    }
}
Python 版 (2022.07)
n = int(input())
arr = list(map(int, input().split(' ')))
m = arr[0]
for i in range(1, n):
    if m < arr[i]:
        m = arr[i]
print(m)

沒有留言:

張貼留言