다음 점수와 크레딧 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를 적용하여 배낭 문제 코드 / 정수 선형 프로그래밍 사용
다른 방법은 여러분이 가지고있는 것과 같은 작은 예제에 실용적인 방법으로 동적 프로그래밍을 사용하는 것입니다. "처음 k 과목을 사용하고 학점을 T까지 합산하려면 최적 점수"테이블을 만들 수 있습니다. 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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다