在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的

Getting the maximum Pair Wise product C++ and the output is not always right with my code

本文关键字:输出 C++ 产品 Pair 代码 我的 Wise      更新时间:2023-10-16

要解决的问题:给定一个数组或数字序列,目标是找到通过该序列中的一些2个数字相乘而获得的最大数。

输入和输出示例

Input:
2
100000 90000
Correct output:
9000000000
Input:
3
1 2 3
Correct output:
6

我的解决方案:在给定的序列中获得2个最大值,并将它们相乘我的代码工作,除非有一个解决方案

我的代码

#include <vector> 
#include <iostream> 
#include <algorithm> 
using namespace std;
void print(std::vector<int> const& input)
{
for (int i = 0; i < input.size(); i++) {
std::cout << input.at(i) << '   ';
}
}
int main()
{

vector<int> seq;
int n;
// Read the nb of elements in vect
cout << "please enter the number of elements in sequence"<<endl;
cin >> n;
// Read the vector 
cout << "please enter the elements of the sequence"<<endl;
for (int i = 0; i < n; i++)
{
int input;
cin >> input;
seq.push_back(input);
}
cout << "sequence you entered" << endl;
print(seq);

// Find the 1st max element 
double FisrtMax=*max_element(seq.begin(), seq.end());
cout <<endl<< "First Maximum Element is" << endl<< FisrtMax;
// remove the found element
std::vector<int>::iterator PosF = find(seq.begin(), seq.end(), FisrtMax);
seq.erase(PosF);
cout <<endl<< "sequence After removing the 1st maximum element" << endl;
print(seq);

// Find the 2nd max element
double SecMax = *max_element(seq.begin(), seq.end());
cout <<endl<< "Second Maximum Element is" << endl << SecMax;
//multiply the 2 elements
int total =  (FisrtMax * SecMax);
cout <<endl<<"The Product of the 2 elemnts is  "<< total;
return 0;
}

输入:

please enter the number of elements in sequence
2
please enter the elements of the sequence
1000000
90000

输出:

please enter the number of elements in sequence
2
please enter the elements of the sequence
1000000
90000
sequence you entered
10000002105376900002105376
First Maximum Element is
1e+06
sequence After removing the 1st maximum element
900002105376
Second Maximum Element is
90000
The Product of the 2 elements is  -2147483648

代码中有几个错误:

  1. cout << ... << ' '语法尝试使用接受单个字母而非多个字母的单引号打印三个空白字符字母。请改用" "

  2. 生成的结果不能用整数表示,需要定义size_t(在大多数编译器中扩展为unsigned long long(。

侧面提示:

  • 在此语法中:

    for (int i = 0; i < input.size(); i++)
    

    您正在尝试将一个整数与size_t(由向量类对象的size()成员函数返回(进行比较。相反,用size_t声明i

  • 在此语法中:

    std::vector<int>::iterator PosF = find(seq.begin(), seq.end(), FisrtMax);
    

    你不需要定义这么长的类型std::vector<int>::iterator,在这里使用auto关键字:

    auto PosF = find(seq.begin(), seq.end(), FirstMax);
    

代码重新定义:

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(std::vector<size_t> const &input) {
for (size_t i = 0; i < input.size(); i++)
std::cout << input.at(i) << "   ";
}
int main(void) {
vector<size_t> seq;
int n;
// Read the nb of elements in vect
cout << "please enter the number of elements in sequence" << endl;
cin >> n;
// Read the vector
cout << "please enter the elements of the sequence" << endl;
for (int i = 0; i < n; i++) {
int input;
cin >> input;
seq.push_back(input);
}
cout << "sequence you entered" << endl;
print(seq);
// Find the 1st max element
double FisrtMax = *max_element(seq.begin(), seq.end());
cout << endl
<< "First Maximum Element is" << endl
<< FisrtMax;
// remove the found element
auto PosF = find(seq.begin(), seq.end(), FisrtMax);
seq.erase(PosF);
cout << endl << "sequence After removing the 1st maximum element" << endl;
print(seq);
// Find the 2nd max element
double SecMax = *max_element(seq.begin(), seq.end());
cout << endl
<< "Second Maximum Element is" << endl
<< SecMax;
//multiply the 2 elements
size_t total = (FisrtMax * SecMax);

cout << endl
<< "The Product of the 2 elements is  " << total;
return 0;
}

其输入:

please enter the number of elements in sequence
2
please enter the elements of the sequence
1000000
90000

输出:

sequence you entered
1000000   90000
First Maximum Element is
1e+06
sequence After removing the 1st maximum element
90000   
Second Maximum Element is
90000
The Product of the 2 elemnts is  90000000000

编辑:此程序成功地在OnlineGDB上运行,并且该程序是用C++14标志编译的。