给定一个数组。任务是找到给定数组的最大和最小元素的和与乘积。
例子:
Input : arr[] = {12, 1234, 45, 67, 1}
Output : Sum = 1235
Product = 1234
Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
Output : Sum = 10
Product = 9
取变量min和max来存储数组的最小和最大元素。找到最小和最大元素并分别存储在这些变量中。最后,打印最小和最大元素的总和与乘积。
下面是说明上述方法的程序:
C++
// CPP program to find the sum and product
// of minimum and maximum element in an array
#include
using namespace std;
// Function to find minimum element
int getMin(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = min(res, arr[i]);
return res;
}
// Function to find maximum element
int getMax(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = max(res, arr[i]);
return res;
}
// Function to get Sum
int findSum(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
int findProduct(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
// Driver Code
int main()
{
int arr[] = { 12, 1234, 45, 67, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Sum of min and max element
cout << "Sum = " << findSum(arr, n) << endl;
// Product of min and max element
cout << "Product = " << findProduct(arr, n);
return 0;
}
Java
// Java program to find the sum and product
// of minimum and maximum element in an array
import java.io.*;
class GFG {
// Function to find minimum element
static int getMin(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.min(res, arr[i]);
return res;
}
// Function to find maximum element
static int getMax(int arr[], int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
// Function to get Sum
static int findSum(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
static int findProduct(int arr[], int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 12, 1234, 45, 67, 1 };
int n = arr.length;
// Sum of min and max element
System.out.println ("Sum = " + findSum(arr, n));
// Product of min and max element
System.out.println( "Product = " + findProduct(arr, n));
}
}
//This Code is contributed by anuj_67....
Python 3
# Python 3 program to find the sum and product
# of minimum and maximum element in an array
# Function to find minimum element
def getMin(arr, n):
res = arr[0]
for i in range(1, n):
res = min(res, arr[i])
return res
# Function to find maximum element
def getMax(arr, n):
res = arr[0]
for i in range(1, n):
res = max(res, arr[i])
return res
# Function to get Sum
def findSum(arr, n):
min = getMin(arr, n)
max = getMax(arr, n)
return min + max
# Function to get product
def findProduct(arr, n):
min = getMin(arr, n)
max = getMax(arr, n)
return min * max
# Driver Code
if __name__ == "__main__":
arr = [ 12, 1234, 45, 67, 1 ]
n = len(arr)
# Sum of min and max element
print("Sum = " , findSum(arr, n))
# Product of min and max element
print("Product = " , findProduct(arr, n))
# This code is contributed
# by ChitraNayal
C#
// C# program to find the sum and product
// of minimum and maximum element in an array
using System;
class GFG {
// Function to find minimum element
static int getMin(int []arr, int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.Min(res, arr[i]);
return res;
}
// Function to find maximum element
static int getMax(int []arr, int n)
{
int res = arr[0];
for (int i = 1; i < n; i++)
res = Math.Max(res, arr[i]);
return res;
}
// Function to get Sum
static int findSum(int []arr, int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min + max;
}
// Function to get product
static int findProduct(int []arr, int n)
{
int min = getMin(arr, n);
int max = getMax(arr, n);
return min * max;
}
// Driver Code
public static void Main()
{
int []arr = { 12, 1234, 45, 67, 1 };
int n = arr.Length;
// Sum of min and max element
Console.WriteLine("Sum = " + findSum(arr, n));
// Product of min and max element
Console.WriteLine( "Product = " + findProduct(arr, n));
}
}
// This Code is contributed by anuj_67....
PHP
输出:
Sum = 1235
Product = 1234
最佳化
我们可以使用一个循环来找到最大值和最小值。这将只需要遍历数组一次。
在C++中,有直接函数可以找到最大值和最小值:max_element()和min_element()