1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iostream>
using namespace std;
void Recursion (int total, int needed, int numbers[], double &result,
double temp, int start, int cycle, int order[]);
int main()
{
int total = 5;
int needed = 2;
int numbers[5] = {300, 500, 100, 150, 800};
double result = 0;
double temp;
int start = 0;
int cycle = total - needed + 1;
int order[2];
Recursion(total,needed,numbers,result,temp,start,cycle,order);
cout << result;
return 0;
}
void Recursion (int total, int needed, int numbers[], double &result,
double temp, int start, int cycle, int order[])
{
if (needed != 0) order[needed - 1] = numbers[start];
else if (needed == 0)
{
double smallest = 1000, sum = 0;
for (int i = 0; i < needed; i++)
{
if (numbers[order[i]] < smallest) smallest = numbers[order[i]];
sum += numbers[order[i]];
}
temp = (smallest / sum) * 100;
if (temp > result) result = temp;
}
else for (int i = start; i < cycle; i++)
Recursion(total,needed - 1,numbers,result,temp,i + 1,cycle,order);
}
|