(http://leetcode.com/2011/01/ctrla-ctrlc-ctrlv.html)
Imagine you has a special keyboard with the following keys:
A
Ctrl + A
Ctrl + C
Ctrl + V
Where CTRL + A, CTRL + C, CTRL + V each acts as one function key for "Select All", "Copy", and "Paste" operations respectively.
If you can have only press the keyboard-N times (with the above four keys), please write a program to produce maximum Numbe RS of A. If possible, please also print out the sequence of keys.
That's to say, and the input parameter is N (No. of keys, you can press), the output is M (No. of as that can produce).
The Code:
intFINDMAXK (intN) { intPower =2; DoubleMax =0.0; intMAXK =0; while(N >0) {n-=2; DoubleT = (Double) n/Power; DoubleR = Pow (t, (Double) power); if(R >max) {MAXK=Power; Max=R; } Power++; } returnmaxk;} unsignedintFintN) { if(N <=7) returnN; intK =findmaxk (n); intsum = n-2* (K-1); unsignedintMul =1; while(k >0) { intAVG = sum/K; Mul*=avg; K--; Sum-=avg; } assert (Sum==0); returnMul;}
CTRL + A, CTRL + C, CTRL + V