C++ 从数组中删除一个最大值和零

c++ delete one biggest value and zeroes from array

本文关键字:一个 最大值 数组 删除 C++      更新时间:2023-10-16

我有一个包含 10 个值的双精度数组,首先,如果该值在数组中只有一次,我的函数应该从数组中找到并删除最大值,其次从数组中删除零值。在这一点上,我们不能使用任何向量来解决这个问题。

void calculations(double first[], int& n)
{
int k = 0;
double max1= first[0];
for (int i = 0; i < n; i++)
{
if (first[i] > max1)
{
max1 = first[i];
}
if (first[i] == max1)
{
k++;
}
if (k < 2)
{
for (int i = max1; i < n - 1; i++)
{
first[i] = first[i + 1];
n--;
}
}
}
for (int i = 0; i < n; i++)
{
if (first[i] == 0)
{
first[i] = first[i + 1];
n--;
}
}
for (int i = 0; i < n; i++)
{
cout << first[i] << endl;
}
}

为了从数组中删除元素,您需要将所有剩余的元素"向上"移动一个,这样array[i] = array[i+1];

另一种替代方法是指示阵列插槽可供重用。

请参阅std::vector::erasestd::array

这读起来很像编程课程作业?无论如何,我会做一个两遍的方法:

第一遍有两个任务:

  1. 找到最大的元素并记住它;还要确保它只存在一次。
  2. 计算零的数量。

在代码中:

if (n == 0)
return; //just to be safe
size_t max_value = first[0];
bool max_value_is_unique = true;
size_t zeroes = first[0] == 0 ? 1 : 0;
//start at 1, since we already looked at first[0] above!
for (size_t i = 1; i < n; ++i) {
//oh, it's a 0; count it!
if (first[i] == 0)
zeroes++;
//check if this is equal to or bigger than current maximum
if (first[i] == max_value)
max_value_is_unique = false;
else if (first[i] > max_value) {
max_value = first[i];
max_value_is_unique = true;
}
}

优化: 如果没有唯一的最大元素并且零数为 0,则返回:

if (zeroes == 0 && !max_value_is_unique)
return;

否则,运行第二遍:

这个只会将条目从右侧移动到您当前的位置。 一旦我们达到 0 或最大值,移动距离就会增加,因为我们想跳过该值。

//move all entries some distance "to the left"
int distance = 0;
for (size_t i = 0; i < n - distance; /* no increment! */) {
bool might_inc = true;
//we skip the current value in case it's a zero or the maximum value
if ((max_value_is_unique && first[i] == max_value) || first[i] == 0)
distance++;
if (distance > 0) {
if (i + distance < n) {
//we might reach out of the array here, so this line guarded
first[i] = first[i + distance];
//since we just copied some value to the current `i`, we need to take a look at that `i` it again - it might be 0 or the maximum value
might_inc = false;
} else
//if we would reach out of the array, write 0
first[i] = 0;
}
if (might_inc)
i++;
}
//you didn't specify it, so I'll just set the remaining entries to 0
for (int i = n - distance; i < n; ++i) {
first[i] = 0;
}

我没有像所有零一样检查极端情况,但我认为它应该有效。 如果您知道有 X 尾随零,可能会更快,而不是复制然后检查复制的值,您可以在复制之前检查该值。但是,正如我所说,它看起来像一个练习,长度是 10,所以很好,应该没问题;-(

如果你不知道size_t,这是一个奇特的数据类型,它总是足够大,可以解决你的整个内存。在此代码中,您很可能只编写unsigned intint。但我相信size_t更好的风格,在适当的时候严格使用它是一个很好的做法。

你的代码是完全错误的

你的第一个for循环应该只用于找到最大值和计数,每次找到更大的值,你需要重置你的计数器,k

然后使用另一个 for 循环来删除值,I+1 可以超出边界,n-- 会阻止你遍历整个数组

用于删除值的 for 循环也是错误的,您应该有两个数字来表示保留和探测的索引。

例如,要从 [1,0,0,4] 中删除 0,您的代码将以 [1,0,4,4] 结尾并将大小更改为 2

您的删除是在搜索中 - 这是错误的

您实际上不需要计算最大值

您无法通过简单的==来比较浮点值,例如floatdoublelong double。您需要测量它们之间的差异,如果它小于一些小的容差值,则认为它们相等。

所以...试试这个:

#include <iostream>
#include <cstddef>
#include <cmath>
#include <cstdlib>
void remove_all(double* array, std::size_t& size, double val) {
double eps = 1e-9; 
std::size_t k = 0, tail = 0;
while (k + tail < size) {
if (std::fabs(array[k + tail] - val) < eps) // correct floating point numbers equality compare
++tail;
else {
array[k] = array[k + tail];
++k;
}
}
size -= tail;
}
void remove_max_and_zeros(double* array, std::size_t& size) {
//-- find max element in array --
double maxVal = array[0];
for (std::size_t k = 0; k < size; ++k) 
if (maxVal < array[k])
maxVal = array[k];
//-- remove max element from array --
remove_all(array, size, maxVal);
//-- remove zeros from array --
remove_all(array, size, 0.0);
}
int main()
{
const std::size_t maxSize = 10;
double arr[maxSize] = { 1.1, 1.3, 0.0, 2.5, 0.0, -5.8, 2.5, 0.0, 0.0001, 0.0 };
std::size_t size = maxSize;
for (std::size_t k = 0; k < size; ++k)
std::cout << arr[k] << " ";
std::cout << std::endl;
remove_max_and_zeros(arr, size);
for (std::size_t k = 0; k < size; ++k)
std::cout << arr[k] << " ";
std::cout << std::endl;
return 0;
}