2017上海金马五校程序设计竞赛 C :Count the Number
来源:互联网 发布:上交所网络投票流程 编辑:程序博客网 时间:2024/06/05 16:55
Time Limit: 3 s
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
21 231 3 5
Sample Output
48
本题题意:
这题说的是第一行给你一个n,接下来一行给你n个数,可以在这n 个数前边加“+”或者“-”,计算这些数的和,输出有几种不同的和。
解题思路:
因为数据比较小,直接暴力深搜就可以。
代码:
#include <iostream>#include <algorithm>#include <map>#include <cstdio>using namespace std;int a[25];map <int,int> M;int n,ans;void dfs(int sum,int i){ if(i==n+1) { if(M[sum]==0)///如果没有出现过 { M[sum]=1; ans++; } return; } dfs(sum+a[i],i+1); dfs(sum-a[i],i+1);}int main(){ while(~scanf("%d",&n)) { for(int i=0; i<n; i++) scanf("%d",&a[i]); M.clear(); ans=0; dfs(0,0); printf("%d\n",ans); } return 0;}
Output
阅读全文
3 0
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number
- 2017年上海金马五校程序设计竞赛 C : Count the Number 深搜
- 【2017年上海金马五校程序设计竞赛】C : Count the Number 【DFS】
- 2017上海金马五校程序设计竞赛 C :Count the Number
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number
- 2017年上海金马五校程序设计竞赛 Problem C : Count the Number
- 2017上海金马五校程序设计竞赛部分题解
- 2017上海金马五校程序设计竞赛 A :STEED Cards
- 2017上海金马五校程序设计竞赛 B:Sailing
- 2017上海金马五校程序设计竞赛 E:Find Palindrome
- 2017上海金马五校程序设计竞赛 K:Treasure Map
- 2017年上海金马五校程序设计竞赛:Find Palindrome
- 上海金马五校程序设计竞赛 Problem B : Sailing
- 2017 -金马五校程序设计竞赛-A
- 2017-金马五校程序设计竞赛-I
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛) Problem J : Raising Bacteria
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- Git小白入门常用命令
- FileUtils如何跨平台查找文件
- String相加,竟然可以用单引号 '',并且单引号内是双引号,不用转义
- spark任务在运行时报错
- 单例模式的7种写法
- 2017上海金马五校程序设计竞赛 C :Count the Number
- mount命令+nfs挂载失败原因
- 二分查找算法的实现
- opencv K临近算法(knearest)
- 解决SqlPlus前台程序出现中文乱码的问题
- react组件创建方式
- check and Unchecked Exception
- 山水知音--目前可以进行的步骤和待思考步骤
- [洛谷3047]USACO12FEB附近的牛