七夕快乐~~~zjr 

Dessert

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6397   Accepted: 2357

Description

FJ has a new rule about the cows lining up for dinner. Not only must the N (3 <= N <= 15) cows line up for dinner in order, but they must place a napkin between each pair of cows with a "+", "-", or "." on it. In order to earn their dessert, the cow numbers and the napkins must form a numerical expression that evaluates to 0. The napkin with a "." enables the cows to build bigger numbers. Consider this equation for seven cows:

      1 - 2 . 3 - 4 . 5 + 6 . 7

This means 1-23-45+67, which evaluates to 0. You job is to assist the cows in getting dessert. (Note: "... 10 . 11 ...") will use the number 1011 in its calculation.)

Input

One line with a single integer, N

Output

One line of output for each of the first 20 possible expressions -- then a line with a single integer that is the total number of possible answers. Each expression line has the general format of number, space, napkin, space, number, space, napkin, etc. etc. The output order is lexicographic, with "+" coming before "-" coming before ".". If fewer than 20 expressions can be formed, print all of the expressions.

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

Source

USACO 2002 February

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=101;
char str[maxn];
int  n,ans,k;
/** sum : 已求得的表达式的值* pre : 当前位置pos的前一位置的值* pos : 当前处理的数字,pos-1为符号插入的位置
*/
void dfs(int sum,int pre,int pos)
{if(pos==n+1){if(sum==0){ans++;if(ans<=20){for(int i=1;i<n;i++){printf("%d %c ",i,str[i]);}printf("%d\n",n);}}return;}str[pos-1]='+';dfs(sum+pos,pos,pos+1);str[pos-1]='-';dfs(sum-pos,-pos,pos+1);str[pos-1]='.';if(pos>=10)k=100;else k=10;if(pre<0)dfs(sum-pre+pre*k-pos,pre*k-pos,pos+1);else if(pre>0)dfs(sum-pre+pre*k+pos,pre*k+pos,pos+1);}
int main()
{scanf("%d",&n);ans=0;dfs(1,1,2);printf("%d\n",ans);return 0;
}

Dessert(dfs)相关推荐

  1. poj1950 Dessert(DFS)

    题目链接 http://poj.org/problem?id=1950 题意 输入一个整数n(3<=n<=15),将1,2,..n顺序排列,在数字中间插入'+','-','.',这样会产生 ...

  2. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)

    一.图的基本介绍 为什么要有图 前面我们学了线性表和树 线性表局限于一个直接前驱和一个直接后继的关系 树也只能有一个直接前驱也就是父节点 当我们需要表示多对多的关系时, 这里我们就用到了图. 图的举例 ...

  3. 【 MATLAB 】离散傅里叶级数(DFS)及 IDFS 的 MATLAB 实现

    有关离散傅里叶级数(DFS)我之前也写过一些博文,例如:离散周期信号的傅里叶级数(DFS) 这里我再次给出标准公式. 分析式: 其中: 综合式: 这里我必须先声明,关于分析式和综合式前面那个系数1/N ...

  4. 部署分布式文件系统(DFS)

    部署分布式文件系统(DFS) 使用 DFS 命名空间,可以将位于不同服务器上的共享文件夹组合到一个或多个逻辑结构的命名空间.每个命名空间作为具有一系列子文件夹的单个共享文件夹显示给用户.但是,命名空间 ...

  5. Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)

    对算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)用Java实现其中的伪代码算法,案例也采用算法导论中的图. import java.util.ArrayList; import java ...

  6. 广度优先搜索(BFS)与深度优先搜索(DFS)

    一.广度优先搜索(BFS) 1.二叉树代码 # 实现一个二叉树 class TreeNode:def __init__(self, x):self.val = xself.left = Nonesel ...

  7. 7.9模拟赛T1图的遍历(dfs)

    图的遍历(dfs) [题目描述] 对于一个有向图G来说,我们存在一个经典的遍历算法,就是DFS (深度优先搜索遍历).将G以1号点为起点进行DFS后,我们可以 得到G的一棵DFS遍历树T.就此,我们可 ...

  8. 7.6 T1 深度优先搜索(dfs)

    深度优先搜索(dfs) [题目描述] sol:50pts随便写写,就是大众分了,直接n2dpOK,100分要找点规律,需要数学头脑 官方题解 //#include <bits/stdc++.h& ...

  9. 二叉树的深度优先遍历(DFS)与广度优先遍历(BFS)

    二叉树的深度优先遍历(DFS)与广度优先遍历(BFS) 深度优先遍历:从根节点出发,沿着左子树方向进行纵向遍历,直到找到叶子节点为止.然后回溯到前一个节点,进行右子树节点的遍历,直到遍历完所有可达节点 ...

最新文章

  1. setcellvalue 格式_POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】
  2. 适配器模式coding
  3. golang 反射_golang原理篇- nil:接口类型和值类型的区别
  4. Python(八) 函数、模块
  5. bootstrap轮播图 原点变为方块_JS实现无缝切换轮播图(自动+手动)
  6. 团队项目——201181120
  7. java方法_Java方法
  8. redis 配置文件翻译
  9. 首席架构师眼里的架构本质
  10. Eclipse/NSight: methond could not resolved
  11. 网络信息安全攻防实验室之基础关
  12. IPC之消息队列(Message Queue)
  13. 浩方 VS 真三国无双 全图
  14. 人生若只如初见-云计算时代
  15. php弹出式搜索,使用PHP进行Spotlight搜索
  16. 根据经纬度查询两点距离(sql)
  17. Linux命令之top命令查看服务器CPU与内存占用
  18. JS解决因循环绑定click事件失效
  19. 扩展Euclidean算法求乘法逆原理详解与算法实现
  20. Linux ~ 系统管理。

热门文章

  1. Mac用Pycharm安装mediapipe报错ERROR: Could not find a version that satisfies the requirement mediapipe
  2. PostgreSQL问题解决--连接数过多
  3. Python30 网络编程通讯协议,1.学习网络编程的目的 2.什么是互联网 3.c/s结构 4.通讯基本要素 5.OSI模型...
  4. Python socket和前端html
  5. Python+Opencv身份证号码区域提取及识别!
  6. 英语中的分数 带分数 小数怎么读
  7. 最爽摸鱼听音乐——使用VS Code解锁网易云灰色歌曲(VSC Netease Music+UnblockNeteaseMusic)
  8. Autojs获取GPS定位信息
  9. 如何用计算机计算幅度,电脑计算器计算css布局
  10. CSDN第九次竞赛题解与总结