Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test pull request #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions pull_reqeust_test/ddiyoung/packing_knapsack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Packing knapsack question
*
* Copyright (C) 2016, Taeung Song <[email protected]>
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define MAX_INPUT 16

struct knapsack {
unsigned int limited_wgt;
unsigned int max_price;
};

struct jewelry {
unsigned int wgt;
unsigned int price;
};

void pack_knapsack(struct knapsack *knapsack_list,
struct jewelry **usable_jewels)
{
/* Case by case pack knapsack following maximum
* price per limited weight.
*/
}

int get_maxprice(struct knapsack *knapsack_list, struct jewelry *jewelry)
{
/* Get maximum price based on limited weight */
}

bool get_values_from(char *input, int *val1, int *val2)
{
char *ptr = input;

*val1 = atoi(strsep(&ptr, " "));
if (ptr == NULL) {
printf("Error: Need a whitespace\n");
return false;
}

*val2 = atoi(ptr);

if (*val1 == 0 || *val2 == 0) {
printf("Error: Input have to contain only numbers\n");
return false;
}

return true;
}

int main(int argc, const char **argv)
{
char input[MAX_INPUT];
char *ptr = input;
int nr_jewels, limited_wgt;
struct jewelry *jewels;
struct knapsack *knapsack_list;

fgets(input, sizeof(input), stdin);
if (get_values_from(input, &nr_jewels, &limited_wgt) == false)
return -1;

jewels = malloc(sizeof(struct jewelry) * nr_jewels);
for (int i = 0; i < nr_jewels; i++) {
bool ret;

fgets(input, sizeof(input), stdin);
ret = get_values_from(input, &jewels[i].wgt,
&jewels[i].price);
if (ret == false)
return -1;
}

knapsack_list = malloc(sizeof(struct knapsack) * limited_wgt);

for (int i = 0; i < nr_jewels; i++) {
struct jewelry **usable_jewels = malloc(sizeof(struct jewelry *)
* (i + 1));

for (int j = 0; j <= i; j++)
usable_jewels[i] = &jewels[j];
pack_knapsack(knapsack_list, usable_jewels);
free(usable_jewels);
}

printf("%d\n", knapsack_list[limited_wgt-1].max_price);
free(jewels);
free(knapsack_list);
return 0;
}