SCP6
SCP6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
double weight;
double profit;
double profit_density;
} item;
static double profit_bound(const item *items, size_t n, double capacity, double current_weight, double
current_profit, unsigned int level) {
remaining_capacity -= items[lvl].weight;
bound += items[lvl].profit;
lvl++;
}
if (lvl < n) {
return bound;
static void knapsack_recursive(const item *items, size_t n, double capacity, unsigned int
*current_knapsack, double *current_weight,
if (level == n) {
*max_profit = *current_profit;
return;
*current_weight += items[level].weight;
*current_profit += items[level].profit;
current_knapsack[items[level].id] = 1;
*current_weight -= items[level].weight;
*current_profit -= items[level].profit;
current_knapsack[items[level].id] = 0;
}
if (profit_bound(items, n, capacity, *current_weight, *current_profit, level + 1) > *max_profit) {
double knapsack(const double *weights, const double *profits, size_t n, double capacity, unsigned int
**max_knapsack) {
unsigned int i;
free(items);
free(current_knapsack);
free(*max_knapsack);
*max_knapsack = NULL;
return 0;
items[i].id = i;
items[i].weight = weights[i];
items[i].profit = profits[i];
free(items);
free(current_knapsack);
return max_profit;
int main(void) {
unsigned int i;
printf("Knapsack contains:\n");
if (max_knapsack[i] == 1) {
printf("Item %u with weight %.2f and profit %.2f\n", i, weights[i], profits[i]);
free(max_knapsack);
return 0;
void RecursiveExTest(int k, int nc, int pc, int rest, int n, int a, int b, int p[], int q, int X[]) {
if (k <= n) {
X[k - 1] = 1;
X[k - 1] = 0;
} else {
printf("\n");
}
void ExamTest() {
int n, a, b, p[MAX_QUESTIONS], q;
int X[MAX_QUESTIONS];
do {
scanf("%d", &n);
int sumP = 0;
scanf("%d", &p[i]);
sumP += p[i];
} while (!(n >= b && b >= a && sumP >= q && q >= p));
int main() {
ExamTest();
return 0;
int row;
int col;
} Queen;
if (queen[i].row == queen[currentQueen].row ||
queen[i].col == queen[currentQueen].col ||
return 1;
return 0;
int count = 0;
int i = 0;
while (i >= 0) {
queen[i].row++;
queen[i].row = -1;
i--;
} else {
while (isUnderAttack(queen, i)) {
queen[i].row++;
queen[i].row = -1;
i--;
break;
if (i == boardSize - 1) {
count++;
queen[i].row = -1;
i--;
} else {
i++;
return count;
int main() {
Queen queens[boardSize];
queens[i].row = -1;
queens[i].col = i;
return 0;