如何在c编程中选择具有指定信用的最佳标记?

时间:2013-12-23 14:41:04

标签: c arrays algorithm sorting

我想制作一个程序,以便从formula = (mark*(credits corresponds to it))/total credits获得以下标记和学分的加权平均值(best(highest) 120 credits module)(学分对应于模块):

module[12]={48, 77, 46, 82, 85, 43, 49, 73, 65, 48, 47, 51}

credits[12]={60, 20, 20, 20, 10, 20, 10, 10, 10, 20, 20, 10}

我所做的是对数组进行冒泡排序,以便通过递减方式对数组进行排序,以了解哪些标记更高,如下所示:

module[12]={85, 82, 77, 73, 65, 51, 49, 48, 48, 47, 46, 43}

credits[12]={10, 20, 20, 10, 10, 10, 10, 60, 20, 20, 20, 20}

然后我需要从排序数组中选择最好的120学分模块,以便加权平均值最大,但后来我不知道从哪里开始。 =(

有人帮帮我!非常感谢!

编辑: 我试图自己编写代码,并最终得到以下代码,它可以工作,但对于一些特殊情况,它停止工作=(

float credits=0, result=0;
n=0;

struct{
    float credits;
    float result;
    float n;
    float addpoint;
}point;

while (credits < 120){
        credits+=credits[n];
        result+=(result[n]*credits[n]);
        n++; 
    }

    if (credits != 120){
        credits -= credits[n-1];
        result -= (result[n-1]*credits[n-1]);

        point.credits = credits;
        point.result = result;
        point.n = (n-1)-1;

        point.addpoint = n;

again:  while (credits < 120){
            credits+=credits[n];
            result+=(result[n]*credits[n]);
            n++;
        }

        if (credits != 120){
            point.credits -= credits[point.n-1];
            point.result -= result[point.n-1]*credits[point.n-1];
            point.n--;

            credits = point.credits;
            result = point.result;
            n = point.addpoint-1;

            goto again;
        }
    }

编辑:

解决。通过应用glpk

使用背包问题代码/整数线性编程

3 个答案:

答案 0 :(得分:1)

这是一个整数线性规划问题。你想找到一个向量[x1 x2 x3 ... x12],其中每个x都是0或1,这样sum(x [i] * cred [i])= 120和sum(x [i] * cred [i] * marks [i])最大化。

关于如何解决这些问题的研究很多,但是现有的解决方案已经准备好让你使用和使用它们将为你节省大量的时间来自己编写求解器。

这是glpk的模块文件,一个自由线性编程和整数线性编程求解器。您可以通过安装glpk运行它,将其保存到文件,然后运行glpsol -m <filename>

set I := 1..12;
var x{I} binary;
param cred{I};
param marks{I};

maximize s:
    sum{i in I}(x[i] * cred[i] * marks[i]);
s.t. totalcreds: sum{i in I}(x[i] * cred[i]) = 120;

solve;
printf {i in I} "%d: %d %d %d\n", i, x[i], cred[i], marks[i];

data;

param cred := 1 60, 2 20, 3 20, 4 20, 5 10, 6 20, 7 10, 8 10, 9 10, 10 20, 11 20, 12 10;
param marks := 1 48, 2 77, 3 46, 4 82, 5 85, 6 43, 7 49, 8 73, 9 65, 10 48, 11 47, 12 51;

end;

输出是这样的:

1: 0 60 48
2: 1 20 77
3: 0 20 46
4: 1 20 82
5: 1 10 85
6: 0 20 43
7: 0 10 49
8: 1 10 73
9: 1 10 65
10: 1 20 48
11: 1 20 47
12: 1 10 51

也就是说,你应该选择课程2,4,5,5,9,10,11,12(即:他们的课程'1')。

答案 1 :(得分:1)

另一种方法,对于像你这样的小例子来说很实用,就是使用动态编程。如果您使用前k个科目,并希望学分加起来,可以建立一个“最佳分数”表。代码有点乱,因为C不会让动态大小的2d数组特别容易,但这是一个解决方案。可能你的教授期待着这些方面的东西。

一个小小的注释,我将所有学分(以及120的目标学分)除以10,因为公共因子是多余的,但是没有它的代码工作得很好(它只会使用更多的内存和时间)

#include <stdio.h>
#include <stdlib.h>

int max(int a, int b) {
    return a > b ? a : b;
}

#define GET(t, i, j, n) ((t)[(i) * (n + 1) + j])

// optimize_marks takes arrays creds and marks (both of length n),
// and finds a subset I of 0..(n-1) that maximizes
// sum(i in I)creds[i]*marks[i], such that sum(i in I)creds[i] = total.
void optimize_marks(size_t n, int *creds, int *marks, int total) {
    // tbl[k * (total + 1) + T] stores the optimal score using only the
    // first k subjects for a total credit score of T.
    // tbl[n * (total + 1) + total] will be the final result.
    // A score of -1 means that the result is impossible.
    int *tbl = malloc((n + 1) * (total + 1) * sizeof(int));
    for (int i = 0; i <= n; i++) {
        for (int T = 0; T <= total; T++) {
            if (i == 0) {
                // With 0 subjects, the best score is 0 if 0 credits are
                // required. If more than 0 credits are required, the result
                // is impossible.
                GET(tbl, i, T, total) = -(T > 0);
                continue;
            }
            // One way to get T credits with the first i subjects is to
            // get T credits with the first (i-1) subjects.
            GET(tbl, i, T, total) = GET(tbl, i - 1, T, total);
            // The other way is to use the marks for the i'th subject
            // and get the rest of the credits with the first (i-1) subjects.
            // We have to check that it's possible to use the first (i-1) subjects
            // to get the remainder of the credits.
            if (T >= creds[i-1] && GET(tbl, i - 1, T - creds[i-1], total) >= 0) {
                // Pick the best of using and not using the i'th subject.
                GET(tbl, i, T, total) = max(
                    GET(tbl, i, T, total),
                    GET(tbl, i - 1, T - creds[i-1], total) + marks[i-1] * creds[i-1]);
            }
        }
    }
    int T = total;
    for (int i = n; i > 0; i--) {
        if (GET(tbl, i - 1, T, total) < GET(tbl, i, T, total)) {
            printf("%d %d %d\n", i, creds[i-1], marks[i-1]);
            T -= creds[i-1];
        }
    }
}

int main(int argc, char *argv[]) {
    int creds[] = {6, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1};
    int marks[] = {48, 77, 46, 82, 85, 43, 49, 73, 65, 48, 47, 51};
    optimize_marks(12, creds, marks, 12);
    return 0;
}

该程序将解决方案作为ILP程序:

12 1 51
11 2 47
10 2 48
9 1 65
8 1 73
5 1 85
4 2 82
2 2 77

答案 2 :(得分:0)

你应该首先隔离所有sum []元素的组合,其总和等于120并在计算所有均值之前存储它们。 它看起来像那样:

// test for all possible combinaisons :
 // [0] + [1] + [2]..., [1] + [0] + [2]...
// starting point on the array : n
 while(n < 12){
    // if the sum isnt equal to 120 and 
   // we havent covered all the array yet
   if(temp < 120 && i < 12){
         // if i is different from the starting point
         if(i < 11 && i != n){
           i++;
           temp += credits[i];
           // store i
         }
    }
    if(temp >120 && i < 11){
            temp -= credits[i];
            // remove i
            i++;
     }
    if(temp == 120){
           // starting point ++
           n++;
     }
 }

这不是最佳选择,但它可以发挥作用。