获得最大的 Pair Wise 产品 C++ 并且输出并不总是与我的代码正确
Getting the maximum Pair Wise product C++ and the output is not always right with my code
要解决的问题:
给定一个数组或数字序列,目标是找到该序列中某些 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
代码有几个错误:
cout << ... << ' '
语法尝试使用单引号打印 three 空格 characters 字母一个字母不是多个。请改用 " "
。
产生的结果不能为整数,需要定义size_t
(在大多数编译器中展开为unsigned long long
)
小贴士:
在这个语法中:
for (int i = 0; i < input.size(); i++)
您正在尝试将整数与 size_t
(由向量 class 对象的 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 标志编译的。
要解决的问题: 给定一个数组或数字序列,目标是找到该序列中某些 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
代码有几个错误:
cout << ... << ' '
语法尝试使用单引号打印 three 空格 characters 字母一个字母不是多个。请改用" "
。产生的结果不能为整数,需要定义
size_t
(在大多数编译器中展开为unsigned long long
)
小贴士:
在这个语法中:
for (int i = 0; i < input.size(); i++)
您正在尝试将整数与
声明size_t
(由向量 class 对象的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 标志编译的。