1 C[ | ]
C
Copy
int sum(int* numbers, int numbersCount) {
if( numbersCount < 3 ) return 0;
int sum = 0;
int max = numbers[0];
int min = numbers[0];
for(int i=0; i<numbersCount; i++) {
sum += numbers[i];
if( numbers[i] > max ) max = numbers[i];
if( numbers[i] < min ) min = numbers[i];
}
return sum-max-min;
}
2 C++[ | ]
C++
Copy
#include<vector>
using namespace std;
int sum(vector<int> numbers)
{
if (numbers.size() < 2) return 0;
int sum = 0;
int min = numbers[0], max = numbers[0];
for (int n : numbers) {
if (n < min) min = n;
else if (n > max) max = n;
sum += n;
}
return sum - max - min;
}
C++
Copy
#include<vector>
using namespace std;
int sum(vector<int> numbers)
{
if( numbers.size() < 3 ) return 0;
int sum = 0;
int max = numbers[0];
int min = numbers[0];
for(int i=0; i<numbers.size(); i++) {
sum += numbers[i];
if( numbers[i] > max ) max = numbers[i];
else if( numbers[i] < min ) min = numbers[i];
}
return sum-max-min;
}
C++
Copy
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
int sum(vector<int> numbers)
{
if ( numbers.size() < 2 ) return 0;
return accumulate(numbers.begin(),numbers.end(),0)
- *max_element(numbers.begin(),numbers.end())
- *min_element(numbers.begin(),numbers.end());
}
3 PHP[ | ]
PHP
PHP
PHP