入力値から選択する方法
Posted: 2012年6月25日(月) 19:10
容量CのバッグにN種類の商品(各々値段p、容量c)から複数個を選んで詰め込む。
その際詰め込む商品の合計金額が出来るだけ高くなる様に商品を選び、その内容を表示するプログラムを作成せよ。
<実行結果例>
Input Capacity C of the bag : 13
Input the number of kinds of goods : 4
Input the detailed information of each goods.
No. 1 c : 5
No. 1 p : 140
No. 2 c : 3
No. 2 p : 120
No. 3 c : 4
No. 3 p : 130
No. 4 c : 7
No. 4 p : 400
No. p
4 400
2 120
2 120
total 640
以上が実行例です。
現在の私のプログラムは下です。
#include<stdio.h>
int main(void)
{
int a,b,c,d;
int x;
printf("Input Capacity C of the bag : ");
scanf("%d",&a);
printf("Input the number of kinds of goods : ");
scanf("%d",&b);
printf("\n");
printf("Input the detailed information each goods.");
printf("\n");
for(x=1;x<=b;x++)
{
printf("No.%d c:",x);
scanf("%d",&c);
printf("No.%d p:",x);
scanf("%d",&d);
printf("\n");
}
}
以上が現在のプログラムです。
N種類の商品から合計金額が最も多くなるようバッグに詰め込むプログラムがわかりません。
どうやってバッグの大きさに合わせて、金額最大になるようにするのですか?
教えてください。
その際詰め込む商品の合計金額が出来るだけ高くなる様に商品を選び、その内容を表示するプログラムを作成せよ。
<実行結果例>
Input Capacity C of the bag : 13
Input the number of kinds of goods : 4
Input the detailed information of each goods.
No. 1 c : 5
No. 1 p : 140
No. 2 c : 3
No. 2 p : 120
No. 3 c : 4
No. 3 p : 130
No. 4 c : 7
No. 4 p : 400
No. p
4 400
2 120
2 120
total 640
以上が実行例です。
現在の私のプログラムは下です。
#include<stdio.h>
int main(void)
{
int a,b,c,d;
int x;
printf("Input Capacity C of the bag : ");
scanf("%d",&a);
printf("Input the number of kinds of goods : ");
scanf("%d",&b);
printf("\n");
printf("Input the detailed information each goods.");
printf("\n");
for(x=1;x<=b;x++)
{
printf("No.%d c:",x);
scanf("%d",&c);
printf("No.%d p:",x);
scanf("%d",&d);
printf("\n");
}
}
以上が現在のプログラムです。
N種類の商品から合計金額が最も多くなるようバッグに詰め込むプログラムがわかりません。
どうやってバッグの大きさに合わせて、金額最大になるようにするのですか?
教えてください。