C++ 在不使用数组的情况下在 5 个数字中找到最大和最小的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18286384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:51:03  来源:igfitidea点击:

Program to find largest and smallest among 5 numbers without using array

c++c++11

提问by mvp

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.

昨天我去参加了一个面试,要求我创建一个程序来在不使用数组的情况下找到 5 个数字中的最大和最小。

I know how to create the program using array.

我知道如何使用数组创建程序。

int largestNumber;
int smallestNumber;
int numbers[n];

largestNumber=numbers[0];
smallestNumber=numbers[0];
for (i=0 ; i<n; i++)
{
if (numbers[i] > largestNumber) 
{
largest = numbers[i];
}
if (numbers[i] < smallestNumber) 
{
smallestNumber= numbers[i];
}
}

But how to create it without using array. Any help??

但是如何在不使用数组的情况下创建它。有什么帮助吗??

回答by

#include <algorithm>
#include <iostream>

template <typename T>
inline const T&
max_of(const T& a, const T& b) {
    return std::max(a, b);
}

template <typename T, typename ...Args>
inline const T&
max_of(const T& a, const T& b, const Args& ...args) {
    return max_of(std::max(a, b), args...);
}

int main() {
    std::cout << max_of(1, 2, 3, 4, 5) << std::endl;
    // Or just use the std library:
    std::cout << std::max({1, 2, 3, 4, 5}) << std::endl;
    return 0;
}

回答by jrok

Works for any number of numbers taken from standard input:

适用于从标准输入中提取的任意数量的数字:

#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    std::istream_iterator<int> it_begin(std::cin), it_end;
    auto p = std::minmax_element(it_begin, it_end);
    if (p.first != it_end)
        std::cout << "min: " << *p.first << " max: " << *p.second;
}

Disclaimer:
Technicaly, this isn't required to work by C++ standard. The minimum iterator category required for minmax_elementis ForwardIteratorwhich stream iterators are not. Once an input iterator is dereferenced or incremented, its copies are no longer guaranteed to be dereferenceable or comparable to other iterators. It Works On My MachineTM. :)

免责声明:
从技术上讲,这不是 C++ 标准所要求的。所需的最小迭代器类别minmax_elementForwardIterator哪些流迭代器不是。一旦输入迭代器被取消引用或递增,它的副本不再保证可取消引用或与其他迭代器进行比较。它适用于我的机器TM。:)

回答by mvp

You can do something like this:

你可以这样做:

int min_num = INT_MAX;  //  2^31-1
int max_num = INT_MIN;  // -2^31
int input;
while (!std::cin.eof()) {
    std::cin >> input;
    min_num = min(input, min_num);
    max_num = max(input, max_num);
}
cout << "min: " << min_num; 
cout << "max: " << max_num;

This reads numbers from standard input until eof (it does not care how many you have - 5 or 1,000,000).

这从标准输入读取数字直到 eof(它不关心你有多少 - 5 或 1,000,000)。

回答by xanatos

The >and <are transitive properties, so if a > band b > c, then a > c. So you can

><是可传递性,所以如果a > bb > c,然后a > c。这样你就可以

int a=10, b=6, c=4, d=21, e=4;

int maxNum = a;
int maxNum = max(b, maxNum);
int maxNum = max(c, maxNum);
int maxNum = max(d, maxNum);
int maxNum = max(e, maxNum);

回答by Sushruth Siv

This is not an efficient answer but it still works

这不是一个有效的答案,但它仍然有效

int a,b,c,d,e,largest;
if ((a>b) and (a>c) and (a>d) and (a>e))
{    
    largest=a;
}
else if ((b>a) and (b>c) and (b>d) and (b>e))
{    
    largest=b;
}
else if ((c>a) and (c>a) and (c>d) and (c>e))
{    
    largest=c;
}
else if ((d>a) and (d>c) and (d>a) and (d>e))
{    
    largest=d;
}
else 
{
largest=e;
}

you can use similar logic to fid the smallest value

您可以使用类似的逻辑来找到最小值

回答by Atish Bundhe

Let max will hold the maximum of 5 numbers. Assign the first number to max. Take the 2nd number and compare it with max if the the 2nd number is greater than max then assign it to max else do nothing. Next take the 3rd number and compare it with max , if the 3rd number is greater than max assign it to max else do nothing. Do the same for 4th and 5th number. Finally max will hold the maximum of 5 number.

让 max 最多容纳 5 个数字。将第一个数字分配给最大值。如果第二个数字大于 max,则取第二个数字并将其与 max 进行比较,然后将其分配给 max 否则什么都不做。接下来取第三个数字并将其与 max 进行比较,如果第三个数字大于 max 将其分配给 max 否则什么都不做。对第 4 个和第 5 个数字执行相同操作。最后 max 将容纳 5 个数字的最大值。

回答by Elvin Mammadov

For example 5 consecutive numbers

例如5个连续的数字

int largestNumber;
int smallestNumber;
int number;
std::cin>>number;
largestNumber = number;
smallestNumber = number;
for (i=0 ; i<5; i++)
{
   std::cin>>number;
   if (number > largestNumber) 
   {
     largest = number;
   }
   if (numbers < smallestNumber) 
   {
     smallestNumber= number;
   }
}

回答by cpp

You could use list (or vector), which is not an array:

您可以使用列表(或向量),它不是数组:

#include<list>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
    list<int> l;
    l.push_back(3); 
    l.push_back(9); 
    l.push_back(30);    
    l.push_back(0); 
    l.push_back(5); 

    list<int>::iterator it_max = max_element(l.begin(), l.end());
    list<int>::iterator it_min = min_element(l.begin(), l.end());

    cout << "Max: " << *it_max << endl;
    cout << "Min: " << *it_min << endl;
}

回答by Blastfurnace

Use a sorting network!

使用排序网络!

#include <iostream>
#include <utility>

int main()
{
    int a, b, c, d, e;
    std::cin >> a >> b >> c >> d >> e;

    if (a < b) std::swap(a, b);
    if (d < e) std::swap(d, e);
    if (c < e) std::swap(c, e);
    if (c < d) std::swap(c, d);
    if (b < e) std::swap(b, e);
    if (a < d) std::swap(a, d);
    if (a < c) std::swap(a, c);
    if (b < d) std::swap(b, d);
    if (b < c) std::swap(b, c);

    std::cout << "largest = " << a << '\n';
    std::cout << "smallest = " << e << '\n';
}

回答by user3834119

If you like to keep things simple, then here is my solution.

如果您喜欢保持简单,那么这是我的解决方案。

It works for any number of integers taken from standard input. It also works for negative integers. Enter end when you are done.

它适用于从标准输入中提取的任意数量的整数。它也适用于负整数。完成后输入结束。

#include <iostream>

int main()
{
int max,min,input;
std::cout<<"Enter the number: ";
std::cin>>input;
min=max=input;

while(std::cin>>input){
    if(input>max) max=input;
    if(input<min) min=input;
    std::cout<<"Enter the number: ";
}
std::cout<<"\nMax: "<<max<<"\nMin: "<<min;
}