zoukankan      html  css  js  c++  java
  • Sum It Up 广搜

    Sum It Up
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general. 
     

    Input

    The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12(inclusive), and x1,...,xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions. 
     

    Output

    For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distince; the same sum connot appear twice. 
     

    Sample Input

    4 6 4 3 2 2 1 1
    5 3 2 1 1
    400 12 50 50 50 50 50 50 25 25 25 25 25 25 0 0
     

    Sample Output

    Sums of 4:
    4
    3+1
    2+2
    2+1+1
    Sums of 5:
    NONE
    Sums of 400:
    50+50+50+50+50+50+25+25+25+25
    50+50+50+50+50+25+25+25+25+25+25
     
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <map>
     4 #include <string.h>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 using namespace std;
     9 int t,a[20],ans[20],n,nu;
    10 void dfs(int s,int as,int sum)
    11 {
    12     int i;
    13     if(sum==t)
    14     {
    15         nu++;
    16         for(i=0;i<as;i++)
    17         if(i!=as-1)
    18         printf("%d+",ans[i]);
    19         else printf("%d
    ",ans[i]);
    20         return ;
    21     }
    22     if(s>=n)return ;
    23     for(i=s;i<n;i++)
    24     {
    25         ans[as]=a[i];
    26         dfs(i+1,as+1,sum+a[i]);
    27         while(i+1<n&&a[i]==a[i+1])i++;
    28     }
    29 }
    30 int main()
    31 {
    32     //freopen("in.txt","r",stdin);
    33     int i,j;
    34     while(scanf("%d%d",&t,&n),t||n)
    35     {
    36         nu=0;
    37         for(i=0;i<n;i++)
    38         scanf("%d",&a[i]);
    39         cout<<"Sums of "<<t<<":"<<endl;
    40         dfs(0,0,0);
    41         if(nu==0)
    42         cout<<"NONE"<<endl;
    43     }
    44 }
    View Code
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <map>
     4 #include <string.h>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 using namespace std;
     9 int t,n,a[20][2],ans[20][2],an,ok;
    10 void dfs(int s,int anss,int sum)
    11 {
    12     //cout<<s<<" "<<anss<<" "<<sum<<endl;
    13     int i,j;
    14     if(sum>t)return;
    15     if(sum==t)
    16     {
    17         for(i=0; i<anss; i++)
    18         {
    19             for(j=0; j<ans[i][1]; j++)
    20             if(i==anss-1&&j==ans[i][1]-1)
    21             printf("%d
    ",ans[i][0]);
    22             else
    23                 printf("%d+",ans[i][0]);
    24         }
    25         ok++;
    26         return ;
    27     }
    28     if(s>=an)return;
    29     for(j=a[s][1]; j>=0; j--)
    30     {
    31         ans[anss][0]=a[s][0];
    32         ans[anss][1]=j;
    33         if(j)
    34             dfs(s+1,anss+1,sum+a[s][0]*j);
    35         else dfs(s+1,anss,sum+a[s][0]*j);
    36     }
    37     return ;
    38 }
    39 int main()
    40 {
    41     //freopen("in.txt","r",stdin);
    42     int i,j,x;
    43     while(scanf("%d%d",&t,&n),t||n)
    44     {
    45         ok=0;
    46         an=0;
    47         for(i=0; i<n; i++)
    48         {
    49             scanf("%d",&x);
    50             if(an==0)
    51             {
    52                 a[an][0]=x,a[an++][1]=1;
    53             }
    54             else
    55             {
    56                 if(x==a[an-1][0])
    57                     a[an-1][1]++;
    58                 else  a[an][0]=x,a[an++][1]=1;
    59             }
    60         }
    61         cout<<"Sums of "<<t<<":"<<endl;
    62         dfs(0,0,0);
    63         if(!ok)
    64         cout<<"NONE"<<endl;
    65     }
    66 }
    View Code
     
  • 相关阅读:
    安装docker (centos7.6)
    idea docker docker-compose发布springboot站点到tomcat
    tomcat启动慢的解决办法
    skywalking6.3.0安装(centos7.6)
    RocketMQ集群安装 2主2从 console
    RocketMQ官方启动脚本不支持jdk11的修改
    python在windows上创建虚拟环境
    JVM之Java运行时数据区(线程共享区)
    Dijkstra算法和Floyd算法
    JVM之Java运行时数据区(线程隔离区)
  • 原文地址:https://www.cnblogs.com/ERKE/p/3844658.html
Copyright © 2011-2022 走看看