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

C++ 程序在不使用数组的情况下查找5个数字中的最大值和最小值,c++,c++11,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 = nu

昨天我参加了一次采访,采访中我被要求创建一个程序,在不使用数组的情况下查找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];
}
}
int最大数;
整数最小数;
整数[n];
最大数量=数量[0];
最小数量=数字[0];
对于(i=0;i最大数)
{
最大=数字[i];
}
if(数字[i]
但是如何在不使用数组的情况下创建它。有什么帮助吗
#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;
}
#包括 模板 内联常数& 最大值(常数T&a、常数T&b){ 返回标准::最大值(a,b); } 模板 内联常数& 最大值(常数T&a、常数T&b、常数参数和…参数){ 返回最大值(标准::最大值(a,b),参数; } int main(){
这不是一个有效的答案,但它仍然有效

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

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

让max最多保存5个数字。将第一个数字分配给max。如果第二个数字大于max,则将第二个数字与max进行比较,如果第二个数字大于max,则将其分配给max,否则不执行任何操作。接下来,如果第三个数字大于ma,则将第三个数字与max进行比较x将其分配给max,否则不执行任何操作。对第4个和第5个数字执行相同操作。最后max将保留最多5个数字。

例如,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;
   }
}
int最大数;
整数最小数;
整数;
标准::cin>>编号;
最大数量=数量;
smallestNumber=数字;
对于(i=0;i>number;
如果(编号>最大编号)
{
最大=数量;
}
如果(数字<最小数字)
{
smallestNumber=数字;
}
}

您可以执行以下操作:

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;
int-min\u-num=int\u-MAX;//2^31-1
int max\u num=int\u MIN;//-2^31
int输入;
而(!std::cin.eof()){
std::cin>>输入;
最小值=最小值(输入,最小值);
max_num=max(输入,max_num);
}

因为
是可传递的属性,所以如果codea>b
b>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);

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

#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;
}
#包括
#包括
#包括
int main()
{
std::istream\u迭代器it\u begin(std::cin),it\u end;
自动p=std::最小最大元素(开始,结束);
如果(p.first!=结束)
std::cout您可以使用列表(或向量),它不是数组:

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
清单l;
l、 推回(3);
l、 推回(9);
l、 推回(30);
l、 推回(0);
l、 推回(5);
列表::迭代器it_max=max_元素(l.begin(),l.end());
列表::迭代器it_min=min_元素(l.begin(),l.end());
不能使用排序网络

#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';
}
#包括
#包括
int main()
{
INTA、b、c、d、e;
标准:cin>>a>>b>>c>>d>>e;
if(a如果你想让事情变得简单,那么下面是我的解决方案

它适用于从标准输入中获取的任意数量的整数。它也适用于负整数。完成后输入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;
}
#包括
int main()
{
int最大值,最小值,输入;
std::coutinput;
最小=最大=输入;
while(标准::cin>>输入){
如果(输入>最大值)最大值=输入;
如果(输入
void main()
{
内部a、b、c、d、e、最大值;
max=a;
如果(b/max)
max=b;
如果(c/max)
max=c;
如果(d/max)
max=d;
如果(e/max)
max=e;

cout这是我在不使用数组的情况下所做的。这是一种返回最高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;
 }
数组的效率要高得多,但我不得不在不使用数组的情况下做作业

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个值的简单的最小值和最大值方法。

以下是我的实现:简单而简短

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;
}
#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;
}
#包括
#包括
使用名称空间std;
五分之一的整数最大值(整数a、整数b、整数c、整数d、整数e){
int large=max(max(a,b),max(c,d));
返回最大值(大,e);
}
五分之一的整数(整数a、整数b、整数c、整数d、整数e){
int small=min(min(a,b),min(c,d));
返回最小值(小,e);
}
int main(){
INTA、b、c、d、e;
scanf(“%d%d%d%d%d”、&a、b、c、d和e);
int ans=五(a,b,c,d,e)中的最大值;
int ans1=五(a,b,c,d,e)中的最小值;
printf(“最大值:\t%d\n”,ans);
printf(“最小值:\t%d”,ans1);
返回0;
}

那么,如果数字不在数组中,它们在哪里?在云中?:-)std::cin,根本没有存储,只有max和minYes,你可以不用数组来完成。有了数组,
std::minmax\U元素
是一个比滚动你自己的更好的解决方案。因此,现在将其应用于从
std::cin
读取的一组数字,而不使用数组(或类似的存储)。编辑很重要。我很惊讶直到现在还没有人提到
std::max
的初始值设定项列表形式。这对任意大小的输入不起作用,比如数百万或数十亿个数字。@mvp:问题是问5…你是如何得到billions的?@mvp:
std::max_元素
,如果我输入“你好”怎么办而不是数字?这个解决方案需要格式良好的输入。改进它以处理错误的输入是留给读者的一个练习。为什么在任务不需要排序的情况下进行排序?@VioletGiraffe:我有点无聊,所以我的答案并不完全严肃。我认为数字的初始化消耗了5个值中的1个,这意味着你的for循环太大了。使用这样的编码技巧可能会奏效,但要回答这个问题,需要解释一下它的功能和工作原理。
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;
}
#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;
}