zoj 1204 Additive equations
ACCEPT
//#include "stdafx.h"
#include"stdio.h"
#include"iostream"
#include<set>
using namespace std;
int tem[33] = { 0 };//存放加数 以便输出
int i = 0;
int flag = 0;
int c_sum=0;
void dfs(set<int> &c_iset, set<int>::iterator c_iterator, int de, int num)//num为加数个数
{
set<int>::iterator set_iter1;
if (num == 2)//两个加数时
{ for (set_iter1 = c_iterator, set_iter1++; *set_iter1 <*c_iset.rbegin(); set_iter1++)//set_iter1++,指向下一个、这里实现的是第二个加数
{
//c_sum = 0;
tem[de] = *set_iter1;
c_sum += tem[de];
// for (int k = 0; tem[k] != 0; k++)
// {
// c_sum = c_sum + tem[k];
// }
if (c_iset.find(c_sum) != c_iset.end())//找到sum 成功可输出
{
flag = 1;//成功标志
printf("%d", tem[0]);
for (int j = 1; tem[j] != 0; j++)
{
printf("+%d", tem[j]);
}
printf("=%d\n", c_sum); }
if (c_sum >= *c_iset.rbegin())//结束,不必再继续递归下去
{
tem[de] = 0;//重置为0 ,已经输出过了
return;
}
c_sum -= tem[de];
//dfs(c_iset, set_iter1, de + 1);//到下一个数 }
}
else
{
for (set_iter1 = c_iterator, set_iter1++; *set_iter1 <*c_iset.rbegin(); set_iter1++)//set_iter1++,指向下一个、这里实现的是第二个加数
{
tem[de] = *set_iter1;//减少一个数
c_sum = 0;
for (int k = 0; tem[k] != 0; k++)
{
c_sum = c_sum + tem[k];
}
if (c_sum > *c_iset.rbegin())//超过最后一个没有必要再进行下去了
{
tem[de] = 0;
return;
} num--;
dfs(c_iset, set_iter1, de + 1, num);
num++;
}
}
tem[de] = 0;//重置为0
return;
}; int main()
{
int n, m, l ;
set<int>iset;
set<int>::iterator set_iter;//
scanf("%d", &n);//多少组
while (n-- > 0)
{
scanf("%d", &m);//每组个数
iset.clear();//清空
for (int j = 0; j < m; j++)
{
scanf("%d", &l);
iset.insert(l); }
flag = 0; for (int j = 2; j < m; j++)//加数个数从2到m-1个
{
for (set_iter = iset.begin(); *set_iter <* iset.rbegin(); set_iter++)//固定的第一个加数
{ c_sum = *set_iter;
tem[0] = c_sum;
i = 1;
dfs(iset, set_iter, i,j);
}
}
if (flag == 0)
{
printf("Can't find any equations.\n");
}
printf("\n");
}
return 0;
}
每次accept都 是历经千辛万苦!!隔了要两个月了吧set 作为参数要引用&!
注意还原,调试非常好用!
虽然做出来一点意思了,但是不符合要求。。。顺序这个,是要考我们按层次递归么?太蠢了!
但是第一次按自己的意思写出了想要的递归,我舍不得删掉,要留着
t等我写出答案,再来。。。
#include "stdafx.h"
#include"stdio.h"
#include"iostream"
#include<set>
using namespace std;
int tem[33] = { 0 };//存放加数 以便输出
int i = 0;
int flag = 0;
void dfs(set<int> &c_iset, set<int>::iterator c_iterator, int de)
{
set<int>::iterator set_iter1;
for (set_iter1 = c_iterator, set_iter1++; set_iter1 != c_iset.end(); set_iter1++)//set_iter1++,指向下一个
{
int c_sum = 0;
tem[de] = *set_iter1;
for (int k = 0; tem[k] != 0; k++)
{
c_sum = c_sum + tem[k];
}
if (c_iset.find(c_sum) != c_iset.end())//找到sum 成功可输出
{
flag = 1;//成功标志
printf("%d", tem[0]);
for (int j = 1; tem[j] != 0; j++)
{
printf("+%d", tem[j]);
}
printf("=%d\n", c_sum);
}
if (c_sum >= *c_iset.rbegin())//结束,不必再继续递归下去
{
tem[de] = 0;//重置为0
return;
}
dfs(c_iset, set_iter1, de + 1); }
tem[de] = 0;//重置为0
return;
}; int main()
{
int n, m, l, sum;
set<int>iset;
set<int>::iterator set_iter;//
scanf("%d", &n);
while (n-->0)
{
scanf("%d", &m);
iset.clear();//清空
while (m > 0)
{
scanf("%d", &l);
iset.insert(l);
m--;
}
flag = 0;
for (set_iter = iset.begin(); set_iter != iset.end(); set_iter++)
{ sum = *set_iter;
tem[0] = sum;
i = 1;
dfs(iset, set_iter, i);
}
if (flag==0)
{
cout << "Can't find any equations." << endl;
}
}
return 0;
}
zoj 1204 Additive equations的更多相关文章
- ZOJ1204——Additive equations(DFS)
Additive equations Description We all understand that an integer set is a collection of distinct int ...
- ZOJ 1204 一个集合能组成多少个等式
Additive equations Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- Additive equations--zoj
Additive equations Time Limit: 10 Seconds Memory Limit: 32768 KB We all understand that an inte ...
- ZOJ ACM 1204 (JAVA)
毕业好几年了,对算法还是比較有兴趣,所以想又一次開始做ACM题.俺做题比較任意,一般先挑通过率高的题来做. 第1204题,详细描写叙述请參考,ZOJ ACM 1204 1)难度分析 这个题目,基本的难 ...
- 重拾ZOJ 一周解题
ZOJ 2734 Exchange Cards 题目大意: 给定一个值N,以及一堆卡片,每种卡片有一个值value和数量number.求使用任意张卡片组成N的方式. 例如N = 10 ,cards(1 ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
随机推荐
- JDBC批量处理
转载自http://www.cnblogs.com/xdp-gacl/p/3983253.html 在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而 ...
- 一SERVLET (1)
转载自http://www.cnblogs.com/xdp-gacl/p/3760336.html 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公 ...
- WWDC2016 Session笔记 - Xcode 8 Auto Layout新特性
目录 1.Incrementally Adopting Auto Layout 2.Design and Runtime Constraints 3.NSGridView 4.Layout Feedb ...
- Cheatsheet: 2013 11.12 ~ 11.30
Mobile Xcode 5 Essentials Android vs. iOS Development: Fight! Using MVC to Understand ASP.NET, iOS, ...
- MySql使用show processlist查看正在执行的Sql语句
今天上班例行的查看了下服务器的运行状况,发现服务器特卡,是mysqld这个进程占用CPU到了99%导致的. 比较好奇是那个程序在使用mysql导致cpu这么高的,通过show processlist命 ...
- 最大熵模型(Maximum Etropy)—— 熵,条件熵,联合熵,相对熵,互信息及其关系,最大熵模型。。
引入1:随机变量函数的分布 给定X的概率密度函数为fX(x), 若Y = aX, a是某正实数,求Y得概率密度函数fY(y). 解:令X的累积概率为FX(x), Y的累积概率为FY(y). 则 FY( ...
- c#扩展方法的理解(一:初识)
扩展方法是静态方法,是类的一部分,但是实际上没有放在类的源代码中. 扩展方法所在的类也必须被声明为static C#只支持扩展方法,不支持扩展属性.扩展事件等. 扩展方法的第一个参数是要扩展的类型,放 ...
- JAVA排序--[选择排序]
package com.array; public class Sort_Select { /** * 项目名称:选择排序 ; * 项目要求:用JAVA对数组进行排序,并运用选择排序算法; * 作者: ...
- Yii2文件上传
首先在app\controllers下建立TestController.php,内容为如下代码: <?php namespace app\controllers; use Yii; use yi ...
- php提示:Call to undefined function curl_init
我要利用curl函数进行数据采集时发现提示Call to undefined function curl_init错误了,后来从官网了解到因为curl默认不是php开启的函数,我们需要手工打开哦,下面 ...