我正在尝试解决Max Range Sum,这是一个代码评估挑战,如下所示:
鲍勃正在制定一项新的战略,以致富股市。他希望投资他的投资组合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
我以某种方式将这三条线的输出变为零。 我试过调试但没有得到问题。 这是我的代码:
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);
}
}
答案 0 :(得分:0)
我不确定max_subarray的方法是什么,因为它如此混乱,但是如果你想在行中加上数字,试着在max_subarray方法中使用它,你可以解决它,我猜。
public static int max_subarray(int[] a, int low, int high) {
int theMiddle = (int) Math.floor(a.length / 2);
int leftSum = addArray(a, 0, theMiddle);
int rightSum = addArray(a, theMiddle, a.length);
if(leftSum > rightSum){
return leftSum;
}else{
return rightSum;
}
}
public static int addArray(int[] a, int start, int end) {
int sum = 0;
for (int i = start; i < end; i++) {
sum = sum + a[i];
}
return sum;
}