da Kawa46 » 17/08/2014, 15:05
Sono tutti numeri interi. Per ora ho provato a cercare qualche info su internet però non ho trovato nulla del genere.
La cosa più utile che ho trovato è stato questo programma che ho adattato al mio problema (non l'ho scritto io, non voglio rubare il merito a chi l'ha fatto):
// Program to print all combination of size r in an array of size n
#include <stdio.h>
void combinationUtil(int arr[], int data[], int start, int end, int index, int r);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[], int start, int end, int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j<r; j++)
printf("%d ", data[j]);
printf("\n");
return;
}
// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
// Driver program to test above functions
int main()
{
int arr[] = {445, 438, 338, 363, 398, 590, 324, 296};
int r = 2;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}
In base al valore di r mi fornisce le combinazioni possibili. Per esempio con r = 2 fornisce:
445 438
445 338
445 363
445 398
445 590
445 324
445 296
438 338
438 363
438 398
438 590
438 324
438 296
338 363
338 398
338 590
338 324
338 296
363 398
363 590
363 324
363 296
398 590
398 324
398 296
590 324
590 296
324 296
Il passo successivo che vorrei ottenere è la somma di questi numeri inserendo anche le combinazioni con r crescenti.