標題:
最大連續子序列和
[打印本頁]
作者:
tonyh
時間:
2023-2-3 19:51
標題:
最大連續子序列和
本帖最後由 tonyh 於 2023-2-10 19:51 編輯
試使用動態規劃法(DP),求最大連續子序列和。
index 0 1 2 3 4 5 6 7 8
data[ ] -2 1 -3 4 -1 2 1 -5 4
curMaxSum -2 1 -2 4 3 5 6 1 5
totMaxSum -2 1 1 4 4 5 6 6 6
範例輸入 1:-2 1 -3 4 -1 2 1 -5 4
範例輸出 1:6
範例輸入 2:0 -2 3 5 -1 2
範例輸出 2:9
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Ch01 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int curMaxSum, totMaxSum;
Ch01() throws Exception
{
raw=br.readLine().split(" ");
for(int i=0, len=raw.length; i<len; i++)
{
int t=Integer.parseInt(raw[i]);
if(i==0)
{
curMaxSum=t;
totMaxSum=t;
}
curMaxSum=Math.max(curMaxSum+t, t);
totMaxSum=Math.max(curMaxSum, totMaxSum);
}
System.out.println(totMaxSum);
}
public static void main(String[] args) throws Exception{
new Ch01();
}
}
複製代碼
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Ch01 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int len, data[];
Ch01() throws Exception
{
raw=br.readLine().split(" ");
len=raw.length;
data=new int[len];
for(int i=0; i<len; i++)
data[i]=Integer.parseInt(raw[i]);
System.out.println(maxSubArray(data));
}
int maxSubArray(int data[])
{
int curMaxSum=data[0];
int totMaxSum=data[0];
for(int i=1; i<len; i++)
{
curMaxSum=Math.max(curMaxSum+data[i],data[i]);
totMaxSum=Math.max(totMaxSum, curMaxSum);
}
return totMaxSum;
}
public static void main(String[] args) throws Exception{
new Ch01();
}
}
/*
-2 11 -4 13 -5 -2
11-4+13=20
-2 1 -3 4 -1 2 1 -5 4
4-1+2+1=6
1 -2 3 5 -3 2
3+5=8
0 -2 3 5 -1 2
3+5-1+2=9
-9 -2 -3 -5 -3
-2=-2
*/
複製代碼
作者:
劉愷鈞
時間:
2023-2-10 19:59
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class f314 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int curmaxsum,totmaxsum;
f314() throws IOException
{
raw=br.readLine().split(" ");
for(int i=0;i<raw.length;i++)
{
int t=Integer.parseInt(raw[i]);
if(i==0)
{
curmaxsum=t;
totmaxsum=t;
}
curmaxsum=Math.max(curmaxsum+t, t);
totmaxsum=Math.max(curmaxsum, totmaxsum);
}
System.out.println(totmaxsum);
}
public static void main(String[] args) throws IOException {
new f314();
}
}
複製代碼
作者:
董宸佑
時間:
2023-2-10 20:03
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Ch01 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int cMS, tMS;
Ch01() throws Exception{
raw=br.readLine().split(" ");
for(int i=0, len=raw.length; i<len; i++){
int t=Integer.parseInt(raw[i]);
if(i==0){
cMS=t;
tMS=t;
}
cMS=Math.max(t, cMS+t);
tMS=Math.max(cMS, tMS);
}
System.out.println(tMS);
}
public static void main(String[] args) throws Exception {
new Ch01();
}
}
複製代碼
作者:
陳宥穎
時間:
2023-2-10 20:04
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Ch01 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int crr,tot,data;
String raw[];
Ch01() throws Exception
{
raw=br.readLine().split(" ");
for(int i=0; i<raw.length; i++)
{
data=Integer.parseInt(raw[i]);
if(i==0)
{
crr=data;
tot=data;
}
crr=Math.max(crr+data, data);
tot=Math.max(crr, tot);
}
System.out.println(tot);
}
public static void main(String[] args) throws Exception {
new Ch01();
}
//-2 1 -3 4 -1 2 1 -5 4
}
複製代碼
作者:
孫嘉駿
時間:
2023-2-10 20:04
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Ch01 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int curMaxSum, totMaxSum;
Ch01() throws Exception
{
raw=br.readLine().split(" ");
for(int i=0, len=raw.length; i<len; i++)
{
int t=Integer.parseInt(raw[i]);
if(i==0)
{
curMaxSum=t;
totMaxSum=t;
}
curMaxSum=Math.max(curMaxSum+t, t);
totMaxSum=Math.max(curMaxSum, totMaxSum);
}
System.out.println(totMaxSum);
}
public static void main(String[] args) throws Exception{
new Ch01();
}
}
複製代碼
作者:
李穎俊
時間:
2023-2-10 20:05
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class P1 {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int cMS, tMS;
P1() throws Exception
{
for (int i = 0,len=raw.length; i < len; i++) {
int t=Integer.parseInt(raw[i]);
if (i==0) {
cMS=t;
tMS=t;
}
cMS=Math.max(cMS+t, t);
tMS=Math.max(tMS, cMS);
}
System.out.println(tMS);
}
public static void main(String[] args) throws Exception{
new P1();
}
}
複製代碼
作者:
洪承廷
時間:
2023-2-10 20:05
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AAA {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int curMaxSum,totMaxSum;
AAA() throws Exception
{
raw=br.readLine().split(" ");
for(int i=0,len=raw.length;i<len;i++)
{
int t=Integer.parseInt(raw[i]);
if(i==0)
{
curMaxSum=t;
totMaxSum=t;
}
curMaxSum=Math.max(curMaxSum+t, t);
totMaxSum=Math.max(curMaxSum, totMaxSum);
}
System.out.println(totMaxSum);
}
public static void main(String[] args) throws Exception {
// TODO 自動產生的方法 Stub
new AAA();
}
}
複製代碼
作者:
林祐霆
時間:
2023-2-10 21:02
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Squeeezy {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String raw[];
int c, t;
Squeeezy() throws Exception {
raw = br.readLine().split(" ");
for (int i = 0, len = raw.length; i < len; i++) { // i=0;i<raw.length;i++
t = Integer.parseInt(raw[i]);
if (i == 0)
c = 0;
t = 0;
c = Math.max(c + t, t);
t = Math.max(c, t);
}
System.out.println(t);
}
public static void main(String[] args) throws Exception {
new Squeeezy();
}
}
複製代碼
作者:
蘇韋誠
時間:
2023-2-12 12:28
package hahaha;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ha {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String raw[];
int curMaxSum, totMaxSum;
ha() throws Exception
{
raw=br.readLine().split(" ");
for(int i=0, len=raw.length; i<len; i++)
{
int t=Integer.parseInt(raw[i]);
if(i==0)
{
curMaxSum=t;
totMaxSum=t;
}
curMaxSum=Math.max(curMaxSum+t, t);
totMaxSum=Math.max(curMaxSum, totMaxSum);
}
System.out.println(totMaxSum);
}
public static void main(String[] args) throws Exception{
new ha();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2