描述
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的更多相关文章

  1. 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 ...

  2. NYOJ 927 The partial sum problem 【DFS】+【剪枝】

    The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...

  3. NYoj The partial sum problem(简单深搜+优化)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...

  4. The partial sum problem

    算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

随机推荐

  1. hibernate中@Entity和@Table的区别

    Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...

  2. Rman备份的保留策略(retention policy)

    什么是备份的保留策略(retention policy) 保留策略就是指某份备份是否要保留以及保留多久.可以使用configure retention policy命令来创建一个一致的.自动的备份保留 ...

  3. java Double封装类的对象保留两位小数

    double value = 22.365454; DecimalFormat df = new DecimalFormat("##.00");value = Double.par ...

  4. Java基础之一组有用的类——使用正则表达式查找和替换(SearchAndReplace)

    控制台程序. 使用正则表达式执行查找和替换操作,只需要调用Matcher对象的find()方法,就可以调用appendReplacement()方法来替换匹配的子序列.在提供给方法的新StringBu ...

  5. html 鼠标移入标签 显示小手指

    <div style="width: 200px; height: 200px; background: red; cursor: pointer;"></div ...

  6. SQL 面向对象(委托)

    委托:也称为代理,事件也是一种委托:定义在类的最外面 1.定义委托关键字:delegate函数签名:签名和函数保持一致定义委托的时候要根据函数来定义public delegate int First( ...

  7. .net 网站预编译命令

    aspnet_compiler -v /Aspnet  -p "C:\inetpub\wwwroot\a"  C:\inetpub\wwwroot\a2 /Aspnet   iis ...

  8. php session 跨子域和跨服务器解决方式

    Session主要分两部分:    一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在     另一个是标志着Session数据的Session Id,Sess ...

  9. Aspose.cell处理Excel

    (一)从数据库中读取数据写入Excel中 方法1: 步骤:1.建立一个新的项目,引用动态链接库Aspose.dll 2.见下面的原代码 using System;using System.Collec ...

  10. PAT乙级 1004. 成绩排名 (20)

    1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...