ACM题目————The partial sum problem
- 描述
- 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.
- 输入
- 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!
题目大意就是,给你n个数, 再给一个sum,能不能用这n个数,加起来等于sum!
主要难点在减少循环。
//时间超限代码:
//Asimple
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
#define CLS(a) memset(a,0,sizeof (a))
const int maxn = 25;
int n, T, num, cnt, point, line, x, y;
int vis[maxn];//标记数组,标记是否走过
int a[maxn];
bool flag; bool check()
{
for(int i=0; i<n; i++)
if( !vis[i] )
return false ;
return true;
} void DFS(int cnt)
{
if( check() ) return ;
if( cnt == num )
{
flag = true ;
return ;
}
for(int i=0; i<n; i++)
{
if( !vis[i] && cnt + a[i] <= num )
{
vis[i] = 1 ;
DFS(cnt + a[i] ) ;
vis[i] = 0 ;
}
}
} int main()
{
while( cin >> n )
{
cnt = 0 ;
CLS(vis);
flag = false ;
for(int i=0; i<n; i++)
cin >> a[i] ;
cin >> num ;
DFS(cnt);
if( flag ) cout << "Of course,I can!" << endl ;
else cout << "Sorry,I can't!" << endl ;
} return 0;
}减少循环后的代码:
//Asimple
#include <iostream>
using namespace std;
const int maxn = 25;
int n, num, cnt;
int a[maxn];
bool flag; void DFS(int x)
{
if( cnt > num ) return ;
if( cnt == num )
{
flag = true ;
return ;
}
for(int i=x; i<n; i++)
{
cnt += a[i] ;
DFS(i+1);
cnt -= a[i] ;
}
} int main()
{
while( cin >> n )
{
cnt = 0 ;
flag = false ;
for(int i=0; i<n; i++)
cin >> a[i] ;
cin >> num ;
DFS(0);
if( flag ) cout << "Of course,I can!" << endl ;
else cout << "Sorry,I can't!" << endl ;
} return 0;
}
ACM题目————The partial sum problem的更多相关文章
- NYOJ--927--dfs--The partial sum problem
/* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...
- NYOJ 927 The partial sum problem 【DFS】+【剪枝】
The partial sum problem 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- The partial sum problem
算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...
- nyoj 927 The partial sum problem(dfs)
描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...
- 2017-5-14 湘潭市赛 Partial Sum 给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。 0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的 最大贡献和。
Partial Sum Accepted : Submit : Time Limit : MS Memory Limit : KB Partial Sum Bobo has a integer seq ...
- HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏
Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HD2058The sum problem
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
随机推荐
- EBS R12.2快速安装前没有配置Global Inventory报错
EBS R12.2快速安装前没有配置Global Inventory,导致验证时"file systems"这一项没有通过,被标记了"X": (本图其它两个验证 ...
- 转:python webdriver API 之cookie 处理
有时候我们需要验证浏览器中是否存在某个 cookie,因为基于真实的 cookie 的测试是无法通过白盒和集成测试完成的.webdriver 可以读取.添加和删除 cookie 信息.webdrive ...
- CSS自定义弹出框
<script type="text/javascript" language="javascript"> function sAlert(str) ...
- [原创]java WEB学习笔记54:Struts2学习之路--- 编写Struts2 的第一个程序,HelloWord,简述 package ,action,result
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Java基础(6):foreach 方法遍历数组
foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英文字面意思理解 foreach 也就是“ for 每一个”的意思,那 ...
- 夺命雷公狗—angularjs—25—angular内置的方法(高级)
查看版本信息 angular.version console.log(angular.version); 判断是否相等 angular.equals() var str1 = ''; var str2 ...
- 夺命雷公狗---Thinkphp----12之文章的增删改查(图片上传和关联查询)
我们由于表分析的不够完善,所以我们来加多一个tid的字段,到时候主要目的是为了更好的遍历出文章是属于那个分类下的,表如下所示: 那么下一步我们就开始创建一个ArticleController.clas ...
- 【《zw版·Halcon与delphi系列原创教程》Halcon图层与常用绘图函数
[<zw版·Halcon与delphi系列原创教程>Halcon图层与常用绘图函数 Halcon的绘图函数,与传统编程vb.c.delphi语言完全不同, 传统编程语言,甚至cad ...
- OpenStack 之vmware机器迁移到openstack集群
原理 openstack本身是支持使用vmware格式的镜像的,但是是需要我们我们在/etc/nova/nova.conf的配置文件中指定该计算节点使用vmware的驱动 1 2 3 4 5 6 7 ...
- React的一个简单示例
首发:个人博客,更新&纠错&回复 React的核心是定义组件类,组件有三个要素:状态.行为.界面. 1.渲染状态到界面:状态由组件对象的state属性持有,从状态到界面的渲染工作由组件 ...