我有n个向量,其中k个元素的类型为int。将这n个向量中包含的元素的所有组合相加时,如何找到出现次数最多的总和?
当前正在处理具有3个向量的样本:
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <limits>
int main()
{
std::vector<int> v;
std::vector<int> x;
std::vector<int> y;
std::vector<int> mostCommon;
int inputV;
int inputX;
int inputY;
std::cout << "Enter values for vector v: ";
while (std::cin >> inputV)
{
v.push_back(inputV);
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter values for vector x: ";
while (std::cin >> inputX)
{
x.push_back(inputX);
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter values for vector y: ";
while (std::cin >> inputY)
{
y.push_back(inputY);
}
std::vector<int> possibleSums;
for( int vE : v )
for( int xE : x )
for( int yE : y )
possibleSums.push_back( vE + xE + yE );
std::sort( possibleSums.begin(), possibleSums.end() );
std::set<int> possibleSumSet( possibleSums.begin(), possibleSums.end() );
int mostCommonCount = 0;
for( int sum : possibleSumSet )
{
int count = std::count( possibleSums.begin(), possibleSums.end(), sum );
if( count >= mostCommonCount )
{
mostCommonCount = count;
mostCommon.push_back (sum);
}
}
std::cout << "Most Common Sum Range: " << int(mostCommon.size()) << std::endl;
std::cout << "Most Common Sums: ";
for (auto i = mostCommon.begin(); i != mostCommon.end(); ++i)
{
std::cout << *i << ' ';
}
std::cout << std::endl << "Most Common Count: " << mostCommonCount;
}
向量的样本输入:
Enter values for vector v: 1 2 4 5
Enter values for vector x: 1 2 3
Enter values for vector y: 2 3
上述示例输入的可能总和:
1+1+2=4
1+1+3=5
1+2+2=5
2+1+2=5
1+2+3=6 <-
1+3+2=6 <-
2+1+3=6 <-
2+2+2=6 <-
1+3+3=7 <-
2+2+3=7 <-
2+3+2=7 <-
4+1+2=7 <-
2+3+3=8 <-
4+1+3=8 <-
4+2+2=8 <-
5+1+2=8 <-
4+2+3=9 <-
4+3+2=9 <-
5+1+3=9 <-
5+2+2=9 <-
4+3+3=10
5+2+3=10
5+3+2=10
5+3+3=11
所以这里的结果将是:
我现在得到的是:
Most Common Sum Range: 6
Most Common Sums: 4 5 6 7 8 9
Most Common Count: 4
答案 0 :(得分:2)
最简单的方法是循环最大的向量,为每个向量的每个元素循环每个较小的向量,将所有结果推入新向量,然后找到最常见的向量。
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 1, 2, 4, 5 };
std::vector<int> x = { 1, 2, 3 };
std::vector<int> y = { 2, 3 };
std::vector<int> possibleSums;
for( int vE : v )
for( int xE : x )
for( int yE : y )
possibleSums.push_back( vE + xE + yE );
std::sort( possibleSums.begin(), possibleSums.end() );
std::set<int> possibleSumSet( possibleSums.begin(), possibleSums.end() );
int mostCommon = 0, mostCommonCount = 0;
for( int sum : possibleSumSet )
{
int count = std::count( possibleSums.begin(), possibleSums.end(), sum );
if( count > mostCommonCount )
{
mostCommonCount = count;
mostCommon = sum;
}
}
std::cout << "Most Common Sum: " << mostCommon
<< "; Most Common Count: " << mostCommonCount;
}
编辑:您还可以将每个出现次数最多的数字存储到std::vector
中,这样就不会忽略随后出现的最大次数:
int mostCommonCount = 0;
std::vector<int> mostCommonSums;
for( int sum : possibleSumSet )
{
int count = std::count( possibleSums.begin(), possibleSums.end(), sum );
if( count > mostCommonCount )
{
mostCommonCount = count;
mostCommonSums.clear();
mostCommonSums.push_back( sum );
}
else if( count == mostCommonCount )
{
mostCommonSums.push_back( sum );
}
}
for( int sum : mostCommonSums )
std::cout << "Most Common Sum: " << sum << std::endl;
std::cout << "Most Common Count: " << mostCommonCount;
答案 1 :(得分:1)
第1步:列出所有总和。
第2步:计算每笔款项出现的频率。
第3步:返回显示最多的一个。
每个步骤都可以是一个功能。我将把它们的实现留给您。