SWEA 5642 (Professional) 합

1 개요[ | ]

SWEA 5642 (Professional) 합
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 3-8

2 C++[ | ]

3 Java[ | ]

import java.io.*;
import java.util.*;
public class Solution {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws NumberFormatException, IOException {
		int T = Integer.parseInt(br.readLine());
		for(int tc=1; tc<=T; tc++) {
			int N = Integer.parseInt(br.readLine());
			int[] a = new int[N];
			StringTokenizer st = new StringTokenizer(br.readLine());
			for(int i=0; i<N; i++) a[i] = Integer.parseInt(st.nextToken());
			int max=-9999, sum = 0;
			for(int i=0; i<N; i++) {
				sum += a[i];
				if( sum > max ) max = sum;
				if( sum < 0 ) sum = 0;
			}
			System.out.format("#%d %d\n", tc, max);
		}
	}
}
import java.util.*;
public class Solution {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int T = sc.nextInt();
		for(int tc=1; tc<=T; tc++) {
			int N = sc.nextInt();
			int[] a = new int[N];
			for(int i=0; i<N; i++) a[i] = sc.nextInt();
			int max=-9999, sum = 0;
			for(int i=0; i<N; i++) {
				sum += a[i];
				if( sum > max ) max = sum;
				if( sum < 0 ) sum = 0;
			}
			System.out.format("#%d %d\n", tc, max);
		}
	}
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}