1 条题解

  • 0
    @ 2021-6-14 23:25:20

    C :

    #include<stdio.h>  
    #include<string.h>
    int main()  
    {  
        int dp[2000];  
        int c[2000],num[7];  
        int weight[7]={0, 1, 2, 3, 5, 10, 20};  
    	int i,j,sum=0;  
        memset(dp,0,sizeof(dp));  
        for(i=1;i<=6;i++)  
        {  
           scanf("%d",&num[i]);  
            sum+=num[i]*weight[i];  
        }
    	c[0]=0;
    	for(i=1;i<=6;i++)  
        {  
           for(j=1;j<num[i];num[i]-=j,j<<=1)  
             c[++c[0]]=weight[i]*j;  
           if(num[i])
    		   c[++c[0]]=weight[i]*num[i];  
         }
    	dp[0]=1;
    	for(i=1;i<=c[0];i++)  
    		for(j=sum;j>=c[i];j--)  
    			dp[j]=(dp[j]||dp[j-c[i]]);  
        int ret=0;  
            for(i=1;i<=sum;i++)  
                ret+=dp[i];  
        printf("Total=%d\n",ret);
    	return 0;
    }  
    

    C++ :

    #include <iostream>
    using namespace std;
    int main()
    {
    	int arr[1000]={0},total=0;
    	int a,b,c,d,e,f;
    	cin>>a>>b>>c>>d>>e>>f;
    	//a=1,b=1,c=d=e=f=0;
    	for(int i=0;i<=a;i++)
    		for(int j=0;j<=b;j++)
    			for(int k=0;k<=c;k++)
    				for(int l=0;l<=d;l++)
    					for(int m=0;m<=e;m++)
    						for(int n=0;n<=f;n++)
    						{
    							int sum=i+2*j+3*k+5*l+10*m+20*n;
    							if(!arr[sum])
    							{
    								arr[sum]+=1;
    								total++;
    							}
    						}
    	cout<<"Total="<<total-1<<endl;
    	return 0;
    }
    

    Pascal :

    const a:array[1..6]of longint=(1,2,3,5,10,20); 
    var i,j,k,ans:longint; 
        b,p:array[-1000..1000]of longint; 
    begin
     for i:=1 to 6 do read(b[i]); 
     p[0]:=1; 
     for i:=1 to 6 do
      for j:=1000 downto a[i] do
       for k:=0 to b[i] do
       if p[j-a[i]*k]=1 then p[j]:=1; 
     for i:=1 to 1000 do if p[i]=1 then inc(ans); 
     writeln('Total=',ans); 
    end. 
    

    Java :

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) throws Exception {
    
    		Scanner scanner = new Scanner(System.in);
    		String input = scanner.nextLine();
    		String[] array = input.split(" ");
    		List<Integer> inputList = new ArrayList<Integer>();
    		for (int i = 0; i < array.length; i++) {
    			for (int j = 0; j < Integer.parseInt(array[i]); j++) {
    				if (i == 0) {
    					inputList.add(1);
    				} else if (i == 1) {
    					inputList.add(2);
    				} else if (i == 2) {
    					inputList.add(3);
    				} else if (i == 3) {
    					inputList.add(5);
    				} else if (i == 4) {
    					inputList.add(10);
    				} else if (i == 5) {
    					inputList.add(20);
    				}
    			}
    		}
    		System.out.println("Total=" + fun(inputList).size());
    	}
    
    	public static List<Integer> fun(List<Integer> inputList) {
    		List<Integer> retList = new ArrayList<Integer>();
    		if (inputList.size() == 1) {
    			return inputList;
    		}
    
    		List<Integer> subList = inputList.subList(1, inputList.size());
    		List<Integer> subRetList = fun(subList);
    		retList.addAll(subRetList);
    		for (Integer subRet : subRetList) {
    			if (!retList.contains(inputList.get(0))) {
    				retList.add(inputList.get(0));
    			}
    			if (!retList.contains(subRet + inputList.get(0))) {
    				retList.add(subRet + inputList.get(0));
    			}
    		}
    		return retList;
    	}
    }
    
    
    • 1

    信息

    ID
    184
    时间
    1000ms
    内存
    125MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者