The partial sum problem(dfs)

The partial sum problem

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

输入

There are multiple test cases.
Each test case contains three lines.The first line is an integer N(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8).

输出

If Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.

样例输入

4
1 2 4 7
13
4
1 2 4 7
15

样例输出

Of course,I can!
Sorry,I can't!

来源

原创

上传者

TC_李远航

描述

One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K. 

题意:给你n个数,以及一个s,问能不能从这n个数中选出几个数是的它们的和等于s。若可以输出 Of course,I can!  否则输出 Sorry,I can't!  

思路:n的范围是20,20个数取或不取那么时间复杂度是2^20,大约是一百万,应该是可以过的。但是坑爹的是非要加上一个剪枝才能过,但我觉得那个剪枝是错的。具体看代码注释

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<map>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define LL long long
const int N=20;
int n,a[N],s,flag;
void dfs(int k,int sum)
{
    //那我们就说一说这个剪枝,当总和sum大于s时就直接返回不再搜下去了。
    //但题目已经很明确的说了给的数组有负数的情况,可能当前sum的值大于s
    //但没有加的数组值中可能存在负数使得他们相等,因此我认为这里的剪枝不合理,后台数据应该是只有只都是正的样例
    if(sum>s) return ;//加上这个才能过,否则超时
    if(sum==s)
    {
        flag=1;
        return ;
    }
    if(flag||k==n) return ;//flag为真则说明已经找到答案,直接返回即可,k=n搜到最后一个数,直接返回即可
    //对于当前这个数取或不取
    dfs(k+1,sum);//不加上这个数
    dfs(k+1,sum+a[k]);//加上这个数
}
int main()
{
    while(~scanf("%d",&n))
    {
        flag=0;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        scanf("%d",&s);
        dfs(0,0);
        if(flag) printf("Of course,I can!\n");
        else printf("Sorry,I can't!\n");
    }
}

 

### 参数说明 `std::partial_sum` 是 C++ 标准库中的一个函数,用于计算指定范围内的部分和,并将结果存储在另一个范围中。其函数定义如下: ```cpp template<class InputIt, class OutputIt> OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first); ``` - `first` 和 `last`:表示输入序列的起始和结束位置的迭代器。`first` 指向要计算部分和的序列的起始位置,而 `last` 指向序列的结束位置(不包含该位置的元素)[^1]。 - `d_first`:表示输出序列的起始位置的迭代器,存储部分和的结果。`partial_sum` 会将计算得到的部分和依次存储到以 `d_first` 为起始位置的输出序列中[^1]。 ### 使用方法 `std::partial_sum` 函数会计算从 `first` 到 `last` 的范围内的部分和,并将结果存储到从 `d_first` 开始的范围中。第一个元素直接拷贝到输出序列中,然后依次计算每个元素的累积和[^1]。 以下是一个示例,展示了如何使用 `std::partial_sum` 函数: ```cpp #include <iostream> #include <vector> #include <numeric> // 包含 partial_sum 函数 int main() { std::vector<int> nums = {1, 2, 3, 4, 5}; std::vector<int> result(nums.size()); // 用于存储结果 // 计算部分和并存储到 result 中 std::partial_sum(nums.begin(), nums.end(), result.begin()); // 输出结果 std::cout << "Partial sum: "; for (int num : result) { std::cout << num << " "; } std::cout << std::endl; return 0; } ``` 在这个示例中,`std::partial_sum` 函数将 `nums` 中的元素累积相加,并将结果存储到 `result` 中。然后,输出了计算得到的部分和[^1]。 ### 自定义操作 除了默认的加法操作,`std::partial_sum` 还允许传入一个自定义的二元操作函数或函数对象,用于替代默认的加法操作。其函数定义如下: ```cpp template<class InputIt, class OutputIt, class BinaryOperation> OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first, BinaryOperation op); ``` - `op`:一个二元操作函数或函数对象,用于定义部分和的计算方式。例如,可以传入一个自定义的函数 `op` 来进行乘法、减法等操作[^3]。 以下是一个示例,展示了如何使用自定义的二元操作函数: ```cpp #include <iostream> #include <vector> #include <numeric> // 包含 partial_sum 函数 // 自定义二元操作函数 int multiply(int a, int b) { return a * b; } int main() { std::vector<int> nums = {1, 2, 3, 4, 5}; std::vector<int> result(nums.size()); // 用于存储结果 // 使用自定义的二元操作函数计算部分和 std::partial_sum(nums.begin(), nums.end(), result.begin(), multiply); // 输出结果 std::cout << "Custom partial sum: "; for (int num : result) { std::cout << num << " "; } std::cout << std::endl; return 0; } ``` 在这个示例中,`std::partial_sum` 使用了自定义的二元操作函数 `multiply` 来计算部分和。输出结果将显示通过乘法操作得到的部分和[^3]。 ### 相关问题 1. 如何在 C++ 中使用 `std::partial_sum` 计算二维数组的部分和? 2. `std::partial_sum` 是否可以用于非数值类型的数据? 3. 如何通过 `std::partial_sum` 实现前缀和的应用? 4. `std::partial_sum` 的时间复杂度是多少? 5. `std::partial_sum` 与 `std::accumulate` 的区别是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值