This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scn = new Scanner(System.in); | |
int n=scn.nextInt(); | |
while(n--!=0){ | |
int num=scn.nextInt(),arr[]=new int[num],max=0; | |
for(int i=0;i<num;i++){ | |
arr[i]=scn.nextInt(); | |
} | |
int sum=0,max_sum=0; | |
for(int j=0;j<num;j++) | |
{ | |
sum+=arr[j]; | |
sum=Math.max(0,sum); | |
max_sum=Math.max(max_sum,sum); | |
} | |
System.out.println(max_sum); | |
} | |
} | |
/*題目:[C_AR130-易] 拉彩金 | |
作者:1010 | |
時間:西元 2017 年 4 月 */ | |
} |
這題陣列走訪,取得最少時間複雜度用單迴圈就行了,每走訪一個就加到sum,並每次判斷sum是否小於0若成立歸零Math.max()就是比較大小函式,比對確認後最後再比目前最大的數字並存入max_sum
沒有留言:
張貼留言