【问题标题】:Finding the max and min values in array of five times查找五次数组中的最大值和最小值
【发布时间】:2013-11-12 06:45:33
【问题描述】:

我应该找到数组中的最小值和最大值,但我似乎无法弄清楚为什么答案不正确。例如,如果我输入“1 2 3 4 5”作为我的五次,它告诉我 1 是我的最大值,0 是最小值。出于某种原因,无论第一个数字是什么,它都将其称为最大值,并将 0 指定为最小值。

#include <iostream>
using namespace std;

int find_distance(int j); //a function that returns a distance based on the choice j
int intmax, intmin;
int main( )
{

int i =0;
int distance[6]; 
double data[6][5]; 
for(int j = 0; j < 6; j++)
{
    distance[j] = find_distance(j);
    cout << "\nEnter 5 of your best running times for \n " << distance[j] << " m \n";
    for(int i = 0; i < 5; i++)
    {
        cout << "Enter a time \n"; cin >> data[j][i];
    }

}
cout << "Here is your best 5 times: ";
for(int j = 0; j < 6; j++)
{
cout << "\nDistance : " << distance[j] << " m \n";

for(int i = 0; i < 5; i++)
{
    system ("pause");
    cout << data[j][i] << "\t"; } cout << endl;

    if (data[j][i] < intmin) 
    intmin = data[j][i]; 
    else if (data[j][i] > intmax) 
    intmax = data[j][i]; 

    cout << "The maximum time is: " << intmax << endl;
    cout << "The minimum time is: "<< intmin << endl;
}
return 0;
}
int find_distance(int j)
{
switch (j)
{ case 0: // 100 meter 
return 100;
break;
case 1: // 150 meter 
return 150;
break;
case 2: // 200 meter 
return 200;
break;
case 3: // 400 meter 
return 400;
break;
case 4: // 500 meter 
return 800;
break;
default: // 1600 meter
    return 1600;
    }
}

【问题讨论】:

  • 为什么不对值进行排序并找到第一个和最后一个元素?
  • @nitinsh99 因为这会使它变成 O(log n) 而不是 O(n)?可能在一个非常短的数组的情况下这无关紧要,但我觉得这里有一个家庭作业,并且可能 OP 应该提出一个比给定所需的更通用的解决方案。
  • @wvxvw 你的意思是“o(nlogn)”对吧?
  • @nitinsh99 是的,这是一个错字。

标签: arrays input max average min


【解决方案1】:

最小值为 0,因为初始化 intmin 时,默认设置为 0。您永远不会输入负时间,因此在您的比较中它总是小于比较值。
最大值已关闭,因为您的 for 循环在一个奇怪的位置结束并且比较代码未正确执行。更改此代码:

for(int j = 0; j < 6; j++)
{
  cout << "\nDistance : " << distance[j] << " m \n";

for(int i = 0; i < 5; i++)
{
system ("pause");
cout << data[j][i] << "\t"; } cout << endl; //why does the for loop end here?

if (data[j][i] < intmin) 
intmin = data[j][i]; 
else if (data[j][i] > intmax) 
intmax = data[j][i]; 

            //move the end bracket to this line and it should work

cout << "The maximum time is: " << intmax << endl;
cout << "The minimum time is: "<< intmin << endl;
}

【讨论】:

    【解决方案2】:

    只是为了练习:

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <boost/regex.hpp>
    
    int main () {
      using namespace std;
    
      string input;
      boost::regex re("-?\\d+");
      vector<int> integers;
    
      cout << "enter sequence of integers: ";
      getline(cin, input);
    
      boost::sregex_token_iterator begin(input.begin(), input.end(), re, 0);
      boost::sregex_token_iterator end;
      while (begin != end) {
        integers.push_back(stoi(*begin));
        ++begin;
      }
    
      if (integers.size()) {
        auto pair = minmax_element(integers.begin(), integers.end());
        cout << "min: " << *pair.first << " max: " << *pair.second << endl;
      } else {
        cout << "you didn't enter any integers." << endl;
      }
      return 0;
    }
    

    这是编译和运行的方法:

    $ g++ -o lab_2 -std=c++11 -lboost_regex lab_2.cpp
    $ ./lab_2 
    $ enter sequence of integers: -10 34 75 101 2 43
    $ min: -10 max: 101
    

    需要安装 boost,因为 STL 正则表达式还不能正常工作。

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2018-05-03
      • 2016-06-06
      • 2017-08-18
      • 1970-01-01
      • 2014-06-11
      • 2015-09-29
      • 1970-01-01
      相关资源
      最近更新 更多