书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

2011年10月18日

感悟Floyd

摘要: 今天做了一道Jump题目,本以为是一道搜索的题目,没想到竟然用Floyd就轻松的解决了。 先来看看这个题目吧! Description There is n pillar, their heights are (A1,A2,A3,…An).you can jump at the top of the pillars. But you will lose abs(a[j]-a[i])*abs(j-i) power when you jump from i-th pillar to j-th pillar. At first you have m power. Can you jump f... 阅读全文

posted @ 2011-10-18 11:22 More study needed. 阅读(886) 评论(0) 推荐(1) 编辑

Floyd算法解决 Jump

摘要: DescriptionThere is n pillar, their heights are (A1,A2,A3,…An).you can jump at the top of the pillars. But you will lose abs(a[j]-a[i])*abs(j-i) power when you jump from i-th pillar to j-th pillar. At first you have m power. Can you jump from s-th pillar to e-th pillar.InputThe input consists of sev 阅读全文

posted @ 2011-10-18 11:03 More study needed. 阅读(268) 评论(0) 推荐(0) 编辑

2011年10月17日

感悟DFS

摘要: 昨天看到了一句话让我对DFS算法有极深的感悟。这句话就是:DFS有三个条件:1.最深深度 2.结束条件 3.如何扩展。其实对于1, 2,两点,感觉不是问题的关键,第三点才是问题的核心。如何说呢?还是来结合一个实例吧,不然,很难说清楚。例题:有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的。有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装满或原桶空了。当然每一次灌注都是完全的。由于节约,牛奶不会有丢失。写一个程序去帮助约翰找出当A桶是空的时候,C桶中牛奶所剩量的所有可能性。解题思路:每次最多无非有6种倒法,即a->b;a 阅读全文

posted @ 2011-10-17 09:33 More study needed. 阅读(355) 评论(0) 推荐(0) 编辑

位运算处理N皇后

摘要: n皇后问题位运算版n皇后问题是啥我就不说了吧,学编程的肯定都见过。下面的十多行代码是n皇后问题的一个高效位运算程序,看到过的人都夸它牛。初始时,upperlim:=(1 shl n)-1。主程序调用test(0,0,0)后sum的值就是n皇后总的解数。procedure test(row,ld,rd:longint);varpos,p:longint;begin{ 1}if row<>upperlim then{ 2}begin{ 3} pos:=upperlim and not (row or ld or rd);{ 4} while pos<>0 do{ 5} be 阅读全文

posted @ 2011-10-17 08:45 More study needed. 阅读(798) 评论(0) 推荐(0) 编辑

2011年10月16日

最基本的位运算

摘要: === 1. and运算 ===( & ) and运算通常用于二进制取位操作,例如一个数 and 1的结果就是取二进制的最末位。这可以用来判断一个整数的奇偶,二进制的最末位为0表示该数为偶数,最末位为1表示该数为奇数. 相同位的两个数字都为1,则为1;若有一个不为1,则为0。 00111 11100 (&或者and) ---------------- 00100=== 2. or运算 ===( | ) or运算通常用于二进制特定位上的无条件赋值,例如一个数or 1的结果就是把二进制最末位强行变成1。如果需要把二进制最末位变成0,对这个数or 1之后再减一就可以了,其实际意义就是 阅读全文

posted @ 2011-10-16 19:53 More study needed. 阅读(1785) 评论(0) 推荐(5) 编辑

DFS解决USACO——Mother's Milk

摘要: DescriptionFarmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucke 阅读全文

posted @ 2011-10-16 18:45 More study needed. 阅读(751) 评论(0) 推荐(0) 编辑

经典进制转换——USACO Palindromic Squares

摘要: DescriptionPalindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also pri 阅读全文

posted @ 2011-10-16 11:00 More study needed. 阅读(227) 评论(0) 推荐(0) 编辑

动态规划解决USACO——Number Triangles

摘要: DescriptionConsider the number triangle shown below. Write a program that calculates the highest sumof numbers that can be passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right. 7 3... 阅读全文

posted @ 2011-10-16 10:50 More study needed. 阅读(908) 评论(0) 推荐(0) 编辑

2011年10月14日

归并排序

摘要: #include<iostream>using namespace std;const int SIZE = 100;int arr[SIZE];void mergeSort(int fir,int end){ //当子序列就只有一个元素的时候就弹出 if(fir==end)return; //分治 int mid = (fir+end)/2; mergeSort(fir,mid); mergeSort(mid+1,end); //合并 int tempArr[SIZE]; int fir1=fir,fir2=mid+1; for(int i=fir;i<=end;i++) 阅读全文

posted @ 2011-10-14 13:19 More study needed. 阅读(170) 评论(0) 推荐(0) 编辑

2011年10月13日

二分查找

摘要: 这个是精简了的二分查找,个人觉得实在是无法简化了,如果还可以的话,请高人指点一二,先谢谢了。View Code #include "iostream"#include "algorithm"using namespace std;int BinSearch(int *R, int n, int KeyNum){ int low = 0, high = n+1, mid=0; //mid设置为0,是为了利用R[mid]来查找,这样更加精简代码 while(low <= high) { if(R[mid] == KeyNum) //包含了R[0]的情况 阅读全文

posted @ 2011-10-13 22:53 More study needed. 阅读(213) 评论(0) 推荐(0) 编辑

导航

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!