我想计算给定数组中最长的子序列的总和和长度t
。长度和最长增加的子序列的总和
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class so {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
br.close();
int[] t = new int[s.length];
for (int i = 0; i < s.length; i++) {
t[i] = Integer.parseInt(s[i]);
}
int length = 1;
int sum = t[0];
int maxSum = 0;
int maxLength = 0;
for (int i = 1; i < t.length; i++) {
for (; i < t.length && t[i - 1] <= t[i]; i++) {
length++;
sum += t[i];
System.out.print(t[i] + " ");
}
if (length > maxLength) {
maxLength = length;
maxSum = sum;
length = 1;
sum = 0;
i--;
}
}
System.out.println("sum is " + maxSum + " length is " + maxLength);
}
}
的数字1 1 7 3 2 0 0 4 5 5 6 2 1
我得到的输出:
sum is 20 length is 6
但以相反的顺序1 2 6 5 5 4 0 0 2 3 7 1 1
相同的数字,我得到的输出:
sum is 17 length is 6
这是不是真的因为我应该得到sum is 12 length is 5
。
有人能发现我的错误吗?