【问题标题】:Program to find largest and smallest among 5 numbers without using array程序在不使用数组的情况下查找 5 个数字中的最大和最小
【发布时间】:2013-08-17 07:40:58
【问题描述】:

昨天我去参加一个面试,我被要求创建一个程序来在不使用数组的情况下找到 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];
}
}

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

【问题讨论】:

  • 那么数字在哪里,如果它们不在数组中?在云端? :-)
  • std::cin,根本没有存储空间,只有最大值和最小值
  • 可以,不用数组也可以。
  • 使用数组,std::minmax_element 是比滚动自己更好的解决方案。

标签: c++ c++11


【解决方案1】:
#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;
}

【讨论】:

  • 所以现在将其应用于从std::cin 读取的一组数字,而不使用数组(或类似存储)。
  • 编辑很重要。我很惊讶直到现在还没有人提到 std::max 的初始化列表形式。
  • 这不适用于任意大小的输入,例如数百万或数十亿的数字。
  • @mvp:问题要求 5... 你是怎么来的 bilions?
  • @mvp: std::max_element
【解决方案2】:

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

#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。 :)

【讨论】:

    【解决方案3】:

    你可以这样做:

    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)。

    【讨论】:

    • 如果我输入“hello”而不是数字呢?
    • 此解决方案需要格式良好的输入。改进它以处理错误输入留给读者作为练习。
    【解决方案4】:

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

    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】:

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 我认为 number 的初始化消耗了 5 个值中的 1 个,这意味着你的 for 循环太大了。
        【解决方案7】:

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

        #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】:

          &gt;&lt; 是传递属性,所以如果 a &gt; bb &gt; c,那么 a &gt; 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);
          

          【讨论】:

            【解决方案9】:

            使用排序网络!

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

            【讨论】:

            • 任务不需要排序时为什么要排序?
            • @VioletGiraffe:我有点无聊,所以我的回答并不完全认真。
            【解决方案10】:

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

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

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

            【讨论】:

              【解决方案11】:
              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;
              }
              

              【讨论】:

              • 使用这样的编码技巧可能会奏效,但要回答这个问题,它需要一些关于它的作用和工作原理的解释。
              【解决方案12】:

              这就是我所做的,没有使用数组。这是一种返回最高 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;
               }
              

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

              【讨论】:

                【解决方案13】:
                int findMin(int t1, int t2, int t3, int t4, int t5)
                {
                    int min1, min2, min3;
                
                    min1 = std::min(t1, t2);
                    min2 = std::min(t3, t4);
                    min3 = std::min(min1, min2);
                    return std::min(min3, t5);
                }
                
                int findMax(int t1, int t2, int t3, int t4, int t5)
                {
                    int max1, max2, max3;
                
                    max1 = std::max(t1, t2);
                    max2 = std::max(t3, t4);
                    max3 = std::max(max1, max2);
                    return std::max(max3, t5);
                }
                

                这些函数非常杂乱但易于理解,因此易于记忆,它只使用最适合 2 个值的简单 min 和 max 方法。

                【讨论】:

                  【解决方案14】:
                  int findMin(int t1, int t2, int t3, int t4, int t5)
                  {
                      int min;
                  
                      min = t1;
                      if (t2 < min)
                          min = t2;
                      if (t3 < min)
                          min = t3;
                      if (t4 < min)
                          min = t4;
                      if (t5 < min)
                          min = t5;
                  
                      return min;
                  }
                  
                  int findMax(int t1, int t2, int t3, int t4, int t5)
                  {
                      int max;
                  
                      max = t1;
                      if (t2 > max)
                          max = t2;
                      if (t3 > max)
                          max = t3;
                      if (t4 > max)
                          max = t4;
                      if (t5 > max)
                          max = t5;
                  
                      return max;
                  }
                  

                  【讨论】:

                    【解决方案15】:

                    这是我的实现:简单而简短

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

                    【讨论】:

                      【解决方案16】:
                      #include<iostream>
                      
                      using namespace std;
                      
                      int main()
                      {
                      
                          int largestNumber;
                          int smallestNumber;
                          int temp1;
                          cin >> temp1;
                          largestNumber = temp1;
                          smallestNumber = temp1;
                      
                          for (int i = 0; i < 5; i++)
                          {
                              int temp;
                              cin >> temp;
                              
                              for (int i = 0; i < 3; i++)
                              {
                                  if (temp > largestNumber)
                                  {
                                      largestNumber = temp;
                                  }
                                  if (temp < smallestNumber)
                                  {
                                      smallestNumber = temp;
                                  }
                              }
                          }
                          cout << "smallest number is " << smallestNumber<<endl;
                          cout << "lagest   number   is " << largestNumber<<endl;
                      
                      
                          system("pause");
                          return 0;
                      }
                      

                      【讨论】:

                      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
                      • 您正在为 0-1-2-3-4 运行外循环,这意味着 5 个数字,加上第一个条目使其成为 6 个数字。固定号码。并且内部循环运行相同的操作 3 次,您只需要该操作一次。所以删除循环。
                      猜你喜欢
                      • 2015-01-25
                      • 2015-04-28
                      • 1970-01-01
                      • 2020-01-12
                      • 2020-07-13
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-12-27
                      相关资源
                      最近更新 更多