?? ex3_a.c
字號:
/* DIT/06/c1/0087 Exercise 3_a*/#include<stdio.h>int KnapRecursive(int i,int k,int n,int p[5],int w[5]);int MaxValue(int x,int y);int main(){ int n,k,p[5],w[5]; int j,temp; printf("Please Enter the Maximum Capacity:\n"); scanf("%d",&k); printf("Please Enter the number of Items:\n"); scanf("%d",&n); printf("Please Enter the Weights of the Items:\n"); for(j=0;j<n;j++) { scanf("%d",&w[j]); } printf("Please Enter the Profits of the Items:\n"); for(j=0;j<n;j++) { scanf("%d",&p[j]); } temp =KnapRecursive(0,k,n,p,w); printf("Maximum Profit is %d\n",temp);}int KnapRecursive(int i,int k,int n,int p[],int w[]){ int x,y; if((i==n-1) && (w[i]>k)) return 0; if((i==n-1) && (w[i]<=k)) return p[n-1]; if((i<n-1) && (w[i]>k)) return KnapRecursive(i+1,k,n,p,w); if((i<n-1) && (w[i]<=k)) { x = KnapRecursive(i+1,k,n,p,w); y =p[i]+KnapRecursive(i+1,k-w[i],n,p,w); return MaxValue(x,y); } }int MaxValue(int x,int y){ if(x<y) return y; else return x;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -