约瑟夫环:递归算法
摘要:假设下标从0开始,0,1,2 .. m-1共m个人,从1开始报数,报到k则此人从环出退出,问最后剩下的一个人的编号是多少?现在假设m=100 1 2 3 4 5 6 7 8 9 k=3第一个人出列后的序列为:0 1 3 4 5 6 7 8 9即:3 4 5 6 7 8 9 0 1(*)我们把该式转化为:0 1 2 3 4 5 6 7 8 (**)则你会发现: ((**)+3)%10则转化为(*)式了也就是说,我们求出9个人中第9次出环的编号,最后进行上面的转换就能得到10个人第10次出环的编号了设f(m,k,i)为m个人的环,报数为k,第i个人出环的编号,则f(10,3,10)是我们要的结果.
阅读全文
posted @
2011-10-30 20:44
yangyh
阅读(28199)
推荐(5)
二叉树遍历(非递归)
摘要://中序遍历,非递归void inTravel(NodePtr root){ if(root==NULL) return ;stack<NodePtr> st;NodePtr left = root;NodePtr lastVisit;while(st.size()>0||left){ //左子树非空则走到底 while(left){ st.push(left); left=left->pLeft; } if(!st.empty()){ NodePtr top = st.top(); visitNode(0,top); st.pop(); if(top->pRig
阅读全文
posted @
2011-10-09 16:44
yangyh
阅读(252)
推荐(0)
TSP:旅行者问题(动态规划+递归)
摘要:// TSP.cpp : Defines the entry point for the console application.//#include "stdafx.h"typedef struct _node_st{ bool inPath ; int nextIndex; _node_st():inPath(false),nextIndex(-1){ }}Node,*NodePtr;#define MAX 6int dis[MAX][MAX]={ 0, 10, 20, 30, 40, 50, 12, 0 ,18, 30, 25, 21, 2...
阅读全文
posted @
2011-10-08 21:02
yangyh
阅读(5485)
推荐(0)
笔试题:二叉树按层遍历&添加兄弟指针&求LCA&排序二叉树的查找
摘要:1.二叉树按层遍历2.二叉树添加兄弟指针3.在二叉树中查找LCA(最近公共祖先)3.在排序二叉树中找到大于N且最接近N的数// PrintByLevel.cpp : Defines the entry point for the console application.// Author : yangyh#include "stdafx.h"#include <iostream>#include <queue>using namespace std;typedef struct _node_st{ int value; _node_st* pLeft
阅读全文
posted @
2011-09-29 21:03
yangyh
阅读(1242)
推荐(1)
未知总数的抽样如何保证相同概率
摘要:1.从未知的大量数中随机抽取一个数 解决:假设这个数为choice, 则对于第i个数,以1/i的概率替换它 choice = 1 以1/2的概率替换choice=2 以1/3的概率替换choice=3 … 直到数据遍历完毕 2.从未知的大量数中抽取出k个 解决:以k/i的概率让第i个数加入抽样集合,如果抽样集合已满,随机替换掉一个 假如要抽取10个 则1,2,3……10为初始化...
阅读全文
posted @
2011-09-18 20:47
yangyh
阅读(475)
推荐(0)
生成若干个随机数的和的概率分布
摘要:百度实习生招聘笔试题: 请编写函数foo(int x, int y, int n) 计算:随机生成x个大小为[1,y]的正整数,它们的和为n的概率是多少? 解 :用一个数组count[m][k]表示共m次生成随机数,和为k的次数,初始化count[1][1],count[1][2]..count[1][y]=1 对于第i次产生随机数:和为i*min到i*max的和数j的概率(或称 次数)需要更改...
阅读全文
posted @
2011-08-17 16:58
yangyh
阅读(769)
推荐(0)
求最大字段和
摘要:int m=-INF; int tmp = 0; for( j=0;j<N;j++){ scanf("%d",&input[j]); if(tmp>0)tmp+=input[j]; else tmp=input[j]; if(tmp>m){ F[j]=m=tmp; }else{ F[j]=m; } }
阅读全文
posted @
2011-08-12 14:55
yangyh
阅读(180)
推荐(0)
递归系列:分解质因数
摘要:void toPrimMulti(int n, int m) { if (n >= m) { while (n % m != 0) m++; n /= m; printf("%d ", m); toPrimMulti(n, m); }}toPrimMulti(21, 2);
阅读全文
posted @
2011-08-10 01:33
yangyh
阅读(537)
推荐(0)
组合问题(递归)
摘要://============================================================================// Name : ForJob.cpp// Author : yangyh// Version :// Copyright : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================//从M个字符中选出N
阅读全文
posted @
2011-08-10 01:04
yangyh
阅读(471)
推荐(0)
2011百度之星 复赛(下)第一题
摘要:// BDFS2_1.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;#define MAX 1000005#define min(x,y) ((x)<(y)?(x):(y))#define INF 99999...
阅读全文
posted @
2011-06-19 22:44
yangyh
阅读(424)
推荐(0)
2011百度之星 个人源码
摘要:A场:求最大分离度 这道题做错了,3分 #include <iostream>#include <algorithm>using namespace std;int cmp( int arg1, int arg2 ) { return arg1 > arg2;}int main(int argc, char* argv[]){ //freopen("i://input.txt","r",stdin...
阅读全文
posted @
2011-06-17 00:18
yangyh
阅读(512)
推荐(0)
全排列/递归/八皇后问题
摘要:分别交换第1个元素与第2、3、4、5..len-1个元素 void permutation(char *str,int begin,int len){ if(len==1){ printf("%s\n",str); count++; return; } char tmp = str[begin]; for(int i=0;i<len;i++){ char tmp2=str[begin+i]; s...
阅读全文
posted @
2011-06-16 23:40
yangyh
阅读(328)
推荐(0)
堆排序
摘要:不稳定排序 比较次数与初始序列有关(基本有序时可以减少堆的调整次数) // HeapSort.cpp : Defines the entry point for the console application.//#include "stdafx.h"void swap(int *a,int *b);void printArr(int arr[],int length){ for(in...
阅读全文
posted @
2011-06-15 23:32
yangyh
阅读(255)
推荐(0)
POJ 3624 /背包问题
摘要:背包大小为m,对于每一个物品,枚举背包大小j(m到w),对于此物品(重量w、价值v),如果dp[j-w]+v大于dp[j],则采用该方案(dp[j]=dp[j-w]+v) 最后dp[实际背包大小m]就是最大价值 #include <iostream>using namespace std;#define SIZE 12881int dp[SIZE];int main(int argc, char...
阅读全文
posted @
2011-06-12 14:16
yangyh
阅读(344)
推荐(0)
POJ 1328 贪心、快排/求最少雷达数量
摘要:对于每个岛屿,计算出在区间[A,B]内必须有一个雷达,对这些区间的左坐标进行排序 第一个岛屿的右坐标为right, 初始雷达数量为1,对于第二个岛屿,如果它的右坐标比right小,则说明区间2包含了区间1,不用增加雷达数量,并设置right = 岛屿2的右坐标; 如果它的右坐标比right大,又岛屿2的左坐标大于right,则雷达数量+1,并设置right = 岛屿2的右坐标. #include ...
阅读全文
posted @
2011-06-10 21:51
yangyh
阅读(530)
推荐(0)
poj 2299/归并排序、逆序数
摘要:归并排序 合并两个有序数组 p,mid 1 3 5 6 7 12 q 2 4 6 8 9 11 找到3比2大,则3之后的都比2大,所以2的逆序数就有mid-p+1个 // 2299.cpp : Defines the entry point for the console application.//#include <iostream>using namespace std;#define MA...
阅读全文
posted @
2011-06-08 22:27
yangyh
阅读(249)
推荐(0)
POJ 2362/DFS:判断是否能形成正方形
摘要:1011简单改一下就OK了 #include <iostream>#include <algorithm>using namespace std;#define MAX 64int sticks[MAX];bool used[MAX];int stickNum,plen,n;bool compare(int a, int b){ return a > b; }//从beginIndex号开始匹配,...
阅读全文
posted @
2011-06-06 21:52
yangyh
阅读(440)
推荐(0)
POJ 1011/DFS:子集划分
摘要:问题描述:原有若干sticks,每支stick被分割成若干支,现在要恢复一下。找出这样的子集,使得每个子集的和(plen)相等,并且最小 算法:排序,遍历所有可能plen 剪枝:排序后, a[0] a[1] a[2].....a[n-1],如果想从a[i]开始(不包括i)匹配一个数v,如果i+1,i+2......n-1都匹配不成功了,则肯定i+2,i+3....n-1也匹配不成功了,所以从TL...
阅读全文
posted @
2011-06-06 18:48
yangyh
阅读(420)
推荐(0)
POJ 1753/BFS:翻转棋
摘要:黑白棋翻转 4X4的棋盘有2^16种状态,如果状态值为0或65535则算法结束 第一层:初始状态 第二层:初始状态翻0、初始状态翻1、初始状态翻……初始状态翻15 第三层: …………………………. 如果该状态值未访问过,则入队列 #include <iostream>#define MAX_STATE 65535#define ALL_BLACK 65535#define ALL_WHITE 0#...
阅读全文
posted @
2011-06-05 21:12
yangyh
阅读(385)
推荐(0)
POJ 2255/递归:前序中序求后序
摘要:Sample InputDBACEGF ABCDEFG已知二叉树的前序遍历与后序遍历求后序遍历 算法:前序遍历时,第一位为根:D;找到中序中的D,则前面的ABC在左子树,右边的EFG在右子树,则后序为为左子树+右子树+根:solve(BAC,ABC)+solve(EGF,EFG)+D#include <iostream>#include <string>using namespace std;s...
阅读全文
posted @
2011-06-05 20:17
yangyh
阅读(374)
推荐(0)