C ++循环,输入的最大/最小数字(C++ looping, Largest/smallest number inputted)

编程入门 行业动态 更新时间:2024-10-22 21:23:23
C ++循环,输入的最大/最小数字(C++ looping, Largest/smallest number inputted)

我试图找出一个代码,可以确定输入的一组数字的最大数字,最小数字,第二大数字和第二小数字。 这是我的代码。 我遇到的问题主要是由于变量的初始化。 我很感激任何帮助!

我的代码:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


int main(){
    string buffer;
    unsigned n;
    double min = 1,  max = 0;
    double min2 = 1, max2 = 5;
    cout << "How many integers will you enter? ";
    cin >> n || die("Input failure ");
    cout << "OK type the " << n << " integers, non #'s to quit: ";
    for (unsigned count = 0; count < n; count++){
        double num;
        cin >> num;
        if (min > num){
            min = num;
        }
        else if (max < num){
            max = num;
        }
        else if (num >= max2 && num <= max){
            max2 = num;
        }
        else if (num >= min2 && num <= min ){
                min2 = num;
        }

    } cout << "The largest number is: " << max << endl;
    cout << "The smallest number is: " << min << endl;
    cout << "The second smallest number is: " << min2 << endl;
    cout << "The second largest number is: " << max2 << endl;

    cin >> buffer;
}

I'm trying to figure out a code that can determine the largest number, smallest number, second largest number, and second smallest number for ay set of numbers inputted. Here is my code. I am having issues mainly due to the initialization of variables. I'd appreciate any help!

My code:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


int main(){
    string buffer;
    unsigned n;
    double min = 1,  max = 0;
    double min2 = 1, max2 = 5;
    cout << "How many integers will you enter? ";
    cin >> n || die("Input failure ");
    cout << "OK type the " << n << " integers, non #'s to quit: ";
    for (unsigned count = 0; count < n; count++){
        double num;
        cin >> num;
        if (min > num){
            min = num;
        }
        else if (max < num){
            max = num;
        }
        else if (num >= max2 && num <= max){
            max2 = num;
        }
        else if (num >= min2 && num <= min ){
                min2 = num;
        }

    } cout << "The largest number is: " << max << endl;
    cout << "The smallest number is: " << min << endl;
    cout << "The second smallest number is: " << min2 << endl;
    cout << "The second largest number is: " << max2 << endl;

    cin >> buffer;
}

                

最满意答案

我建议采用略有不同的方法。

C ++有许多数据结构,如std::set , std::map和std::vector ,它们对不同的场景很有用。 例如,如果您知道您的值是唯一的,则可以将它们全部放在std::set ,这样可以按大小顺序保存值。 然后,取前两个和后两个将给出最高和最低值。 如果你的值不是唯一的,你可以将它们全部放在std::vector ,对矢量进行排序,然后做同样的事情:取前两个和后两个元素。

这是我的意思的一个例子,使用std::set - 我还没有测试过,但如果你决定采用这种方法,它应该让你开始。

#import <set>

int main() {

  std::set<double> mySet;

  mySet.insert(0.0);
  mySet.insert(100.0);
  mySet.insert(10.0);
  mySet.insert(90.0);
  mySet.insert(20.0);
  mySet.insert(80.0);

  double min = *(mySet.begin());
  double min2 = *(mySet.begin()+1);
  double max = *(mySet.end()-1);
  double max2 = *(mySet.end()-2);

  return 0;
}
 

希望有所帮助!

I'd suggest a slightly different approach.

C++ has a number of data structures such as std::set, std::map, and std::vector, that are useful for different scenarios. For example, if you know your values are unique, you could put them all in a std::set, which keeps values ordered in order of size. Then, taking the first two and last two would give you the highest and lowest values. If your values aren't unique, you could put them all in a std::vector, sort the vector, and then do the same thing: take the first two and last two elements.

Here's an example of what I mean, using std::set -- I haven't tested this yet, but it should get you started, if you decide to take this approach.

#import <set>

int main() {

  std::set<double> mySet;

  mySet.insert(0.0);
  mySet.insert(100.0);
  mySet.insert(10.0);
  mySet.insert(90.0);
  mySet.insert(20.0);
  mySet.insert(80.0);

  double min = *(mySet.begin());
  double min2 = *(mySet.begin()+1);
  double max = *(mySet.end()-1);
  double max2 = *(mySet.end()-2);

  return 0;
}
 

Hope that helps!

更多推荐

本文发布于:2023-08-02 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最小   数字   looping   Largest   number

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!