3
7 4
2 4 6
8 5 9 3
that is, 3 7 4 9 = 23.
find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
note: as there are only 16384 routes, it is possible to solve this problem by trying every route. however, , is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
最简单的方法就是穷举,从根节点出发,每个节点都有两个分叉,到达底部的路径有估计有2的指数级的数目(有会算的朋友请留言,我的组合数学都还给老师了),不过这道题显然是符合动态规划的特征,往下递增一层的某个节点的最佳结果f[i][j]肯定是上一层两个入口节点对应的最佳结果的最大值,也就是f[i-1][j]或者f[i-1][j 1],递归的边界就是定点f[0][0]=75。因此我的解答如下,考虑了金字塔边界的情况,数据按照金字塔型存储在numbers.txt中,
rope
的附加性能通常比 stringbuffer
好,这时使用 rope
是否有意义呢?答案还是否。不论何时将输入的数据组合在一起形成格式化输出时,最漂亮最有效的方法是使用模板引擎(例如 stringtemplate
或 freemarker)。这种方法不仅能干净地将表示标记与代码分开,而且模板只进行一次编译(通常编译为 jvm
字节码),以后可以重用,从而使它们拥有极佳的性能特征。”
游戏人数 |
第一名获得赌注 |
第二名获得赌注 |
第三名获得赌注 |
第四名获得赌注 |
二人 |
100% |
0% |
— |
— |
二人(出现2个第1名时) |
50% |
50% |
|
|
三人 |
70% |
30% |
0% |
— |
三人(出现3个第1名时) |
33.3333% |
33.3333% |
33.3333% |
|
三人(出现2个第1名时) |
50%×2 |
0% |
|
|
1)概念:对于树中的每个节点n,其左子节点中保存的所有数值都小于n保存的数值,右子节点保存的数值都大于n保存的数值。
2)二叉查找树可以实现更为优越的查找性能,主要实现方式有数组和链表结构,相比较而言,链表实现更为容易,因为数组实现删除和添加功能需要移动数组元素(如填补删除空位等)
可以看到,c#的delegate机制非常有趣,如果在java中恐怕需要用inner class来实现了。
// heap.java
// demonstrates heaps
// to run this program: c>java heapapp
import java.io.*;
////////////////////////////////////////////////////////////////
class node
{
private int idata; // data item (key)
// -------------------------------------------------------------
public node(int key) // constructor
{ idata = key; }
// -------------------------------------------------------------
public int getkey()
{ return idata; }
// -------------------------------------------------------------
public void setkey(int id)
{ idata = id; }
// -------------------------------------------------------------
} // end class node
////////////////////////////////////////////////////////////////
class heap
{
private node[] heaparray;
private int maxsize; // size of array
private int currentsize; // number of nodes in array
// -------------------------------------------------------------
public heap(int mx) // constructor
{
maxsize = mx;
currentsize = 0;
heaparray = new node[maxsize]; // create array
}
// -------------------------------------------------------------
public boolean isempty()
{ return currentsize==0; }
// -------------------------------------------------------------
public boolean insert(int key)
{
if(currentsize==maxsize)
return false;
node newnode = new node(key);
heaparray[currentsize] = newnode;
trickleup(currentsize );
return true;
} // end insert()
// -------------------------------------------------------------
public void trickleup(int index)
{
int parent = (index-1) / 2;
node bottom = heaparray[index];
while( index > 0 &&
heaparray[parent].getkey() < bottom.getkey() )
{
heaparray[index] = heaparray[parent]; // move it down
index = parent;
parent = (parent-1) / 2;
} // end while
heaparray[index] = bottom;
} // end trickleup()
// -------------------------------------------------------------
public node remove() // delete item with max key
{ // (assumes non-empty list)
node root = heaparray[0];
heaparray[0] = heaparray[--currentsize];
trickledown(0);
return root;
} // end remove()
// -------------------------------------------------------------
public void trickledown(int index)
{
int largerchild;
node top = heaparray[index]; // save root
while(index < currentsize/2) // while node has at
{ // least one child,
int leftchild = 2*index 1;
int rightchild = leftchild 1;
// find larger child
if(rightchild < currentsize && // (rightchild exists?)
heaparray[leftchild].getkey() <
heaparray[rightchild].getkey())
largerchild = rightchild;
else
largerchild = leftchild;
// top >= largerchild?
if( top.getkey() >= heaparray[largerchild].getkey() )
break;
// shift child up
heaparray[index] = heaparray[largerchild];
index = largerchild; // go down
} // end while
heaparray[index] = top; // root to index
} // end trickledown()
// -------------------------------------------------------------
public boolean change(int index, int newvalue)
{
if(index<0 || index>=currentsize)
return false;
int oldvalue = heaparray[index].getkey(); // remember old
heaparray[index].setkey(newvalue); // change to new
if(oldvalue < newvalue) // if raised,
trickleup(index); // trickle it up
else // if lowered,
trickledown(index); // trickle it down
return true;
} // end change()
// -------------------------------------------------------------
public void displayheap()
{
system.out.print("heaparray: "); // array format
for(int m=0; mif(heaparray[m] != null)
system.out.print( heaparray[m].getkey() " ");
else
system.out.print( "-- ");
system.out.println();
// heap format
int nblanks = 32;
int itemsperrow = 1;
int column = 0;
int j = 0; // current item
string dots = "...............................";
system.out.println(dots dots); // dotted top line
while(currentsize > 0) // for each heap item
{
if(column == 0) // first item in row?
for(int k=0; ksystem.out.print(' ');
// display item
system.out.print(heaparray[j].getkey());
if( j == currentsize) // done?
break;
if( column==itemsperrow) // end of row?
{
nblanks /= 2; // half the blanks
itemsperrow *= 2; // twice the items
column = 0; // start over on
system.out.println(); // new row
}
else // next item on row
for(int k=0; ksystem.out.print(' '); // interim blanks
} // end for
system.out.println("/n" dots dots); // dotted bottom line
} // end displayheap()
// -------------------------------------------------------------
} // end class heap
////////////////////////////////////////////////////////////////
class heapapp
{
public static void main(string[] args) throws ioexception
{
int value, value2;
heap theheap = new heap(31); // make a heap; max size 31
boolean success;
theheap.insert(70); // insert 10 items
theheap.insert(40);
theheap.insert(50);
theheap.insert(20);
theheap.insert(60);
theheap.insert(100);
theheap.insert(80);
theheap.insert(30);
theheap.insert(10);
theheap.insert(90);
while(true) // until [ctrl]-[c]
{
system.out.print("enter first letter of ");
system.out.print("show, insert, remove, change: ");
int choice = getchar();
switch(choice)
{
case 's': // show
theheap.displayheap();
break;
case 'i': // insert
system.out.print("enter value to insert: ");
value = getint();
success = theheap.insert(value);
if( !success )
system.out.println("can't insert; heap full");
break;
case 'r': // remove
if( !theheap.isempty() )
theheap.remove();
else
system.out.println("can't remove; heap empty");
break;
case 'c': // change
system.out.print("enter current index of item: ");
value = getint();
system.out.print("enter new key: ");
value2 = getint();
success = theheap.change(value, value2);
if( !success )
system.out.println("invalid index");
break;
default:
system.out.println("invalid entry/n");
} // end switch
} // end while
} // end main()
//-------------------------------------------------------------
public static string getstring() throws ioexception
{
inputstreamreader isr = new inputstreamreader(system.in);
bufferedreader br = new bufferedreader(isr);
string s = br.readline();
return s;
}
//-------------------------------------------------------------
public static char getchar() throws ioexception
{
string s = getstring();
return s.charat(0);
}
//-------------------------------------------------------------
public static int getint() throws ioexception
{
string s = getstring();
return integer.parseint(s);
}
//-------------------------------------------------------------
} // end class heapapp
////////////////////////////////////////////////////////////////