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

时间:2013-08-17 07:40:59

标签: c++ c++11

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

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

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];
}
}

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

13 个答案:

答案 0 :(得分:19)

#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;
}

答案 1 :(得分:10)

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

#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;
}

声明:
技术上,C ++标准不需要这样做。 minmax_element所需的最小迭代器类别是ForwardIterator,而流迭代器则不是。一旦输入迭代器被解除引用或递增,其副本就不再保证可解除引用或与其他迭代器相当。它适用于我的机器 TM 。 :)

答案 2 :(得分:5)

您可以这样做:

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;

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

答案 3 :(得分:1)

例如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;
   }
}

答案 4 :(得分:0)

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

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;
}

您可以使用类似的逻辑来填充最小值

答案 5 :(得分:0)

让max最多可容纳5个数字。将第一个数字分配给最大值。取第二个数字,如果第二个数字大于max,则将其与max进行比较,然后将其分配给max,否则不执行任何操作。接下来取第3个数字并将其与max进行比较,如果第3个数字大于max,则将其指定为max,否则不执行任何操作。对第4和第5个数字执行相同操作。最后max将保持最多5个数字。

答案 6 :(得分:0)

><是传递属性,因此如果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);

答案 7 :(得分:0)

您可以使用list(或vector),它不是数组:

#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;
}

答案 8 :(得分:0)

使用分拣网络!

#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';
}

答案 9 :(得分:0)

如果你想保持简单,那么这就是我的解决方案。

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

#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;
}

答案 10 :(得分:0)

void main()
{
int a,b,c,d,e,max;
    max=a;
    if(b/max)
        max=b;
    if(c/max)
        max=c;
    if(d/max)
        max=d;
    if(e/max)
        max=e;
    cout<<"Maximum is"<<max;
}

答案 11 :(得分:0)

继承我所做的,不使用数组。这是一种返回最多5个分数的方法。

double findHighest(double score1, double score2, double score3, double score4, double score5)
 {
   double highest = score1;
   if (score2 > score1 && score2 > score3 && score2 > score4 && score2 > score5)
      highest = score2;
   if(score3 > score1 && score3 > score2 && score3 > score4 && score3 > score5)
      highest = score3;
   if(score4 > score1 && score4 > score2 && score4 > score3 && score4 > score5)
      highest = score4;
   if (score5 > score1 && score5 > score2 && score5 > score3 && score5 > score4)
      highest = score5;
   return highest;
 }

数组效率会更高,但我不得不在不使用数组的情况下完成作业。

答案 12 :(得分:0)

这是我的实施:简单和简短

#include <iostream>
#include <cstdio>
using namespace std;


int max_of_five(int a, int b, int c, int d,int e){
    int large= max(max(a, b), max(c,d));
    return max(large,e);

}

int min_of_five(int a,int b,int c, int d,int e){
    int small=min(min(a,b),min(c,d));
    return min(small,e);
}


int main() {
    int a, b, c, d,e;

    scanf("%d %d %d %d %d", &a, &b, &c, &d,&e);
    int ans = max_of_five(a, b, c, d, e);
    int ans1=min_of_five(a,b,c,d,e);
    printf("Max:\t%d\n", ans);
    printf("Min:\t%d", ans1);

    return 0;
}