-1

要解决的问题:给定一个数组或数字序列,目标是找到通过该序列中一些 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
4

1 回答 1

2

代码中有几个错误:

  1. cout << ... << ' '语法尝试使用单引号打印三个空白字符字母,该单引号接受单个字母而不是多个字母。改为使用" "

  2. 产生的结果不能由整数保存,您需要定义(在大多数编译器size_t中扩展为)。unsigned long long

小贴士:

  • 在这种语法中:

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

    您正在尝试将整数与size_t(由size()矢量类对象的成员函数返回)进行比较。而不是这样,i用声明size_t

  • 在这种语法中:

     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 标志编译的。

于 2020-07-09T20:06:40.133 回答