如何求最大值和最小值?

时间:2022-06-01 21:05:08
#include<iostream>
main()
{
    int limit,input;
    int sum=0;
    int i;
    std::cout<<"Please Enter the limit: ";
    std::cin>>limit;
    for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;
        sum+=input;
    }
    std::cout<<"The sum of the values is: "<<sum;
    std::cout << std::endl;
    std::cout<<"The average of the values is: "<<sum/1+i;


}

What to do if i want to find Max and Min Values from the values input by the user?

如果我想从用户输入的值中找到最大值和最小值,该怎么办?

5 个解决方案

#1


0  

Please try the code below.

请尝试下面的代码。

int main()
{
    int limit,input;
    int sum=0;
    int i;
    std::cout<<"Please Enter the limit: ";
    std::cin>>limit;
    int min = 0;
    int max = 0;
    for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;

        if(i == 0)
        {
            min = input;
            max = input;
        }
        else
        {
            if(input < min)
                min = input;

            if(input > max)
                max = input;
        }
        sum+=input;
    }
    std::cout<<"The sum of the values is: "<<sum<<std::endl;
    std::cout<<"The average of the values is: "<<sum/(1+i)<<std::endl;
    std::cout<<"Max: "<<max<<std::endl;
    std::cout<<"Min: "<<min<<std::endl;

    return 0;
}

#2


4  

Try this

试试这个

int min_v = std::numeric_limits<int>::max();
int max_v = std::numeric_limits<int>::min();

for(i=0;i<limit;i++)
{
    std::cout<<"Please Input values: ";
    std::cin>>input;
    sum+=input;
    min_v = std::min(input, min_v);
    max_v = std::max(input, max_v);
}

#3


1  

There are multiple ways, You can store all values input from user into a std::vector<int>. Use this vector to find out sum of elements and then sort it using std::sort to get Max and Min values as well. Simple!

有多种方法,可以将用户输入的所有值存储到std::vector 中。使用这个向量找出元素的和,然后使用std::sort对它进行排序,以获得最大值和最小值。简单!

vector<int> v(limits);
for(int i = 0; i<limits; i++)
{
    int input;
    cin >> input;

    v.push_back(input);
}

int sum = 0;
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
    sum += *it;
}

cout << "sum = " << sum << endl;


sort(v.begin(), v.end());

cout << "Min = " << v[0] << endl;
cout << "Max = " << v[v.size() -1] << endl;

#4


0  

Generally you need to do the following:

一般来说,你需要做以下工作:

  1. Declare the variable to store Max and Min values.
  2. 声明该变量以存储Max和Min值。
  3. Initialize Min with a very big value, e.g. 2147483647, and Max to a very small value, e.g. -2147483648.
  4. 用一个非常大的值初始化Min,例如2147483647,最大值为一个非常小的值,例如-2147483648。
  5. Inside the loop, compare input with Min, if input is smaller than Min, update Min. Do the reverse with Max.
  6. 在循环内部,将输入与最小值进行比较,如果输入小于最小值,则更新最小值。

That's it.

就是这样。

#5


0  

int max=0, min=0;
for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;
        sum+=input;

        if(i==0){
        max=input;
        min=input;
        }

        if(input>max)
          max=input;

        if(input<min)
          min=input;
    }

Now, you got the max and min value.

#1


0  

Please try the code below.

请尝试下面的代码。

int main()
{
    int limit,input;
    int sum=0;
    int i;
    std::cout<<"Please Enter the limit: ";
    std::cin>>limit;
    int min = 0;
    int max = 0;
    for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;

        if(i == 0)
        {
            min = input;
            max = input;
        }
        else
        {
            if(input < min)
                min = input;

            if(input > max)
                max = input;
        }
        sum+=input;
    }
    std::cout<<"The sum of the values is: "<<sum<<std::endl;
    std::cout<<"The average of the values is: "<<sum/(1+i)<<std::endl;
    std::cout<<"Max: "<<max<<std::endl;
    std::cout<<"Min: "<<min<<std::endl;

    return 0;
}

#2


4  

Try this

试试这个

int min_v = std::numeric_limits<int>::max();
int max_v = std::numeric_limits<int>::min();

for(i=0;i<limit;i++)
{
    std::cout<<"Please Input values: ";
    std::cin>>input;
    sum+=input;
    min_v = std::min(input, min_v);
    max_v = std::max(input, max_v);
}

#3


1  

There are multiple ways, You can store all values input from user into a std::vector<int>. Use this vector to find out sum of elements and then sort it using std::sort to get Max and Min values as well. Simple!

有多种方法,可以将用户输入的所有值存储到std::vector 中。使用这个向量找出元素的和,然后使用std::sort对它进行排序,以获得最大值和最小值。简单!

vector<int> v(limits);
for(int i = 0; i<limits; i++)
{
    int input;
    cin >> input;

    v.push_back(input);
}

int sum = 0;
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
    sum += *it;
}

cout << "sum = " << sum << endl;


sort(v.begin(), v.end());

cout << "Min = " << v[0] << endl;
cout << "Max = " << v[v.size() -1] << endl;

#4


0  

Generally you need to do the following:

一般来说,你需要做以下工作:

  1. Declare the variable to store Max and Min values.
  2. 声明该变量以存储Max和Min值。
  3. Initialize Min with a very big value, e.g. 2147483647, and Max to a very small value, e.g. -2147483648.
  4. 用一个非常大的值初始化Min,例如2147483647,最大值为一个非常小的值,例如-2147483648。
  5. Inside the loop, compare input with Min, if input is smaller than Min, update Min. Do the reverse with Max.
  6. 在循环内部,将输入与最小值进行比较,如果输入小于最小值,则更新最小值。

That's it.

就是这样。

#5


0  

int max=0, min=0;
for(i=0;i<limit;i++)
    {
        std::cout<<"Please Input values: ";
        std::cin>>input;
        sum+=input;

        if(i==0){
        max=input;
        min=input;
        }

        if(input>max)
          max=input;

        if(input<min)
          min=input;
    }

Now, you got the max and min value.