如何在c语言编程中选择具有指定学分的最佳分数?
我想制作一个程序,从如何在c语言编程中选择具有指定学分的最佳分数?,c,arrays,algorithm,sorting,C,Arrays,Algorithm,Sorting,我想制作一个程序,从最佳(最高)120学分模块获得加权平均值(公式=(分数*(学分对应于它))/总学分),从以下分数和学分(学分对应于模块): 我所做的是对数组进行气泡排序,以便数组按递减方式排序,以知道哪个标记更高,如下所示: 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学分模块
获得加权平均值(公式=(分数*(学分对应于它))/总学分
),从以下分数和学分(学分对应于模块):
我所做的是对数组进行气泡排序,以便数组按递减方式排序,以知道哪个标记更高,如下所示:
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学分模块,这样加权平均值将是最大的,但是我不知道从哪里开始=(
有人帮帮我!非常感谢
编辑:
我试着自己编写代码,最终得到下面的代码,它可以工作,但在某些特殊情况下它停止工作=(
浮动积分=0,结果=0;
n=0;
结构{
浮动信贷;
浮动结果;
浮动n;
浮点数;
}点;
而(学分<120){
学分+=学分[n];
结果+=(结果[n]*学分[n]);
n++;
}
如果(学分!=120){
学分-=学分[n-1];
结果-=(结果[n-1]*学分[n-1]);
积分=积分;
point.result=结果;
点n=(n-1)-1;
point.addpoint=n;
再次:while(学分<120){
学分+=学分[n];
结果+=(结果[n]*学分[n]);
n++;
}
如果(学分!=120){
积分点-=积分点[n-1];
分数结果-=结果[分数n-1]*学分[分数n-1];
点n;
积分=点积分;
结果=点结果;
n=点。添加点-1;
再次转到;
}
}
编辑:
已解决。通过应用glpk,使用背包问题代码/整数线性规划,您可能应该首先隔离总和等于120的credits[]元素的所有组合,并在计算所有均值之前存储它们。 看起来是这样的:
// 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++;
}
}
//测试所有可能的组合:
// [0] + [1] + [2]..., [1] + [0] + [2]...
//数组上的起始点:n
而(n<12){
//如果总和不等于120和
//我们还没有覆盖所有的阵列
如果(温度<120&&i<12){
//如果我与起点不同
如果(i<11&&i!=n){
i++;
临时工+=学分[i];
//商店一
}
}
如果(温度>120&&i<11){
临时-=学分[i];
//移除i
i++;
}
如果(温度==120){
//起点++
n++;
}
}
这是一个整数线性规划问题。你想找到一个向量[x1x2x3…x12],其中每个x都是0或1,这样和(x[i]*cred[i])=120,和(x[i]*cred[i]*marks[i])最大化 关于如何解决这些问题,有大量的研究,但是有现成的解算器可供您使用,使用它们将为您节省大量的时间,而不是自己编写解算器 这是glpk的一个模块文件,一个免费的线性规划和整数线性规划求解器。您可以通过安装glpk来运行它,将其保存到一个文件中,然后运行
glpsol-m
输出如下:
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、8、9、10、11、12(即:课程中有“1”的课程)。另一种方法是使用动态规划,这种方法对于像你所得到的例子这样的小例子是实用的。你可以建立一个“如果你使用前k个科目,并且希望学分加起来等于T,那么最佳分数”的表格.代码有点凌乱,因为C并不能使动态调整2d数组的大小变得特别容易,但这里有一个解决方案。可能你的教授希望这样做 一个小提示,我将所有的积分(以及120的目标积分)除以10,因为公因数是多余的,但是如果没有公因数,代码就可以正常工作(只需要多一点内存和时间)
“最佳”是一个非常模糊的术语,上面可以看到你如何恰当地提出问题。什么语言?C还是C++?(它们是两种非常不同的语言)@ PaulR;这些标签在一起非常危险(特别是当他们提供答案后删除其中的一个)。是的,我想我解释得不够。威尔·伊迪特:我在用c语言。谢谢你指出这一点。我是个新手。你能解释一下为什么要用if(I<11&&I!=n)和if吗(temp>120&&i是的,GLPK的功能是通过C库公开的。您需要转到下载它,并且包含文档。这更像是一种C编程方法,我更喜欢这种方法。谢谢!
// 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++;
}
}
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
#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;
}
12 1 51
11 2 47
10 2 48
9 1 65
8 1 73
5 1 85
4 2 82
2 2 77