zoukankan      html  css  js  c++  java
  • hdoj

    Problem 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.
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace::std;
    
    int map[15],t,n;
    int map_[15],k=0;
    int sum;
    
    void dfs1(int l,int ans)
    {
        if(sum > t)
        {
            return ;
        }
        if( sum == t)
        {
            for(int i =0;i<ans;i++)
            {
                if(i)
                {
                    printf("+%d",map_[i]);
                }
                else
                {
                    printf("%d",map_[i]);
                }
            }
            cout<<endl;
            k++;
        }
        for(int i = l;i<n;i++)
        {
            if(map[i] == map[i-1] && i>l)       //这一个语句是重点
                continue;
            sum = sum + map[i];
            map_[ans] = map[i];
            dfs1(i+1,ans+1);
            sum =sum - map[i];
            map_[ans] = 0;
        }
    }
    int main()
    {
        while(scanf("%d %d",&t,&n) && n != 0)
        {
            memset(map,0,sizeof(map));         //重置地图和标记
            memset(map_,0,sizeof(map_));
            k=0;
            for(int i=0; i<n;i++)
            {
                scanf("%d",&map[i]);
            }
            printf("Sums of %d:
    ",t);
            dfs1(0,0);
            if(k == 0)
            {
                printf("NONE
    ");
            }
        }
    
        return 0;
    }
  • 相关阅读:
    Mosquitto搭建Android推送服务(一)MQTT简介
    Quartz定时任务简单实例
    Oracle基础知识(一)、简介与安装
    [2013-08-01]window.open
    C#中DataTable与泛型集合互转(支持泛型集合中对象包含枚举)
    C#代码安装Windows服务(控制台应用集成Windows服务)
    Node+Socket实现聊天室
    web前端架构
    Laravel-admin form 表单是增加或者修改
    Laravel-admin 消息提醒、播放音频、点击跳转
  • 原文地址:https://www.cnblogs.com/hdyss/p/10808776.html
Copyright © 2011-2022 走看看