1 条题解

  • 0
    @ 2021-6-15 1:39:04

    C :

    #include <stdio.h>   
      
    int main()  
    {  
        int  dp[30000];
        int n,m;
        int i,j; 
        while(scanf("%d%d",&n,&m)!=EOF)    
        {  
            for(j=1;j<=m;j++)  
            {  
                int v,p;     
                scanf("%d%d",&v,&p); 
                for(int i=n;i>=v;i--)  
                    if(dp[i]<dp[i-v]+v*p)  
                        dp[i]=dp[i-v]+v*p;  
            }  
            int max=0;  
            for(i=1;i<n;i++)  
                if(dp[i]>max)  
                    max=dp[i];  
            printf("%d\n",max);  
            //memset(dp,0,sizeof(dp));  
        }  
        return 0;  
    }  
    
    

    C++ :

    #include <iostream>
    //状态转移方程:
    //F[i] = max{F[v],F[v-Ci]+Wi*Ci}
    using namespace std;
    int C[30];
    int W[30];
    int F[30000] = {0};
    int main()
    {
    	int N,m;
    	cin >> N >> m;
    	for(int i=1; i<=m; ++i)
    		cin >> C[i] >> W[i];
    	for(int i=1; i<=m; ++i)
    		for(int v=N; v>=C[i]; --v)
    		{
    			if(F[v-C[i]]+W[i]*C[i] > F[v])
    				F[v] = F[v-C[i]]+W[i]*C[i];	
    		}
    	cout << F[N]<< endl;
    	return 0;
    }
    
    

    Pascal :

    var n,m,i,max:longint;a,b:array[0..25]of longint;
    procedure try(t,s,x:longint);
    var i:longint;
    begin
     if t>m then
      begin
       if (s<=n)and(x>max)then max:=x;
       exit;
      end;
     for i:=0 to 1 do
      if i=1 then try(t+1,s+a[t],x+b[t]*a[t])
      else try(t+1,s,x);
    end;
    begin
     read(n,m);
     for i:=1 to m do read(a[i],b[i]);
     max:=-1;
     try(1,0,0);
     writeln(max);
    end.
    
    • 1

    信息

    ID
    247
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者