【DFS练习】Pku1950 Dessert-C++】的更多相关文章

-->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…
/* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再算一遍值! */ 1 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; ]; ]={'+', '-', '.'}; int…
题目链接 http://poj.org/problem?id=1950 题意 输入一个整数n(3<=n<=15),将1,2,..n顺序排列,在数字中间插入'+','-','.',这样会产生一个算数表达式,如果表达式的值为0,则输出该表达式.如果表达式为0的个数大于20,则只输出前20个. 思路 采用DFS搜索解决.由于数字的位置是固定的,所以在符号的位置上不断地尝试三种符号即可,若最终的表达式结果为0且是前20个表达式,则输出该表达式. 代码 #include <iostream>…
这道题和这道题很类似. 这里还是说一下坑点,因为前一道题'等式'的加数只有9个,但是这道题目最大到了15,所以在选择不加符号的时候需要判断是用100去乘还是用10去乘就可以了. 基本代码稍微把相关的9改成n就可以了.具体思路去看一下前面那篇博客哈,下面就直接贴代码了. #include<bits/stdc++.h> using namespace std; int n,cnt=0; int num[16];//0.1+2- bool flag; void dfs(int sum) { if(s…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27092   Accepted: 8033 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple t…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been…
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been car…
Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36442   Accepted: 10894 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has bee…