- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我编写了这个程序,它使用递归函数计算 vector 中元素的总和和平均值。用户应该输入任意数量的数字,然后在完成后按“0”。我忽略了最后一个元素,即“0”。但是,sum 函数仍然有问题,因为它忽略了 vector 中的第一个元素,因此平均值也不正确。这是代码:
int main(void){
vector <int> series;
int n;
unsigned int nr_elem;
cout << "Type as many numbers as you want and press 0 when you are finished:";
do{
cin >> n;
series.push_back(n);
} while (n);
nr_elem = series.size()-1;
cout << "nr_elem="<< nr_elem;
cout << "\n The sum of the elements in the series is: " << sum(series, nr_elem);
cout << "\n The average of the elements is: " << average(series, nr_elem);
return 0;
}
int sum(vector<int> &series, int n){
if (n == 0)
return 0;
else
return series.at(n) + sum(series, n - 1);
}
double average(vector<int> &series, int n){
if (n == 0)
return 0;
else
return (series.at(n) + sum(series, n - 1)) / n;
}
对于输入值:10,20,50,0
我得到 nr_elem=3
The sum of the elements is: 70
The mean is: 23`
这是我第二次使用 vectors 进行试验,因此,我们将不胜感激任何解释和额外的更正。
最佳答案
vector 的大小由 size()
函数给出,它准确返回 vector 中元素的数量,但索引从零开始。
您没有计算求和函数中的 0(不是您插入的终端项, vector 的第一项!)元素
int sum(vector<int> &series, int n){
if (n == 0) // This is the problem, 0 isn't the last element, is the first element!
return 0;
else
return series.at(n) + sum(series, n - 1);
}
要解决(最小侵入性)改变
if (n == 0)
类似于
if (n == -1)
编辑:如果你想返回一个 double 值,你也需要处理 double 值。如果您使用整数(没有小数位)进行数学运算,您将得到一个整数结果(没有小数位)。所以你丢失了你的小数位。
另一个“丑陋”的解决方案是强制将整数转换为 double ,并在所有东西都为 double 时进行计算
double average(vector<int> &series, int n){
if (n == -1)
return 0;
else
return (static_cast<double>(series.at(n)) + static_cast<double>(sum(series, n - 1))) / static_cast<double>(n);
}
关于c++ - vector 递归均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22438116/
我想获取每一行某些列的平均值。 我有此数据: w=c(5,6,7,8) x=c(1,2,3,4) y=c(1,2,3) length(y)=4 z=data.frame(w,x,y) 哪个返回:
类似于Numpy mean with condition我的问题将其扩展到对矩阵进行操作:计算矩阵 rdat 的行均值,跳过某些单元格 - 在本例中我使用 0 作为要跳过的单元格 - 就好像这些值从一
我有一个数据集,其中的列标题为产品名称、品牌、评级(1:5)、评论文本、评论有用性。我需要的是提出一个使用评论的推荐算法。我这里必须使用 python 进行编码。数据集采用.csv 格式。 为了识别数
我在 R^3 中有 n 个点,我想用 k 个椭球体或圆柱体覆盖它们(我不在乎;以更容易的为准)。我想大约最小化卷的并集。假设 n 是数万,k 是少数。开发时间(即简单性)比运行时更重要。 显然我可以运
我创建了一个计算均值、中位数和方差的程序。该程序最多接受 500 个输入。当有 500 个输入(我的数组的最大大小)时,我的所有方法都能完美运行。当输入较少时,只有“平均值”计算器起作用。这是整个程序
我已经完成了距离的计算并存储在推力 vector 中,例如,我有 2 个质心和 5 个数据点,我计算距离的方法是,对于每个质心,我首先计算 5 个数据点的距离并存储在阵列,然后与距离一维阵列中的另一个
下面的代码适用于每一列的总数,但我想计算出每个物种的平均值。 # Read data file into array data = numpy.genfromtxt('data/iris.csv',
我有一个独特的要求,我需要两个数据帧的公共(public)列(每行)的平均值。 我想不出这样做的 pythonic 方式。我知道我可以遍历两个数据框并找到公共(public)列,然后获取键匹配的行的平
我把它扔在那里,希望有人会尝试过这种荒谬的事情。我的目标是获取输入图像,并根据每个像素周围小窗口的标准差对其进行分割。基本上,这在数学上应该类似于高斯或盒式过滤器,因为它将应用于编译时(甚至运行时)用
有没有一种方法可以对函数进行向量化处理,使输出成为均值数组,其中每个均值代表输入数组的 0 索引值的均值?循环这个非常简单,但我正在努力尽可能高效。例如0 = 均值(0),1 = 均值(0-1),N
我正在尝试生成均值为 1 的指数分布随机数。我知道如何获取具有均值和标准差的正态分布随机数。我们可以通过normal(mean, standard_deviation)得到它,但是我不知道如何得到指数
我遇到了一段 Python 代码,它的内容类似于以下内容: a = np.array([1,2,3,4,5,6,7]) a array([1, 2, 3, 4, 5, 6, 7]) np.mean(a
我有两个数组。 x 是独立变量,counts 是 x 出现的次数,就像直方图一样。我知道我可以通过定义一个函数来计算平均值: def mean(x,counts): return np.sum
我有在纯 python 中计算平均速度的算法: speed = [...] avg_speed = 0.0 speed_count = 0 for i in speed: if i > 0:
我正在尝试计算扩展窗口的平均值,但是数据结构使得之前的答案至少缺少一点所需的内容(最接近的是:link)。 我的数据看起来像这样: Company TimePeriod IndividualID
我正在尝试实现 Kmeans python中的算法将使用cosine distance而不是欧几里得距离作为距离度量。 我知道使用不同的距离函数可能是致命的,应该小心使用。使用余弦距离作为度量迫使我改
有谁知道自组织映射 (SOM) 与 k 均值相比效果如何?我相信通常在颜色空间(例如 RGB)中,SOM 是将颜色聚类在一起的更好方法,因为视觉上不同的颜色之间的颜色空间存在重叠( http://ww
注意:我希望能得到更多有关如何处理和提出此类解决方案的指南,而不是解决方案本身。 我的系统中有一个非常关键的功能,它在特定上下文中显示为排名第一的分析热点。它处于 k-means 迭代的中间(已经是多
我有一个 pandas 数据框,看起来像这样: 给定行中的每个值要么是相同的数字,要么是 NaN。我想计算数据框中所有两列组合的平均值、中位数和获取计数,其中两列都不是 NaN。 例如,上述数据帧的结
任何人都知道如何调整简单的 K 均值算法来处理 this form 的数据集. 最佳答案 在仍然使用 k-means 的同时处理该形式的数据的最直接方法是使用 k-means 的内核化版本。 JSAT
我是一名优秀的程序员,十分优秀!