我有一个函数可以计算每个柜台上结账的金额总和,经过计算,我想找到最好的两个结账柜台,我可以找到最大结帐的指数,但我有一个小混乱关于寻找第二大结账柜台。 这是代码。如何查找数组集合中第二大元素的索引?
for (int j = 0; j < Bq[i]; j++)
{
sumM += p.GetValue(i, j).GetSum();
sumT += p.GetValue(i, j).GetTime();
}
indexofmax = i;
if(sumM > maxmoney)
{
Secondbestcheckout=firstbestcheckout;
firstbestcheckout = indexofmax+1;
maxmoney = sumM;
secondindex = Secondbestcheckout;
indexofmax =firstbestcheckout;
}
fr << i+1 << fixed << setw(9) << setprecision(2) << sumM
<< " " << setw(6) << sumT << endl;
}
fr << "The first best checkout interms of money collected is "
<< firstbestcheckout << "And the second best checkout is "
<< secondindex << endl;
此代码成功打印最大结帐计数器的索引,但未成功打印第二大索引。
这是我在这里的第一篇文章,我不知道玩的工具。 – 2014-12-07 12:39:36
我看你是新的。欢迎!我不是在抨击你,我只是觉得你的坏缩进仍然是可读的:)。你会习惯收费的,不用担心。 – bolov 2014-12-07 12:41:13
这看起来像一个复杂解决方案的简单问题。你错过了很多相关的代码。例如,我看不到,如果在没有迭代的情况下“i”是指数。尽量给所有相关的代码,但只有相关的代码。 – SHR 2014-12-07 12:45:16