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 ...
随机推荐
- Virtualbox+UbuntuServer+Xshell搭建Linux开发环境
需求背景 嵌入式开发环境以编译服务器(Linux系统)为核心, 开发人员较多使用Windows系统, 通过若干工具, 可以实现开发人员使用Windows的开发工具,编写代码,然后在Linux系统上编译 ...
- H2的MVStore
翻译自http://www.h2database.com/html/mvstore.html 转载请著名出处,及译者信息. 第一次翻译,诸多不妥请谅解,谢谢. 概述 MVStore是一个持久化的.日志 ...
- MVC 自定义过滤器/特性来实现登录授权及验证
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 最近在做自学MVC,遇到的问题很多,索性一点点总结 ...
- 常用SQL操作(MySQL或PostgreSQL)与相关数据库概念
本文对常用数据库操作及相关基本概念进行总结:MySQL和PostgreSQL对SQL的支持有所不同,大部分SQL操作还是一样的. 选择要用的数据库(MySQL):use database_name; ...
- C++Builder组件
1.TOpenDialog: Title属性:用于获取或设置对话框标题,如果么偶有给该属性赋值,则系统将使用默认值标题:“打开” .InitialDir属性:用于获取或设置文件对话框显示的初始目录.如 ...
- ASPNET服务端控件练习(一个机试题)
简单记录: 模糊查询的select语句的拼写 public List<Model.Student> GetWhereStudent(string name, string sub, str ...
- Appium-android环境的快速搭建
1.Android的要求: Android的sdk api>=16 appium支持android在linux,osx(mac),window平台 2.Appium的安装与启动: 安装: (1) ...
- 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版
<zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...
- 【fedora】设置中文为默认语言
安装Fedora时,界面语言是英语,手动安装简体中文语言包: 1.打开终端,执行yum install system-config-language命令来安装语言套件: 2.然后执行system-co ...
- wc之上传图片
wc上传图片:以上传头像为例 1.找到views/default/account/settings/profile.tpl.htm中 init_avatar_uploader($('#avatar_u ...