簡體   English   中英

在不使用數組的情況下在 5 個數字中找到最大和最小的程序

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

昨天我去參加了一個面試,要求我創建一個程序來在不使用數組的情況下找到 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];
}
}

但是如何在不使用數組的情況下創建它。 有什么幫助嗎??

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

適用於從標准輸入中獲取的任意數量的數字:

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

你可以這樣做:

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

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

您可以使用類似的邏輯來填充最小的值

設max最多可容納5個數字。 將第一個數字分配給最大值。 取第二個數字,如果第二個數字大於max,則將其與max進行比較,然后將其分配給max,否則不執行任何操作。 接下來取第3個數字並將其與max進行比較,如果第3個數字大於max,則將其指定為max,否則不執行任何操作。 對第4和第5個數字執行相同操作。 最后max將保持最多5個數字。

><是傳遞屬性,因此如果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);

您可以使用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;
}

使用分揀網絡!

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

如果你想保持簡單,那么這就是我的解決方案。

它適用於從標准輸入中獲取的任意數量的整數。 它也適用於負整數。 完成后輸入結束。

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

繼承人我做了什么,沒有使用數組。 這是一種返回最多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;
}
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;
}

#包括

使用命名空間標准;

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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM