简体   繁体   English

了解分数背包,数组和指针

[英]Understanding fractional knapsack, arrays and pointers

Just started learning C programming and decided to take a class in algorithmic Toolbox on Coursera. 刚开始学习C编程,并决定在Coursera的算法工具箱上一堂课。 One of the challenges is writing a code using fractional knapsack, maximizing the value of loot and a pseudo code was given to help in coding the solution. 挑战之一是使用分数背包来编写代码,最大化战利品的价值,并提供了伪代码来帮助编码解决方案。 Below are the pseudo code and the code I wrote for the pseudo code. 以下是伪代码和我为伪代码编写的代码。

#include<stdio.h>

int min(int a, int b)
{
    if (a < b)
        return a;
    else
        return b;
}

int knapsack(int value[], int weight[])
{
    int capacity = 100;
    int val = 0;
    int array[] = { 0 };
    for (int i = 1; i < capacity; i++)
    {
        if (capacity == 0)
        {
            return val;
        }
        for (int i = 1; i < capacity; i++)
        {
            if (weight[i] > 0 && (value[i] / weight[i]))
            {
                int a = min(weight[i], capacity);
                val = val + a * (value[i] / weight[i]);
                weight[i] = weight[i] - a;
                array[i] = array[i] + a;
                capacity = capacity - a;
            }
        }
    }
    return val;
}

int main()
{
    int value[100];
    int weight[100];
    scanf("%d", &value[100]);
    scanf("%d", &weight[100]);
    printf("%d", knapsack(value[100], weight[100]));
    return 0;
}

pseudo code 伪码

Knapsack(W, w1,v1,......wn,vn)
A <-- [0,0,], V <-- 0;
repeat n times:
if W =  0:
return (V,A)
select i with Wi > 0 and max vi/wi
a <-- min(wi, W)
V <-- V + a(vi/wi)
wi <-- wi - a, A[i] <-- A[i] + a, W <-- W - a
return (V, A)

I am getting errors when I compile such as "passing argument 1 of 'knapsack' makes pointer from integer without a cast [-Wint-conversion]" 我在编译时遇到错误,例如“传递'knapsack'的参数1使指针从整数开始而没有强制转换[-Wint-conversion]“

printf("%d", knapsack(value[100],weight[100]));

"expected int * but argument is of type 'int'" “预期为int *,但参数的类型为'int'”

int knapsack(int value[], int weight[])

I also want to know if it is a good practice to declare int value[], int weight[] in the function int knapsack argument and also more explanation in using arrays and pointers in situations like this. 我还想知道在int knapsack函数参数中声明int value[], int weight[]以及在这种情况下使用数组和指针的更多解释是一种好习惯。

int knapsack(int value[], int weight[])

The above statement gives the compiler the information about HOW the function should be called (type of arguments) and WHAT the function will return. 上面的语句使编译器如何在功能应该叫信息(类型参数), 什么函数将返回。

  1. It says the function knapsack will return an integer value (the 1st int). 它说函数背包将返回一个整数值(第一个整数)。

  2. Its name is knapsack (case-sensitive). 名称为背包(区分大小写)。

  3. It expects two arguments: an integer array (named value) and an integer array (named weight). 它需要两个参数:一个整数数组(命名值)和一个整数数组(命名为weight)。

Points 1, 2 and 3 together make up the signature of a function. 点1、2和3共同构成一个函数的签名。

To call the function you have to pass 2 integer arrays as its arguments. 要调用该函数,您必须传递2个整数数组作为其参数。

The mistake : value[100] corresponds to an INTEGER ENTRY in the array and not the array itself. 错误value[100]对应于数组中的INTEGER ENTRY ,而不是数组本身。

To pass the array you should pass the array name as its argument, which your function expects. 要传递数组,您应该传递函数名所期望的数组名称作为其参数。

Call the function like this: knapsack(value, weight) 像这样调用函数: knapsack(value, weight)

value corresponds the array value and weight corresponds to the array weight 对应于数组值权重对应于数组权重

Also, passing value[100] corresponds to some garbage value that is not within the array bounds as you can only access elements ranging from value[0] to value[99] (0-based indexing). 另外,传递value[100]对应于一些不在数组范围内的垃圾值,因为您只能访问从value[0]value[99] (基于0的索引)的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM