【POJ - 1950】Dessert(dfs)
-->Dessert
Descriptions:
给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”。输出和值等于零的所有的运算情况及次数num,如果num大于20,则只输出前20中情况。运算符‘.’相当于连接符,例如:11.22 <-> 1122.
Sample Input
7
Sample Output
1 + 2 - 3 + 4 - 5 - 6 + 7
1 + 2 - 3 - 4 + 5 + 6 - 7
1 - 2 + 3 + 4 - 5 + 6 - 7
1 - 2 - 3 - 4 - 5 + 6 + 7
1 - 2 . 3 + 4 + 5 + 6 + 7
1 - 2 . 3 - 4 . 5 + 6 . 7
6
题目链接
https://vjudge.net/problem/POJ-1950
用dfs深搜,如果和值sum=0,则num++;注意‘.’的处理,需要用一个pre记录上一个数字,如果遇到‘.’则回溯到第一个不为‘.’的符号,注意不管怎么样都会多加或多减一个pre;
必须用scanf,prinf,不然超时
加上ios_base::sync_with_stdio(0); cin.tie(0);都没用 为什么啊 大佬留言告诉我一下呗
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 30
using namespace std;
int n;
int cnt;
char op[Maxn];//符号
//第x为数,当前式子总和,前一个数
void dfs(int x,int ans,int pre)
{
if(x==n)
{
if(ans==)
{
cnt++;
if(cnt<=)//保证只输出前20组的答案
{
for(int i=; i<n; i++)//注意输出格式 WA了无数遍
printf("%d %c ",i,op[i]);
printf("%d\n",n);
}
}
return;
}
else
{
int now;
op[x]='+';//第x个符号
dfs(x+,ans+x+,x+);
op[x]='-';
dfs(x+,ans-(x+),x+);
op[x]='.';
if(x+>)//如果x==9,则其后一位x+1=10,9.10 <-> 910;
now=pre*+x+;
else
now=pre*+x+;
int j=x-;
while(op[j]=='.'&&j>=)//找到在符号'.'前面所遍历过中第一个不为'.'的符号;
j--;
if(op[j]=='+')
dfs(x+,ans-pre+now,now);//前面多加了一个pre,减回来;
else
dfs(x+,ans+pre-now,now);//前面多减了一个pre,加回来;
}
return;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)//必须用scanf,prinf,不然超时
{
cnt=;////计算符合条件的个数
op[]='+';//第一个数之前的符号默认为+
dfs(,,);
printf("%d\n",cnt);
}
}
【POJ - 1950】Dessert(dfs)的更多相关文章
- 【POJ - 2078】Matrix(dfs)
-->Matrix Descriptions: 输入一个n×n的矩阵,可以对矩阵的每行进行任意次的循环右移操作,行的每一次右移后,计算矩阵中每一列的和的最大值,输出这些最大值中的最小值. Sam ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 【POJ - 3050】Hopscotch (dfs+回溯)
-->Hopscotch 这接写中文了 Descriptions: 奶牛们以一种独特的方式玩孩子们的跳房子游戏. 奶牛们创造了一个5x5的格子 他们熟练地跳上其中的一个格子,可以前后左右地跳(不 ...
- 【POJ - 1190】生日蛋糕 (dfs+剪枝)
Descriptions: 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为 ...
- 【POJ - 3040】Allowance(贪心)
Allowance 原文是English,这里就放Chinese了 Descriptions: 作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1 ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- 【POJ - 3104 】Drying(二分)
Drying 直接上中文 Descriptions 每件衣服都有一定单位水分,在不使用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,但如果使用了烘干机则每分钟流失K个单位水分,但是遗憾是只有1台 ...
- 【POJ - 1862】Stripies (贪心)
Stripies 直接上中文了 Descriptions 我们的化学生物学家发明了一种新的叫stripies非常神奇的生命.该stripies是透明的无定形变形虫似的生物,生活在果冻状的营养培养基平板 ...
- 【POJ - 2431】Expedition(优先队列)
Expedition 直接中文 Descriptions 一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队.作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱.卡车现在每运行一个单位的距离 ...
随机推荐
- CF776D The Door Problem[2-SAT]
翻译 对于一扇门,如果是关的,那么他必须使用其中一个开关开开来,如果是开的,要么使用两个开关,要么啥都不做.这样,每扇门恰好对应两种状态,要选一个. 考虑用2-SAT模型解决.连边的话是对于一个机关, ...
- java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
扩展阅读:https://blog.csdn.net/kimylrong/article/details/50353161
- Linux(Ubuntu)安装ssh服务
在终端(Ctrl + Alt + T )输入 $ps -e | grep ssh 看到 “ ssh-agent ” 和 “sshd” ,表示没有安装服务,或没有开机启动 1.安装SSH 输入:sudo ...
- ACM-ICPC 2016 大连赛区现场赛 K. Guess the number && HDU 5981(思维+DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5981 题意:A在[L, R]之间随机选取一个数X,之后B来猜这个数,如果猜的数比X小,那么A就告诉B猜 ...
- 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers
题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...
- [Luogu] 计算系数
https://www.luogu.org/problemnew/show/P1313#sub Answer = a ^ n * b ^ m * C(k, min(n, m)) 这里用费马小定理求逆 ...
- 【luogu2668斗地主】模拟
题目描述: 输入格式: 输出格式: 输入样例: 1: 1 8 7 4 8 4 9 1 10 4 11 1 5 1 1 4 1 1 2: 1 17 12 3 4 3 2 3 5 4 10 2 3 3 1 ...
- Love to be loved by you & Just one last dance
http://baike.baidu.com/link?url=wOnBuPncIH5b5oWc0ZREXCU8x6XPYqlZazTLarTjE8eOpdtpv57YMeB_kgXQq4BcCeh2 ...
- jQuery属性操作之DOM属性操作
DOM属性操作是对DOM元素的属性进行读取.设置和移除操作, 比如prop(). removeProp(). 1. prop() 1.1 使用prop()获取返回值 prop() 用于返回属性值时, ...
- 【java设计模式】-05建造者模式
建造者模式 简述 建造者模式,是将一个复杂对象的创建和它的表示分离开来,这就使得同样的构建构成可以有不同的表示. 建造者模式是一步步构建一个复杂的对象,允许用户只需要指定复杂对象的类型和必要的内容就可 ...