摘要:
今天做了一道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...
閱讀全文
摘要:
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
閱讀全文
摘要:
昨天看到了一句話讓我對DFS算法有極深的感悟。這句話就是:DFS有三個條件:1.最深深度 2.結束條件 3.如何擴展。其實對于1, 2,兩點,感覺不是問題的關鍵,第三點才是問題的核心。如何說呢?還是來結合一個實例吧,不然,很難說清楚。例題:有三個容量分別是A,B,C升的桶,A,B,C分別是三個從1到20的整數,最初,A和B桶都是空的,而C桶是裝滿牛奶的。有時,約翰把牛奶從一個桶倒到另一個桶中,直到被灌桶裝滿或原桶空了。當然每一次灌注都是完全的。由于節約,牛奶不會有丟失。寫一個程序去幫助約翰找出當A桶是空的時候,C桶中牛奶所剩量的所有可能性。解題思路:每次最多無非有6種倒法,即a->b;a
閱讀全文
摘要:
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
閱讀全文
摘要:
=== 1. and運算 ===( & ) and運算通常用于二進制取位操作,例如一個數 and 1的結果就是取二進制的最末位。這可以用來判斷一個整數的奇偶,二進制的最末位為0表示該數為偶數,最末位為1表示該數為奇數. 相同位的兩個數字都為1,則為1;若有一個不為1,則為0。 00111 11100 (&或者and) ---------------- 00100=== 2. or運算 ===( | ) or運算通常用于二進制特定位上的無條件賦值,例如一個數or 1的結果就是把二進制最末位強行變成1。如果需要把二進制最末位變成0,對這個數or 1之后再減一就可以了,其實際意義就是
閱讀全文
摘要:
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
閱讀全文
摘要:
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
閱讀全文
摘要:
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...
閱讀全文
摘要:
#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++)
閱讀全文
摘要:
這個是精簡了的二分查找,個人覺得實在是無法簡化了,如果還可以的話,請高人指點一二,先謝謝了。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]的情況
閱讀全文