2017年上海金马五校程序设计竞赛 C : Count the Number 深搜
来源:互联网 发布:cp linux 直接覆盖 编辑:程序博客网 时间:2024/06/06 09:26
Description
Given n numbers, your task is to insert '+' or '-' in front of each number to construct expressions. Note that the position of numbers can be also changed.
You can calculate a result for each expression. Please count the number of distinct results and output it.
Input
There are several cases.
For each test case, the first line contains an integer n (1 ≤ n ≤ 20), and the second line contains n integers a1,a2, ... ,an(-1,000,000,000 ≤ ai ≤ 1,000,000,000).
Output
For each test case, output one line with the number of distinct results.
Sample Input
21 231 3 5
Sample Output
48
其实就是一个递归啦,别想太多啦,迷迷糊糊就能过
#include <iostream>#include <set>using namespace std;int a[25];set <int> q;void getsum(int sum,int n,int m){if(n == m){q.insert(sum);return;}getsum(sum+a[n],n+1,m);getsum(sum-a[n],n+1,m);}int main(){int n;while(scanf("%d",&n)!=EOF){for(int i = 0;i < n; i++){scanf("%d",&a[i]);}q.clear();getsum(0,0,n);int u = q.size();printf("%d\n",u);}}
阅读全文
0 0
- 2017年上海金马五校程序设计竞赛 C : Count the Number 深搜
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number
- 【2017年上海金马五校程序设计竞赛】C : Count the Number 【DFS】
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number
- 2017年上海金马五校程序设计竞赛 Problem C : Count the Number
- 2017上海金马五校程序设计竞赛 C :Count the Number
- 2017年上海金马五校程序设计竞赛:Find Palindrome
- 2017上海金马五校程序设计竞赛部分题解
- 2017上海金马五校程序设计竞赛 A :STEED Cards
- 2017上海金马五校程序设计竞赛 B:Sailing
- 2017上海金马五校程序设计竞赛 E:Find Palindrome
- 2017上海金马五校程序设计竞赛 K:Treasure Map
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem H : DHU Club Festival
- 2017年上海金马五校程序设计竞赛(网上资格赛)部分题目题解
- ubuntu 16.04 设置位wifi热点 方法(手机可链接)亲测可用
- Where is the Marble?
- 指针笔记
- Binary Tree的相关练习 104,111
- 机房重构之组合查询
- 2017年上海金马五校程序设计竞赛 C : Count the Number 深搜
- 有序广播
- 与Weex2.x的一次亲密接触(Web和IOS平台上共用一套代码)
- UVa 136 丑数
- POJ 1715 Hexadecimal Numbers 笔记
- HDU-4597-区间DP-(博弈)
- 洛谷3377【【模板】左偏树】--左偏树&并查集
- js添加class属性
- bzoj1533: [POI2005]Lot-A Journey to Mars