【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 一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队.作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱.卡车现在每运行一个单位的距离 ...
随机推荐
- dockerfile-maven plugin自动镜像制作并发布
环境准备:win10+docker 1.打开hyper-v 2.下载最新版本docker:https://store.docker.com/editions/community/docker-ce-d ...
- Neo4j入门博客分享
Neo4j学习参考博客:https://www.cnblogs.com/ljhdo/p/5516793.html Neo4j Cypher查询语言详解 http://www.ttlsa.com/nos ...
- 2、CString与string借助char *互转
CString是MFC中的类,MFC前端界面中获得的字符串是CString类.标准C/C++库函数是不能直接对CString类型进行操作的. string是C++中的类. 安全性 CString &g ...
- C#实现代码生成器
最近在用layui做后台管理,增删改查这些一成不变的东西写起来浪费大量时间,于是做了个简单的代码生成器快速生成 代码生成器的原理其实很简单,都是基于模板实现替换,Razor是个不错的选择(或者Nvel ...
- umei-spider
umei-spider 1 #!/usr/bin/python3 2 3 import requests 4 from bs4 import BeautifulSoup 5 from contextl ...
- c str to float
#include <wchar.h> int main () { wchar_t szOrbits[] = L"365.24 29.53"; wchar_t * pEn ...
- python 系统模块 OS
os.system("系统命令") 调用系统命令 os.system("task kill /f /im 系统的进程") 关闭系统进程 os.listdir( ...
- centos7下面改变亮度
通过改自己的配置文件来改变亮度的方法在我的机器上面是不中用了,我的是台式机,我用的集成显卡,目前我找到的还勉强的方法就是通过安装redshift来改变屏幕的亮度,我就是不喜欢太亮,差点瞎了. 具体是这 ...
- Spring AOP:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException
1 报错 Exception encountered during context initialization - cancelling refresh attempt: org.springfra ...
- nginx 实现高并发和高负载
一.Nginx是如何实现高并发的 service nginx start之后,然后输入#ps -ef|grep nginx,会发现Nginx有一个master进程和若干个worker进程,这些work ...