我試圖解決最大距離和這是一個代碼的eval挑戰這是這樣的:最大範圍和編碼評估和演示挑戰
鮑勃正在制定新的戰略,以獲得豐富的股市。他希望將自己的投資組合投資1天或更長時間,然後在合適的時間出售以最大限度地提高收益。鮑勃苦心追蹤了他的投資組合在過去N天裏每增加或減少多少。現在他已經僱傭你去弄清楚他的投資組合能夠取得的最大收益。
例如:
鮑勃跟蹤股票市場的最後10天。上的每一天中,增益/損失如下:
7 -3 -10 4 2 8 -2 4 -5 -2
如果Bob進入第4天股市和退出在第8天( 5天計),他的收益將一直
16(4 + 2 + 8 + 2 + 4)
我的輸入文件包括:
5;7 -3 -10 4 2 8 -2 4 -5 -2
6;-4 3 -10 5 3 -7 -3 7 -6 3
3;-7 0 -45 34 -24 7
我索姆ehow得到這三條線的輸出爲零。 我試過調試,但沒有得到該問題。 這裏是我的代碼:
package MaxRangeSum;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import static java.lang.Math.floor;
import java.util.StringTokenizer;
/**
*
* @author kanua_000
*/
public class MaxRangeSum {
public static int ret_cross = 0,ret_sum = 0;
public static Integer leftsum = Integer.MIN_VALUE;
public static Integer rightsum = Integer.MIN_VALUE;
public static int sum_cross = 0, maxleft = 0,sum_max = 0;
public static int leftsum_subarray = 0;
public static int rightsum_subarray = 0;
public static int crosssum = 0;
public static int max_crossing_subarray(int[] A, int low, int mid, int high) {
int i = mid;
while (i != low) {
sum_cross = sum_cross + A[i];
if (sum_cross > leftsum) {
leftsum = sum_cross;
// maxleft = i;
}
--i;
}
// int maxright = 0;
sum_cross = 0;
int j = mid + 1;
while (j != high) {
sum_cross = sum_cross + A[j];
if (sum_cross > rightsum) {
rightsum = sum_cross;
// maxright = j;
}
++j;
}
ret_cross = leftsum + rightsum;
return (ret_cross);
}
public static int max_subarray(int[] a, int low, int high) {
int i = 0,j = 0;
int[] A = new int[50];
if (low == high) {
return (A[low]);
} else {
int mid = (int) (floor((low + high)/2));
while (i != mid) {
ret_sum = ret_sum + A[i];
if (ret_sum > leftsum_subarray) {
leftsum_subarray = ret_sum;
}
i++;
}
leftsum_subarray = max_subarray(A, low, mid);
j = mid+1;
while (j != high) {
ret_sum = ret_sum + A[j];
if (ret_sum > rightsum_subarray) {
rightsum_subarray = ret_sum;
// maxleft = i;
}
j++;
}
rightsum_subarray = max_subarray(A, (mid + 1), high);
crosssum = max_crossing_subarray(A, low, mid, high);
if ((leftsum_subarray >= rightsum_subarray) & (leftsum_subarray >= crosssum)) {
return (leftsum_subarray);
} else if ((rightsum_subarray >= leftsum_subarray) & (rightsum_subarray >= crosssum)) {
return (rightsum_subarray);
} else {
return (crosssum);
}
}
}
public static void main(String args[]) throws FileNotFoundException, IOException {
int n = 0, i = 0;
int line1[] = new int[50];
int line2[] = new int[10];
int line3[] = new int[10];
int line4[] = new int[6];
try (BufferedReader in = new BufferedReader(new FileReader("E:\\FresnoState\\CSCI 174\\MaxRangeSum.txt"))) {
String line;
while ((line = in.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(line, " ");
while (tokenizer.hasMoreTokens()) {
String digits = tokenizer.nextToken();
if (digits.contains(";")) {
digits = (digits.substring(2));
}
line1[i] = Integer.parseInt(digits);
i++;
}
}
}
int numOfChunks = 10;
for (int k = 0; k < numOfChunks; k++) {
line2[k] = line1[k];
}
for (int r = 0; r < line2.length; ++r) {
System.out.print(line2[r] + " ");
}
System.out.println();
numOfChunks = 9;
int j = 10;
for (int l = 0; l <= numOfChunks; ++l, ++j) {
line3[l] = line1[j];
}
for (int r = 0; r < line3.length; ++r) {
System.out.print(line3[r] + " ");
}
System.out.println();
numOfChunks = 6;
j = 20;
for (int m = 0; m < numOfChunks; ++m, ++j) {
line4[m] = line1[j];
}
for (int r = 0; r < line4.length; ++r) {
System.out.print(line4[r] + " ");
}
System.out.println();
int line1_maxsum = max_subarray(line2, 1, 10);
int line2_maxsum = max_subarray(line3, 11, 20);
int line3_maxsum = max_subarray(line4, 21, 26);
System.out.println(line1_maxsum);
System.out.println(line2_maxsum);
System.out.println(line3_maxsum);
}
}
你是怎麼「*試着調試*」的?你是否一步一步地通過你的代碼? –
我建議閱讀[Kadane算法](https://en.wikipedia.org/wiki/Maximum_subarray_problem)。 – Emz
是的,我一步一步來,我的斷點是循環,返回語句和函數調用。 –