我似乎无法弄清楚为什么我在下面的突出显示行的编译时收到错误消息。错误消息是" ValueError:需要2个以上的值才能解压" ...提前感谢您的帮助
#include <iostream>
using namespace std;
class ManagedArray
{
private:
int numberOfElements;
float *elements;
public:
ManagedArray(): numberOfElements(0), elements(NULL){}
int size();
float get(int index);
void add(float value);
ManagedArray(ManagedArray & ma);
~ManagedArray() {delete [] elements;}
};
ManagedArray::ManagedArray(ManagedArray & ma):numberOfElements(ma.numberOfElements)
{
elements = ma.elements;
}
int ManagedArray::size()
{
return numberOfElements;
}
float ManagedArray::get(int index)
{
return elements[index];
}
void ManagedArray::add(float value)
{
float * BiggerArray = new float[numberOfElements+1];
if(elements != NULL)
{
// copy the old elements into the biggerArray
for(int i = 0; i < numberOfElements; i++)
{
BiggerArray[i] = elements[i];
}
// the old array is not needed anymore, we have a better copy
this->~ManagedArray();
}
elements = BiggerArray;
numberOfElements = numberOfElements+1;
// put the new number into the last element of the array
elements[numberOfElements-1] = value;
}
void print(ManagedArray ma)
{
// print the stored numbers
cout << "Entered numbers: " << endl;
cout << "{";
for(int i = 0; i < ma.size(); ++i)
{
if(i > 0)
{
cout << ", ";
}
cout << ma.get(i);
}
cout << "}" << endl;
float sum = 0;
for(int i = 0; i < ma.size(); ++i)
{
sum += ma.get(i);
}
cout << "total: " << sum << endl;
cout << "average: " << (sum / ma.size()) << endl;
}
int main()
{
float userInput;
ManagedArray elementArray;
bool addingNumbersToTheList;
cout << "Keep entering numbers. Enter a non-number to stop." << endl;
do
{
cin >> userInput;
addingNumbersToTheList = !std::cin.fail();
if(addingNumbersToTheList) {
elementArray.add(userInput);
}
}
while(addingNumbersToTheList);
// fix cin after intentionally breaking it above.
if(std::cin.fail())
{
std::cin.clear();
while(std::cin.get() != '\n');
}
bool hasNumbers = elementArray.size() > 0;
if(hasNumbers) {
ManagedArray copyArray(elementArray);
cout << "printing elementArray:\n";
print(elementArray);
cout << "\nprinting copyArray:\n";
print(copyArray);
}
else
{
cout << "no numbers entered." << endl;
}
cin.get();
cin.get();
return 0;
}
这是我的主要代码
def divConHelper(array, first, center, last):
sumRight = float('-inf') #assign impossibly low number
sumLeft = float('-inf') #assign impossibly low number
leftIndexMax = 0
rightIndexMax = 0
# Determine crossover max toward left
currentLTotal = 0
for i in range(first, center):
#Incrementally add values to sum
currentLTotal += sum(array[i:center-1])
#if the total is greater than the sumLeft, replace
if currentLTotal > sumLeft:
sumLeft = currentLTotal
leftIndexMax = i
# Determine crossover max toward right
currentRTotal=0
for i in range(center+1, last):
#Incrementally add values to sum
currentRTotal += sum(array[center:i+1])
#if the rightTotal is greater than the sumRight, replace
if currentRTotal > sumRight:
sumRight = currentRTotal
rightIndexMax = i+1
bothTotal = sumRight + sumLeft
return(leftIndexMax, rightIndexMax, bothTotal)
#return(bothTotal)
def divConMaxSub(self, array, first, last):
if (last-first) == 0:
return array[first: last +1], array[0]
center = (first+last)// 2
#Recursive calls to divConMaxSub
(minA, maxA, sumA) = self.divConMaxSub(array, first, center)
(minB, maxB, sumB) = self.divConMaxSub(array, center+1, last)
#call to get max cross values
(maxL, maxR, maxCross) = divConHelper(array, first, center, last)
#Calulate the max subarray value
finalMax = max(sumA, sumB, maxCross)
#Logic block for return values
if (finalMax == sumA):
return array[minA: maxA], sumA
#return minA, maxA, sumA
elif (finalMax == sumB):
return array[minB: maxB], sumB
#return minB, maxB, sumB
elif (finalMax == maxCross):
return array[maxL: maxR], maxCross
#return maxL, maxR, maxCross
答案 0 :(得分:1)
本声明:
return array[minA: maxA], sumA
不会返回三个参数(正如您在此代码中所期望的那样):
(minA, maxA, sumA) = self.divConMaxSub(array, first, center)
它返回一个数组的块作为第一个参数,sum作为第二个参数。因此,Python抱怨说它不能使用divConMaxSub的输出来初始化左边的三个值,因为右边只有两个值可用。