有关用C++求一个数列的最大子序列和,运行始终存在问题,实在找不到问题所在,求助各位
#include<iostream>#include<vector>usingnamespacestd;intmax(inta,intb,intc){if(a>=b){i...
#include <iostream>
#include <vector>
usingnamespacestd;
int max(int a,int b,int c){
if (a>=b) {
if (a>=c)
return a;
else
if (b>=c)
return b;
else
return c;
}
else
if (b>=c)
return b;
else
return c;
}
int maxSumRec(constvector<int> & a,longint left,longint right){
if (left==right)
return a[left];
int center=(left+right)/2;
int maxLeftSum=maxSumRec(a, left, center);
int maxRightSum=maxSumRec(a, center+1, right);
int maxLeftBorderSum=a[center],leftBorderSum=0;
for (int i=center; i>=left; i--) {
leftBorderSum+=a[i];
if (leftBorderSum>maxLeftSum) {
maxLeftBorderSum=leftBorderSum;
}
}
int maxRightBorderSum=a[center+1],rightBorderSum=0;
for (int j=center+1; j<=right; j++) {
rightBorderSum+=a[j];
if (rightBorderSum>maxRightBorderSum) {
maxRightBorderSum=rightBorderSum;
}
}
int maxMiddleSum=maxLeftBorderSum+maxRightBorderSum;
returnmax(maxLeftSum, maxRightSum, maxMiddleSum);
}
int main(){
vector<int> a;
char next_char;
int next_num;
cout<<"Please enter a list:\n";
cin.get(next_char);
while (next_char!='\n') {
if (!(isdigit(next_char)||next_char=='-')) {
next_char=cin.get();
continue;
}
cin.putback(next_char);
cin>>next_num;
a.push_back(next_num);
next_char=cin.get();
}
cout<<"You entered:\n";
for (int k=0; k<a.size(); k++) {
cout<<a[k]<<" ";
}
cout<<endl;
longint size=a.size();
cout<<"The max sonsum is:\n";
cout<<maxSumRec(a, 0, size-1);
return0;
} 展开
#include <vector>
usingnamespacestd;
int max(int a,int b,int c){
if (a>=b) {
if (a>=c)
return a;
else
if (b>=c)
return b;
else
return c;
}
else
if (b>=c)
return b;
else
return c;
}
int maxSumRec(constvector<int> & a,longint left,longint right){
if (left==right)
return a[left];
int center=(left+right)/2;
int maxLeftSum=maxSumRec(a, left, center);
int maxRightSum=maxSumRec(a, center+1, right);
int maxLeftBorderSum=a[center],leftBorderSum=0;
for (int i=center; i>=left; i--) {
leftBorderSum+=a[i];
if (leftBorderSum>maxLeftSum) {
maxLeftBorderSum=leftBorderSum;
}
}
int maxRightBorderSum=a[center+1],rightBorderSum=0;
for (int j=center+1; j<=right; j++) {
rightBorderSum+=a[j];
if (rightBorderSum>maxRightBorderSum) {
maxRightBorderSum=rightBorderSum;
}
}
int maxMiddleSum=maxLeftBorderSum+maxRightBorderSum;
returnmax(maxLeftSum, maxRightSum, maxMiddleSum);
}
int main(){
vector<int> a;
char next_char;
int next_num;
cout<<"Please enter a list:\n";
cin.get(next_char);
while (next_char!='\n') {
if (!(isdigit(next_char)||next_char=='-')) {
next_char=cin.get();
continue;
}
cin.putback(next_char);
cin>>next_num;
a.push_back(next_num);
next_char=cin.get();
}
cout<<"You entered:\n";
for (int k=0; k<a.size(); k++) {
cout<<a[k]<<" ";
}
cout<<endl;
longint size=a.size();
cout<<"The max sonsum is:\n";
cout<<maxSumRec(a, 0, size-1);
return0;
} 展开
2个回答
展开全部
int maxSub(const vector<int> &a){
int max = 0;
int sum = 0;
for(int i=0; i<a.size(); i++){
sum += a[i];
if(sum > max){
max = sum;
}
else if (sum < 0){
sum = 0;
}
}
return max;
}
把vector直接丢到这个function里面就可以了
int max = 0;
int sum = 0;
for(int i=0; i<a.size(); i++){
sum += a[i];
if(sum > max){
max = sum;
}
else if (sum < 0){
sum = 0;
}
}
return max;
}
把vector直接丢到这个function里面就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询