在我的代码中,获得最大的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 << ... << ' '
语法尝试使用接受单个字母而非多个字母的单引号打印三个空白字符字母。请改用" "
。 -
生成的结果不能用整数表示,需要定义
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标志编译的。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词