摘要: *题意: 在1到n之间添加‘+’,‘-’,‘.’三种运算符令最后结果为0,输出前20种添加方法,若不足20种则全 输出,最后输出共有多少种添加方式。(‘.’表示将其两侧的数连成一个数例:1.2.3是123)*思路:dfs,枚举各种情况,找出合适的。*代码:#include#includeusing namespace std;char symbol[16];int num,n;void print(){ int i; for(i=1;i printf("%d %c ",i,symbol[i]); printf("%d\n",i);}void dfs(i. 阅读全文
posted @ 2013-08-23 19:02 Neptunes 阅读(152) 评论(0) 推荐(0) 编辑
摘要: *题意: 相当于在一个数轴上给定农夫的位置n与牛的位置k。假定牛不懂,农夫每次有三种移动方式:n+1.n-1.n*2。问农夫最少移动几步后才能到牛的位置。*思路: 用bfs寻找最短路径。*代码:#include#include#includeint queue[100001];bool visit[100001];int step[100001];using namespace std;void bfs(int n,int k){ memset(visit,0,sizeof(visit)); memset(step,0,sizeof(step)); int i,next,... 阅读全文
posted @ 2013-08-23 19:01 Neptunes 阅读(180) 评论(0) 推荐(0) 编辑
摘要: *题意: 将1-20摆成一个环,要求相邻两数相加是素数。*思路: 回溯*代码:#includeusing namespace std;int a[21];int check1(int c,int b){ for(int i=1;i<=b-1;i++) if(a[i]==c)return 0; return 1;}int check2(int x){ int k; for(k=2;k if(x%k==0)return 0; return 1;}int check3(int j,int i){ if(i<20)return (check2... 阅读全文
posted @ 2013-08-23 18:58 Neptunes 阅读(198) 评论(0) 推荐(0) 编辑