gpt4 book ai didi

c++ - c++ 中的累积函数故障无法找到均值

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:57 25 4
gpt4 key购买 nike

我需要在一个 vector 中找到一个数字,如果我们取出它不会影响均值,我正在替换每个数字并找出均值并进行比较但是当我尝试在删除数字后计算均值时它总是不正确为什么?

mean 是 vector 元素的原始均值...mean1 是删除元素后的新均值,但每次计算都是错误的

#include <iostream>
#include <vector>
#include <numeric>
#include <iterator>
using namespace std;

int main()
{
int t;
cin >> t;
while (t--)
{
int n, i, sum = 0, sum1 = 0;
cin >> n;
vector<int> ser;
for (i = 0; i < n; i++)
{
int temp;
cin >> temp;
ser.push_back(temp);
}
int mean = accumulate(ser.begin(), ser.end(), sum) / n;

vector<int> ser1;
ser1 = ser;
bool flag = false;
vector<int>::iterator it;
it = ser1.begin();

for (i = 0; i < n; i++)
{
ser1.erase(it);
int mean1 = accumulate(ser1.begin(), ser1.end(), 0) / (ser1.size());
if (mean == mean1)
{
cout << i;
flag = true;
break;
}
else
{
ser1 = ser;
continue;
}
}
if (flag == false) {
cout << "Impossible";
}
}
return 0;
}

最佳答案

您的代码中有几个问题。

 it = ser1.begin();

for (i = 0; i < n; i++)
{
ser1.erase(it);

您正在 for 循环之前初始化迭代器。然后调用 erase。正如您在 cppreference 中所读到的那样, 删除

"Invalidates iterators and references at or after the point of the erase, including the end() iterator."

在你的情况下甚至可以,因为你永远不会增加迭代器。这意味着,您总是在删除第一个元素。所以2个错误。这是行不通的。

您还为平均值使用了错误的数据类型。

另请阅读您帖子下的评论。基本上已经提到了所有内容。

我将另外向您展示 2 个建议,代码的外观。

第一个版本检查,如果可以减少。

第二个版本在循环中重复执行此归约。

版本 1:


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>

using NumberType = int;

constexpr size_t MinNumberOfValuesToCheck = 1;
constexpr size_t MaxNumberOfValuesToCheck = 20;

int main()
{
// Read the number of values to check
std::cout << "How many vaues shall be checked? Please enter a number: ";
size_t numberOfValuesToCheck{0};
std::cin >> numberOfValuesToCheck;
// Limit the input to meaningful values
numberOfValuesToCheck = std::clamp(numberOfValuesToCheck, MinNumberOfValuesToCheck, MaxNumberOfValuesToCheck);

// Here we will store all values
std::vector<NumberType> values(numberOfValuesToCheck);
// Read all user input and stor it in our vector
std::copy_n(std::istream_iterator<NumberType>(std::cin), numberOfValuesToCheck, values.begin());

// Calculate mean. The result is most likely a double
double meanValue {static_cast<double>(std::accumulate(values.begin(), values.end(), 0)) / static_cast<double>(values.size())};
std::cout << "\nMean value: " << meanValue << '\n';

// Look, if there is a mean value
std::vector<NumberType>::iterator found = std::find_if(values.begin(),values.end(),[&meanValue](NumberType& n){ return n == meanValue;});

if (found != values.end() ) {
std::cout << "Could erase " << *found << "New Vector:\n";
std::copy(values.begin(), values.end(), std::ostream_iterator<NumberType>(std::cout, " "));
}
else {
std::cout << "No reduction possible\n";
}
return 0;
}

版本 2。更“完整的解决方案”。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>

using NumberType = int;

constexpr size_t MinNumberOfValuesToCheck = 1;
constexpr size_t MaxNumberOfValuesToCheck = 20;

int main()
{
// Read the number of values to check
std::cout << "How many vaues shall be checked? Please enter a number: ";
size_t numberOfValuesToCheck{0};
std::cin >> numberOfValuesToCheck;
// Limit the input to meaningful values
numberOfValuesToCheck = std::clamp(numberOfValuesToCheck, MinNumberOfValuesToCheck, MaxNumberOfValuesToCheck);

// Here we will store all values
std::vector<NumberType> values(numberOfValuesToCheck);
// Read all user input and stor it in our vector
std::copy_n(std::istream_iterator<NumberType>(std::cin), numberOfValuesToCheck, values.begin());

while(numberOfValuesToCheck) {
// Calculate mean. The result is most likely a double
double meanValue {static_cast<double>(std::accumulate(values.begin(), values.end(), 0)) / static_cast<double>(values.size())};
std::cout << "\nMean value: " << meanValue << '\n';

values.erase(std::remove_if(values.begin(),values.end(),[&meanValue](NumberType& v){ return v == meanValue;}),values.end());

// Check if we coud remove a value
if (values.size() < numberOfValuesToCheck) {
// If so then the vector has less values
// Show some output:
std::cout << "Could eliminate " << numberOfValuesToCheck - values.size() << " from Vector of Values. New vector:\n";
std::copy(values.begin(), values.end(), std::ostream_iterator<NumberType>(std::cout, " "));

// New size of vector
numberOfValuesToCheck = values.size();
}
else {
// Could not reduce more
std::cout << "No further reduction possible\n";
break;
}
}
return 0;
}

希望这对您有所帮助。 . .

关于c++ - c++ 中的累积函数故障无法找到均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com