1 条题解

  • 0
    @ 2025-2-14 21:20:39

    C :

    #include <stdio.h>
    #include <stdlib.h>
    #define INF 0x7fffffff
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char *argv[]) {
    	int n;
    	while(scanf("%d",&n)!=EOF){
    		
    		while(n--){
    			int s,g;
    			scanf("%d%d",&s,&g);
    			g-=s;
    			int w[550],v[550];
    			int number;
    			scanf("%d",&number);
    			int i,j;
    			int dp[10050]; 
    			for(i=0;i<number;i++){
    				scanf("%d%d",&v[i],&w[i]);
    			}
    			for(i=0;i<=g;i++){
    				dp[i]=INF;
    			}
    			dp[0]=0;
    			for(i=0;i<number;i++){
    				for(j=w[i];j<=g;j++){
    					if(dp[j-w[i]]!=INF)
    					dp[j]=dp[j]<(dp[j-w[i]]+v[i])?dp[j]:(dp[j-w[i]]+v[i]);
    				}
    			}
    			if(dp[g]!=INF)
    			printf("The minimum amount of money in the piggy-bank is %d.\n",dp[g]);
    			else
    			printf("This is impossible.\n");
    		}
    	}
    	return 0;
    }
    

    C++ :

    #include <stdio.h>
    int c[555][10001];
    void run()
    {
    	int i,j,a[666],b[666],e,f,n,k;
    	scanf("%d%d%d",&e,&f,&n);
    	f-=e;
    	for(i=1;i<=n;i++)
    		scanf("%d%d",&a[i],&b[i]);
    	for(i=1;i<=n;i++)
    		for(j=0;j<=f;j++)
    			c[i][j]=1000000000;
    	c[1][0]=0;
    	for(i=1;i<=n;i++)
    		for(j=0;j<=f;j++)
    		{
    			k=1000000000;
    			if(j>=b[i])
    				k=c[i][j-b[i]]+a[i];
    			if(i>1)
    				if(k>c[i-1][j])
    					k=c[i-1][j];
    			if(c[i][j]>k)
    				c[i][j]=k;
    		}
    	if(c[n][f]==1000000000)
    		printf("This is impossible.\n");
    	else
    		printf("The minimum amount of money in the piggy-bank is %d.\n",c[n][f]);
    }
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t!=0)
    	{
    		t--;
    		run();
    	}
    	return 0;
    }
    

    Python :

    #coding=utf-8
    INF = 1000000
    def Matrix(rows,cols):
        #matrix = [[0 for col in range(cols)] for row in range(rows)]
        matrix = [0 for col in range(cols)]
        return matrix
    """
    def printf(lst,rows,cols):
        for i in range(rows):
            for j in range(cols):
                print lst[i][j],
            print
        print
    """
    
    t = input()
    while t:
        [x,y]=raw_input().split()
        C=int(y)-int(x)
        lines=input()
        n = lines
        weight = []
        price = []
        while lines:
            [a,b]=raw_input().split()
            weight.append(int(b))
            price.append(int(a))
            lines -= 1
            if lines==0:break
        mat = Matrix(n+1,C+1)
        #init row-0 in matrix
        mat[0]=0
        for j in range(1,C+1):mat[j]=INF
        #compute the matrix
        for i in range(1,n+1):
            for j in range(weight[i-1],C+1):
                mat[j]=min(mat[j],mat[j-weight[i-1]]+price[i-1])
        if mat[C]==INF:print "This is impossible."
        else: print "The minimum amount of money in the piggy-bank is %d." % mat[C]
        t -= 1
        if t==0:break
    
    
    • 1

    信息

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