编程之美

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 94

1

1.1 CPU
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11 NIM1
1.12 NIM2
1.13 NIM3
1.14
1.15
1.16
1.17
1.18
2
2.1 1
2.2
2.3
2.4
2.5 K
2.6
2.7
2.8
2.9 Fibonacci
2.10
2.11
2.12
2.13
2.14
2.15
2.16
2.17
2.18
2.19
2.20
2.21
3
3.1
3.2

3.3
3.4
3.5
3.6
3.7
3.8
3.9
3.10
3.11
4
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11

1.1 CPU

http://www.china-pub.com/38070

VSTS

3
www.msra.cn/bop

CPU

Windows Task Manager CPU

1. CPU50%
2. CPU1~ 100

1.1 CPU

3. CPU

while (true)
{
if (busy)
i++
else
}

else CPU
0 CPU
CPU
MSRA TTGMicrosoft Research Asia, Technology Transfer Group
1-1

1-1

CPU

CPU
50%

1.1 CPU

CPU 100%
1
2

CPU
CPU CPU
CPU 0 CPU
ProcessSystem Idle Process CPU

WaitForSingleObject() Sleep()
CPU
CPU CPU

/ CPU

CPU usage CPU Task


Manager busy idle loop
CPU Usage

Busy loop idle Sleep()

loop
Sleepfornn
for(i = 0; i < n; i++) n
CPU

loop:
mov dx i
inc dx
mov i dx
cmp i n
jl loop

;idx
;dx1
;dxi
;in
;i n

CPU P4 2.4Ghz2.4 * 10 9
CPU 2 400 000
000 * 2/5=960 000 000/ CPU 1 960 000 000
n = 60 000 000 Sleep(1000) CPU

Windows

1.1 CPU

1 1 >50%

n = 9 600 000 10
Sleep(10) 10 Windows
1

1-1
int main()
{
for(;;)
{
for(int i = 0; i < 9600000; i++);
Sleep(10);
}
return 0;
}

9 600 000
50% CPU

1. sleep/awake

CPU
2. system callI/Oprivilege instruction

CPU
n CPU /

GetTickCount() Sleep()
GetTickCount() 49.7
Sleep() 1

GetTickCount() busy loop Sleep() idle loop


1-2
int busyTime = 10; //10 ms
int idleTime = busyTime; //same ratio will lead to 50% cpu usage

1.1 CPU

Int64 startTime = 0;
while (true)
{
startTime = GetTickCount();
// busy loop
while ((GetTickCount() - startTime) <= busyTime) ;
//idle loop
Sleep(idleTime);
}

10% CPU
40% CPU 50% 50%

CPU Perfmon.exe
Perfmon Windows NT Windows
1-2 Perfmon
Perfmon
RAM
CPU

1-2

Perfmon

Perfmon Microsoft .Net Framework


PerformanceCounter()
CPU

1.1 CPU

1-3
//C# code
static void MakeUsage(float level)
{
PerformanceCounter p = new PerformanceCounter("Processor", "% Processor Time",
"_Total");
while (true)
{
if (p.NextValue() > level)
System.Threading.Thread.Sleep(10);
}
}

CPU
2

1-4
//C++ code to make task manager generate sine graph
#include "Windows.h"
#include "stdlib.h"
#include "math.h"
const
const
const
const

double SPLIT = 0.01;


int COUNT = 200;
double PI = 3.14159265;
int INTERVAL = 300;

int _tmain(int argc, _TCHAR* argv[])


{
DWORD busySpan[COUNT]; //array of busy times
DWORD idleSpan[COUNT]; //array of idle times
int half = INTERVAL / 2;
double radian = 0.0;
for(int i = 0; i < COUNT; i++)
{
busySpan[i] = (DWORD)(half + (sin(PI * radian) * half));
idleSpan[i] = INTERVAL - busySpan[i];
radian += SPLIT;
}
DWORD startTime = 0;
int j = 0;
while (true)
{
j = j % COUNT;
startTime = GetTickCount();
while ((GetTickCount() - startTime) <= busySpan[j]) ;
Sleep(idleSpan[j]);
j++;
}
return 0;
}

1.1 CPU

CPU CPU
CPU
50%
CPU CPU GetProcessorInfo()
,
SetThreadAffinityMask()

RDTSC CPU
x86
inline __int64 GetCPUTickCount()
{
__asm
{
rdtsc;
}
}

x64
#define GetCPUTickCount() __rdtsc()

CallNtPowerInformation API CPU


1-5
_PROCESSOR_POWER_INFORMATION info;
CallNTPowerInformation(11, //query processor power information
NULL,
//no input buffer
0,
//input buffer size is zero
&info,
//output buffer
Sizeof(info));
//outbuf size
__int64 t_begin = GetCPUTickCount();
//do something
__int64 t_end = GetCPUTickCount();
double millisec = ((double)t_end
(double)t_begin)/(double)info.CurrentMhz;

RDTSC CPU CPU CPU


CPU
CPU
SetThreadAffinityMask CPU

// API

1.1 CPU
1. Sleep()
2. WaitForSingleObject()
3. GetTickCount() Tick
4. QueryPerformanceFrequency()QueryPerformanceCounter()

CPU
5. timeGetSystemTime()
6. PerformanceCounter
7. GetProcessorInfo()/SetThreadAffinityMask()

CPU
8. GetCPUTickCount() CPU

API Windows

www.ieee.org.cn/BCZM.asp

1.2

1-3 A B

1-3
AB 33 ,A {d10, f10,
d8, f8} B {d3, f3, d1, f1}AB
A BA B
A d10 B d1d2 d3
AB

www.ieee.org.cn/BCZM.asp

AB

A
B
AB

AB
A B
1~9 1-4
AB

1-4

1~9 AB

A B

bool true false


byte int 9

8 byte 28=256 AB
b 4 bit A
4 bit B 4 bit 16
bit byte

byte b10100101 4 bit0101 n0011

b bits bits:

www.ieee.org.cn/BCZM.asp
11110000LMASK
& 10100101b
----------10100000
n
10100000LMASK & b
^ 00000011n
-----------10100011
byte b10100101 4 bit1010 n0011

b bits bits:
00001111RMASK
& 10100101b
----------00000101
n byte
n << 4 = 00110000

00000101RMASK & b
^ 00110000n << 4
----------00110101

www.ieee.org.cn/BCZM.asp
byte 4 bits 4 bitse.g. 10100101 1010 0101

b bits bits
00001111RMASK
& 10100101b
----------00000101
b bits bits
11110000LMASK
& 10100101b
----------10100000
4 bits
10100000 >> 4 = 00000101
for
1byte

for (LSET(b, 1); LGET(b) <= GRIDW * GRIDW; LSET(b, (LGET(b) + 1)))

1-6
#define HALF_BITS_LENGTH 4
// 4bit
#define FULLMASK 255
// bitmask11111111
#define LMASK (FULLMASK << HALF_BITS_LENGTH)
// bitsmask11110000
#define RMASK (FULLMASK >> HALF_BITS_LENGTH)
// bitsmask00001111
#define RSET(b, n) (b = ((LMASK & b) ^ n))
// bn
#define LSET(b, n) (b = ((RMASK & b) ^ (n << HALF_BITS_LENGTH)))
// bn
#define RGET(b) (RMASK & b)
// b
#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)
// b
#define GRIDW 3
//
#include <stdio.h>
#define HALF_BITS_LENGTH 4
#define FULLMASK 255
#define LMASK (FULLMASK << HALF_BITS_LENGTH)
#define RMASK (FULLMASK >> HALF_BITS_LENGTH)
#define RSET(b, n) (b = ((LMASK & b) ^ n))
#define LSET(b, n) (b = ((RMASK & b) ^ (n << HALF_BITS_LENGTH)))
#define RGET(b) (RMASK & b)
#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)
#define GRIDW 3

www.ieee.org.cn/BCZM.asp
int main()
{
unsigned char b;
for(LSET(b, 1); LGET(b) <= GRIDW * GRIDW; LSET(b, (LGET(b) + 1)))
for(RSET(b, 1); RGET(b) <= GRIDW * GRIDW; RSET(b, (RGET(b) + 1)))
if(LGET(b) % GRIDW != RGET(b) % GRIDW)
printf("A = %d, B = %d\n", LGET(b), RGET(b));
return 0;
}

N N = 1, 2, , 8, 9 1-5

1-5

www.ieee.org.cn/BCZM.asp

A = 1, B = 2

A = 4, B = 2

A = 7, B = 2

A = 1, B = 3

A = 4, B = 3

A = 7, B = 3

A = 1, B = 5

A = 4, B = 5

A = 7, B = 5

A = 1, B = 6

A = 4, B = 6

A = 7, B = 6

A = 1, B = 8

A = 4, B = 8

A = 7, B = 8

A = 1, B = 9

A = 4, B = 9

A = 7, B = 9

A = 2, B = 1

A = 5, B = 1

A = 8, B = 1

A = 2, B = 3

A = 5, B = 3

A = 8, B = 3

A = 2, B = 4

A = 5, B = 4

A = 8, B = 4

A = 2, B = 6

A = 5, B = 6

A = 8, B = 6

A = 2, B = 7

A = 5, B = 7

A = 8, B = 7

A = 2, B = 9

A = 5, B = 9

A = 8, B = 9

A = 3, B = 1

A = 6, B = 1

A = 9, B = 1

A = 3, B = 2

A = 6, B = 2

A = 9, B = 2

A = 3, B = 4

A = 6, B = 4

A = 9, B = 4

A = 3, B = 5

A = 6, B = 5

A = 9, B = 5

A = 3, B = 7

A = 6, B = 7

A = 9, B = 7

A = 3, B = 8

A = 6, B = 8

A = 9, B = 8

MSRA

BYTE i = 81;
while(i--)
{
if(i / 9 % 3 == i % 9 % 3)
continue;
printf(A = %d, B = %d\n, i / 9 + 1, i % 9 + 1);
}

www.ieee.org.cn/BCZM.asp
1-7
struct {
unsigned char a:4;
unsigned char b:4;
} i;
for(i.a = 1; i.a <= 9; i.a++)
for(i.b = 1; i.b <= 9; i.b++)
if(i.a % 3 == i.b % 3)
printf(A = %d, B = %d\n, i.a, i.b);

Matt Scott

--www.ieee.org.cn/BCZM.asp

1.6

No

STCSmart Tea Corp. VSTC


2 23=8 25=32
STC

--www.ieee.org.cn/BCZM.asp

STC n SiViCiHiBi
i i = 0, 1,, n-1
i Ci=V/Vi

=V max{

OptV', i i, i+1,
i+2, , n-1, n V'
OptV, n
Opt V', i = max { k* Hi + OptV' - Vi * k, i-1}
k = 0, 1, , Ci, i =0, 1, , n-1
= k + k

Opt0, n= 0 0 0
Optx, n= -INFx != 0INF 0

1-9
int Cal(int V, int type)
{
opt[0][T] = 0;//
for(int i = 1; i <= V; i++)//
{
opt[i][T] = -INF;
}
for(int j = T - 1; j >= 0; j--)
{
for(int i = 0; i <= V; i++)
{

--www.ieee.org.cn/BCZM.asp
opt[i][j] = -INF;
for(int k = 0; k <= C[j]; k++)
// jk
{
if(i <= k * V[j])
{
break;
}
int x = opt[i - k * V[j]][j + 1];
if(x != -INF)
{
x += H[j] * k;
if(x > opt[i][j])
{
opt[i][j] = x;
}
}
}
}
}
return opt[V][0];
}

OV*N OV*N*MaxCi
opt[i][j] opt[i][j+2]
opt[i][j] opt[i][j+1] Ov

--www.ieee.org.cn/BCZM.asp

1-10
int[V + 1][T + 1] opt;

//
//
//
//

i T
V
optV'iT-1opt[V][i]
opt-1

int Cal(int V, int type)


{
if(type == T)
{
if(V == 0)
return 0;
else
return -INF;
}
if(V < 0)
return -INF;
else if(V == 0)
return 0;
else if(opt[V][type] != -1)
return opt[V][type];
//
//
int ret = -INF;
for(int i = 0; i <= C[type]; i++)
{
int temp = Cal(V i * C[type], type + 1);
if(temp != -INF)
{
temp += H[type] * i;
if(temp > ret)
ret = temp;
}
}
return opt[V][type] = ret;
}

--www.ieee.org.cn/BCZM.asp

L
Volume

TotalCount

Happiness

20L
20L
...
21L
...
2000L
...

TC_00
TC_01
...
TC_10
...
TC_M0
...

H_00
H_01
...
H_10
...
H_M0
...

2 000L V%21 20L

V-20L 21L
21L 20L
20L
2L1

1-12

NIM

NIM

N A B A BABA

1
A

NIM

[345]

what
C++
how

N M
N=1

N=2 11

N = 2M > 2 1, 11, X
1, X1, 1
1, X2, 222
113, 34, 4X, X

X, X

M=3 2, 11, 1, 1
M=5 M=3
M=7

1, 1,, 1 1 1

1, 1,, 1, X 1 1 X

1 X X-1 1

1 X X 1

M = 9
2, 3, 41, 4, 41, 2, 6

XOR

1-12

NIM

XOR0, 0= 0
XOR1, 0= 1
XOR1, 1= 0
NM1, M2, , Mn
0, 0,, 0
M M
M1, M1

XORM1, M1= 0

M1, M2

XORM1, M2!= 0

M2, M2

XORM2, M2= 0

M2, M2

M1, 1, ,1XOR1, 1,,1


[]

!=0XOR1, 1,, 1[]=0

M 1 XOR
M1, M2, , Mn

XORM1, M2, Mn=

M1, M2, Mn

XORM1, M2, Mn=

0, 0, , 0

XOR00 0=0

XORM1, M2, Mn
0 1
1

[ 1]

XORM1, M2, Mn!= 0


MiXORM1, M2, Mi Mn= 0

[ 2]

XORM1, M2, Mn= 0 M


XORM1, M2, Mi Mn! = 0

[ 3]

M=9126
126
1=0 0 1
2=0 1 0
6=1 1 0
XOR=1 0 1

XOR126!=0

B 1, 2, 3123
1=0 0 1
2=0 1 0
3=0 1 1
XOR=0 0 0

XOR123=0

A 1, 2, 2XOR1, 2, 2!=0
B 0, 2, 2XOR 0, 2, 2=0
A
B 0, 0, 0XOR0, 0, 0= 0

XOR 1
XOROrz
M1, M2, , Mn
2

3, 4, 5 1, 4, 5

XORNIM

6, 6

1-12

NIM

2K

--www.ieee.org.cn/BCZM.asp

Microsoft Link-up

1-17

1-17 Microsoft Link-up

Microsoft Link-up

1.
2.
3.

4.

--www.ieee.org.cn/BCZM.asp

1-22

Grid preClick = NULL, curClick = NULL;


while()
{

if((x, y)(x, y))


{
preClick = curClick;
curClick.Pos = (x, y);
}
if(preClick != NULL && curClick != NULL
&& preClick.Pic == curClick.Pic
&& FindPath(preClick, curClick) != NULL)
{

preClick, curClick;
preClick = curClick = NULL;
}
}

1.
2.

--www.ieee.org.cn/BCZM.asp

3
3.
4.

1-18
3

1-18

Ax1, y1 Bx2, y2

Ax1, y1
Ax1, y1 Ax1, y1

--www.ieee.org.cn/BCZM.asp

0
S0S0 = Findx1, y1 Bx2, y2 S0
A B
S0
S1S1 = {Findp| p S0}
S1 S0 S1= S1-S0 S1 Ax1, y1
1 Bx2, y2 S1
A B 1
S1
S2S2 = Find{ Find
p| p S1}S2 S0 S1 S2= S2 - S0 - S1 = S2
- S0 - S1 S2 Ax1, y1 2
Bx2, y2 S2 A B
2 A B
3

A B
S1 = S1 - S0
S2= S2 - S0 -S1 Ax1, y1x,
yx, y Ax1, y1
MinCrossingx, y
MinCrossingA= MinCrossingx1, y1= 0 A
S0
MinCrossing 0 S0 S1 X
A 1 X S0
MinCrossingX= 0
MinCrossingX= MinValueMinCrossingX, 1= 0
S1= S1 - S0S2= S2 - S0
S1
Xx, y
MinCrossingX A

X
MinCrossing X MinDistanceX X Y

--www.ieee.org.cn/BCZM.asp

if((MinCrossing(X) + 1 < MinCrossing(Y)) ||


((MinCrossing(X) + 1 == MinCrossing(Y) && (MinDistance(X) +
Dist(X,Y) < MinDistance(Y)))
{
MinCrossing(Y) = MinCrossing(X) + 1;
MinDistance(Y) = MinDistance(X) + Dist(X, Y);
}
X
Y

Ax1, y1

1.

2.

www.ieee.org.cn/BCZM.asp

8bit1

www.ieee.org.cn/BCZM.asp

PC

2
0 1
10 100 010
2 1 010 001 0
2 101 000 1

2-1
int Count(int v)
{
int num = 0;
while(v)
{
if(v % 2 == 1)
{
num++;
}
v = v/ 2;
}
return num;
}

1
10 100 001
1
00000001 1
1 0
2-2

www.ieee.org.cn/BCZM.asp
int Count(int v)
{
int num = 0;
While(v)
{
num += v &0x01;
v >>= 1;
}
return num;
}

O
log2vlog2v
1
10 100 001 1
1
1 01 000 000
1 2
1
0 1
001 000 000 00 111 111
01 000 000 &01 000 000 00 000 001= 01 000 000 &
00 111 111 = 0

2-3
int Count(int v)
{
int num = 0;
while(v)
{
v &= (v-1);
num++;
}
return num;
}

OM M v 1
1 0~255

www.ieee.org.cn/BCZM.asp
2-4
int Count(int v)
{
int num = 0;
switch (v)
{
case 0x0:
num = 0;
break;
case 0x1:
case 0x2:
case 0x4:
case 0x8:
case 0x10:
case 0x20:
case 0x40:
case 0x80:
num = 1;
break;
case 0x3:
case 0x6:
case 0xc:
case 0x18:
case 0x30:
case 0x60:
case 0xc0:
num = 2;
break;
//...
}
return num;
}

a =0 1 case a =255
case 255

1+2+ +N

2-5
/* */
int countTable[256]
{
0, 1, 1, 2, 1, 2,
3, 4, 3, 4, 4,
4, 3, 4, 4, 5,
3, 4, 4, 5, 2,
4, 4, 5, 3, 4,

=
2,
5,
3,
3,
4,

3,
1,
4,
3,
5,

1,
2,
4,
4,
4,

2,
2,
5,
3,
5,

2,
3,
4,
4,
5,

3,
2,
5,
4,
6,

2,
3,
5,
5,
3,

3,
3,
6,
3,
4,

3,
4,
1,
4,
4,

4,
2,
2,
4,
5,

1,
3,
2,
5,
4,

2,
3,
3,
4,
5,

2,
4,
2,
5,
5,

3,
3,
3,
5,
6,

2,
4,
3,
6,
4,

3,
4,
4,
2,
5,

3,
5,
2,
3,
5,

4,
2,
3,
3,
6,

2,
3,
3,
4,
5,

3,
3,
4,
3,
6,

www.ieee.org.cn/BCZM.asp
6,
5,
3,
4,
4,
7,

7, 1, 2, 2,
3, 4, 4, 5,
4, 4, 5, 4,
4, 5, 4, 5,
5, 4, 5, 5,
6, 7, 7, 8

3,
4,
5,
5,
6,

2,
5,
5,
6,
4,

3,
5,
6,
3,
5,

3,
6,
4,
4,
5,

4,
2,
5,
4,
6,

2,
3,
5,
5,
5,

3,
3,
6,
4,
6,

3,
4,
5,
5,
6,

4,
3,
6,
5,
7,

3,
4,
6,
6,
4,

4,
4,
7,
4,
5,

4,
5,
2,
5,
5,

5,
3,
3,
5,
6,

2,
4,
3,
6,
5,

3,
4,
4,
5,
6,

3,
5,
3,
6,
6,

4,
4,
4,
6,
7,

3,
5,
4,
7,
5,

4,
5,
5,
3,
6,

4,
6,
3,
4,
6,

};
int Count(int v)
{
//check parameter
return countTable[v];
}

0~255 1v
countTable[v] v 1 O1

1. 32DWORD
2. ABAB
bitA B

--www.ieee.org.cn/BCZM.asp

2.2

Factorial

1. NNN0N10N3 628 800


N0
2. N1

--www.ieee.org.cn/BCZM.asp

N
10
N= K10M K 10 N M 0
NN=2x3y5z 10 = 25 M X Z
2 5 10 M = minX, Z X Z
2 5 M = Z
Z N 0
1
Z ii =1, 2, , N 5

2-6
ret = 0;
for(i = 1; i <= N; i++)
{
j = i;
while(j % 5 ==0)
{
ret++;
j /= 5;
}
}

1
Z = [N/5] +[N/52] +[N/53] +
K 5K > N[N/5K]=0
[N/5] N 5 5[N/52] N 52
5
ret = 0;
while(N)
{
ret += N / 5;
N /= 5;
}

2 N 1 N N
1 N = 3N= 6 N1 010
1

--www.ieee.org.cn/BCZM.asp

2
2
0 0
1 2
N 2 N
2 1
2
N! 2 N/2 + N/4 + N/8 + N/16 +
1

2-7
int lowestOne(int N)
{
int Ret = 0;
while(N)
{
N >>= 1;
Ret += N;
}
return Ret;
}

2
N 2 N N 1

N = 11011 N! 2 N/2
+ N/4 + N/8 + N/16 +

1101 + 110 + 11 + 1
=1000 + 100 + 1
+100 + 10
+10 + 1
+1
=1000 + 100+ 10 + 1+100 + 10 + 1+ 1
= 1111 + 111 + 1
=10000 -1+1000 - 1+10-1+1-1

N/k 1, 2, 3, , N k

--www.ieee.org.cn/BCZM.asp
= 11011-N 1

m N N = b[1] + b[2] * 2 + b[3] * 22 + + b[m] *


2(m-1) b [ i ] i 1 0 b[1] 1
N 2

n 2 n>0&&n&n-1==0

--www.ieee.org.cn/BCZM.asp

Tango Tango
Tango ID

ID Tango

--www.ieee.org.cn/BCZM.asp

ID
ID ID ID
ID ON * log2N + N
ID ID
ID N ID
ID N/2 0 ID

O1
ID

ID ID ID
ID
ID ID
ON

2-8
Type Find(Type* ID, int N)
{
Type candidate;
int nTimes, i;
for(i = nTimes = 0; i < N; i++)
{
if(nTimes == 0)
{
candidate = ID[i], nTimes = 1;
}
else
{
if(candidate == ID[i])
nTimes++;
else
nTimes--;
}
}
return candidate;
}

--www.ieee.org.cn/BCZM.asp
ID ID

Tango 3
ID N 1/4 ID
ID

--www.ieee.org.cn/BCZM.asp

N 1 N
1

N= 2 12 1 1
N= 12 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 121
5

1.

fN1N1,f12
=5

2.

32fN= NN

--www.ieee.org.cn/BCZM.asp

fN
1 N1
1 N 1
2-9
ULONGLONG Count1InAInteger(ULONGLONG n)
{
ULONGLONG iNum = 0;
while(n != 0)
{
iNum += (n % 10 == 1) ? 1 : 0;
n /= 10;
}
return iNum;
}
ULONGLONG f(ULONGLONG n)
{
ULONGLONG iCount = 0;
for (ULONGLONG i = 1; i <= n; i++)
{
iCount += Count1InAInteger(i);
}

--www.ieee.org.cn/BCZM.asp

return iCount;
}

ON1 = ON *
log2 N
N
N=100 000 000 f
N 40 N

1 N 1
1 N
1 N
fN
1
N N
1
N
1 fN

--www.ieee.org.cn/BCZM.asp

1
N = 3 1 3 123
1 1 N
N>=1 fN 1 N=0 fN
0
2
N=13 1 13 123456
78910111213 1
1 1 11
1 4 101112 13 fN=2+4=6
11 1 11
1 1
N=23 N=13 1
10 10 19 1 111 21
fN=3+10=13
1 N
1 1 1
N 0 1
1
1 1 1
1 1 10
f(13) = 1 + 1 = 2 + 4 = 6
f(23) = 1 + 1 = 3 + 10 = 13
f(33) = 1 + 1 = 4 + 10 = 14

f(93) = 1 + 1 = 10 + 10 =
20
3

--www.ieee.org.cn/BCZM.asp

N = 123
1 131, 11, 21, , 91, 101, 111, 121
1 201019, 110119
1 24100123
f23= 1 + 1 +
1 = 13 + 20 + 24 = 57
4 5

N
fN
N=abcde abcde N
1

0 1
12 013 1
1001991 1001 1992 1002 19911 100~11 199
1 200 12
12100

--www.ieee.org.cn/BCZM.asp

1 1

12 113 1 100
1991 1001 1992 1002 19911 100~11 199 1 200
12100
1 12 10012 113
114 123+1
1 2~9 1
12 213 1
1001991 1001 1992 1002 19911 10011 199
12 10012 199 1 300 +112+1
100

fN
2-10
LONGLONG Sum1s(ULONGLONG n)
{
ULONGLONG iCount = 0;
ULONGLONG iFactor = 1;
ULONGLONG iLowerNum = 0;
ULONGLONG iCurrNum = 0;

--www.ieee.org.cn/BCZM.asp

ULONGLONG iHigherNum = 0;
while(n / iFactor != 0)
{
iLowerNum = n - (n / iFactor) * iFactor;
iCurrNum = (n / iFactor) % 10;
iHigherNum = n / (iFactor * 10);
switch(iCurrNum)
{
case 0:
iCount += iHigherNum * iFactor;
break;
case 1:
iCount += iHigherNum * iFactor + iLowerNum
+ 1;
break;
default:
iCount += (iHigherNum + 1) * iFactor;
break;
}
iFactor *= 10;
}
return iCount;
}
N fN 1 N
Len N OLen
Olnn/ln10+1 N=100 000 000

--www.ieee.org.cn/BCZM.asp

40 1
40 000

--www.ieee.org.cn/BCZM.asp

2
N fN=N

99

110+101=20

999

1100+1020=300

9999
11000+10300=4000

999999999

900000000

9999999999

10000000000

f10n-1= n * 10n-1
n = 9 fn n
n N fn n
0N N f
n= n N n N
0 fn= n

--www.ieee.org.cn/BCZM.asp

N
N
fn= n

n 10 fn 1
n 100 fn 20
n 1 000 fn 300
n 10 000 fn 4 000

n 10k fn k*10k-1
k>=10 k*10 k-1> 10 k fn
n
f1010-1=1010>1010-1 N n = N fN
-N>1010-1 n fn n
n m m 1010-1 f
N-N>1010-1 fN + m> fN> N + 1010-1 > N + m
fn n m 1010-1 f n

--www.ieee.org.cn/BCZM.asp

n fN + m- fN>N+m- N
fN + m> fN+ m > N + 1010-1+ m > N + m fn
n
fN- N > 1010-1 N

N
f1010-1= n *1010-1 N = 10K-1 f10 K-1
-10 K-1> 1010-1 K*10 K-1 -10 K-1> 1010-1 K > =11
K = 11 N=1011-1
n
N = 1011-1=99 999 999 999 n N 0
fn= n
n = 1 111 111 110 fn= n

f1= 1
f10= 10 01, 10 1
f11= 100 01, 10, 11 1

--www.ieee.org.cn/BCZM.asp

--www.ieee.org.cn/BCZM.asp
K

int array[100]

int array[1000]
1
float array [100 000 000]

--www.ieee.org.cn/BCZM.asp

float array [10000000]


StackHeap

ON * log2N K OK O
N * log2N+ OK = ON * log2N
K=1 ON * log2N
N-1
K K
N-K
N-K
N K
ON * K
ON * log2N ON * K K
KK < = log2N

N S S
X Sa SbSa XSb X

--www.ieee.org.cn/BCZM.asp
1. SaKSaSbK-|Sa||Sa|Sa

SK
2. SaKSaK

ON *
log2K
2-11
Kbig(S, k):
if(k <= 0):
return []
//
if(length S <= k):
return S
(Sa, Sb) = Partition(S)
return Kbig(Sa, k).Append(Kbig(Sb, k length Sa)
Partition(S):
Sa = []
Sb = []

//
//
//

//

// Swap(S[1], S[Random() % length S])


p = S[1]
for i in [2: length S]:
S[i] > p ? Sa.Append(S[i]) : Sb.Append(S[i])
// p

length Sa < length Sb ? Sa.Append(p) : Sb.Append(p)


return (Sa, Sb)

N K K
K N K
p ON p
N Vmax Vmin N K
[Vmin, Vmax] N K
p

--www.ieee.org.cn/BCZM.asp
2-12
while(Vmax Vmin > delta)
{
Vmid = Vmin + (Vmax - Vmin) * 0.5;
if(f(arr, N, Vmid) >= K)
Vmin = Vmid;
else
Vmax = Vmid;
}
farr, N, Vmid arr[0, , N-1] Vmid

delta N
delta 0.5
Vmin, Vmax
K ON * log2|Vmax - Vmin|
/delta delta N

ON * log2N

[0, 2m-1] m bit


, 0, 1, , m-1 m-1
N 1 0 [0, 2m-1-1]
[2m-1, 2m-1]m-1 0
m-1 1 1 A K
1 K 0
K-A m-2

2 * log2|Vmax - Vmin|/delta

--www.ieee.org.cn/BCZM.asp
N K

N 100

1T 1

N > K K K K
K K+1 X X K
Y K X Y K
Y X K
X Y X Y
ON * K
K K
K X X
Y K X
X Y X Y X

Olog2K

2-1

--www.ieee.org.cn/BCZM.asp
2-1 h[] h[i] h[i/2]
h[2 * i + 1] h[2 * i + 2] X
:
2-13
if(X > h[0])
{
h[0] = X;
p = 0;
while(p < K)
{
q = 2 * p + 1;
if(q >= K)
break;
if((q < K 1) && (h[q + 1] < h[q]))
q = q + 1;
if(h[q] < h[p])
{
t = h[p];
h[p] = h[q];
h[q] = t;
p = q;
}
else
break;
}
}
ON * log2K

K
K K K+1
2 * K K
ceil1K/K

ceilceiling

--www.ieee.org.cn/BCZM.asp
N
K
0, MAXN count[MAXN]
count[i] i
count K
2-14
for(sumCount = 0, v = MAXN 1; v >= 0; v--)
{
sumCount += count[v];
if(sumCount >= K)
break;
}
return v;
N bit

N Vmax Vmin
[Vmin, Vmax] M d =Vmax Vmin
/M [Vmin, Vmin+d], [Vmin + d, Vmin + 2d],
K

O
N+M* log2M|Vmax - Vmin|/delta 2 * log2M|Vmax Vmin|/delta M M
K N

--www.ieee.org.cn/BCZM.asp
3. NK10

1.5, 1.5, 2.5, 2.5, 3.5, 3.5, 5, 0, -1.5, 3.53


5, 3.5, 2.5
4. km0 < k < = m < = n
5. page rank

K
incremental update
K

6.

Kquery
d query
f query, dK
101000
10 001
9999

K
K K
K K
90%*K
K 90% K

--www.ieee.org.cn/BCZM.asp
90% K K
110%*K
7. 4dq1, q2, , qm

fd, q1fd, q2, , fd, qm


qiKqjqi
qi
KqjK

--www.ieee.org.cn/BCZM.asp

float double

0.9 = 9/10
0.3333= 1/3

0.3333= 1/3 = 3/9

0.30.300.30000.33333333

--www.ieee.org.cn/BCZM.asp

0 1

X=0.a1a2 an
X=0.a1a2anb1b2bmX a1a2anb1b2bm 0~9
b1b2bm
X=0.a1a2an X a1a2an
/10n
X=0.a1a2anb1b2bm

X = 0.a1a2anb1b2bm
10n* X= a1a2an.b1b2bm
10n* X= a1a2an+0.b1b2bm
X =a1a2an+0.b1b2bm/10n
a1a2an

Y=0. b1b2bm
m

10 *Y=b1b2bm.b1b2bm
m

10 *Y=b1b2bm+0.b1b2bm
m

10 *Y-Y=b1b2bm
m

Y= b1b2bm /10 -1
Y X
X =a1a2an+Y/10n
=a1a2an+ b1b2bm/10m-1/10n
m

=a1a2an*10 -1+ b1b2bm/10 -1*10n

--www.ieee.org.cn/BCZM.asp

A/BA /GcdA, B/B


/GcdA, B Gcd A B
2.7

0.333
0.333
=3 *102-1+ 33/102-1*10
=3*99+33/990
=1/3
0. 285714285714
0. 285714285714
= 285714 *106-1 + 285714 / 106-1*106
= 285714*999999 +285714 / 999999000000
= 285714 / 999999
= 2/7

--www.ieee.org.cn/BCZM.asp

N N-1

N-1
N N-1 ON2

--www.ieee.org.cn/BCZM.asp

N-1 i 0iN-1
2-13

2-13

array[]s[i] i

iNs[0] = 1 s[i]= s[i-1]array[i-1] i = 1, 2, , N-1, N


t[i]N-i

1iNt[N+1]=1

t[i]=t[i+1]array[i] i=1, 2, , N-1, N


p[i] i , N-1
p[i]=s[i-1]t[i+1]
s[] t[]
p[] p[] p[]
s[]t[]p[] p[]
ON

--www.ieee.org.cn/BCZM.asp

N
P P AN-1 N-1 PN-1
N-1
1. P0

00N-1
QQ
Q0
0N-100
Q
Q0AN-1PN-10
Q
Q
0AN-1PN-10Q
0
2. P

N
PN-1

--www.ieee.org.cn/BCZM.asp
3. P

PPN-1

N P P

+- 0 P

+- 0
ON

www.ieee.org.cn/BCZM.asp

1. ab
2. abddaebdd
3. travellingtraveling
abcdefgabcdef/g

+1abcdefgabcdef
1 1 / 2 = 0.5

www.ieee.org.cn/BCZM.asp

A=xabcdae B=xfdfa A[2, , 7] = abcdae


B[2, , 5] = fdfa
lenA lenB A B
1 AA[2, , lenA]B[1, , lenB]
2 BA[1, , lenA]B[2, , lenB]
3 ABA[2, , lenA]B[2, ,
lenB]
4 BAA[2, , lenA]B[2, ,
lenB]
5 BAA[1, , lenA]
B[2, , lenB]
6 ABA[2, , lenA]
B[1, , lenB]

www.ieee.org.cn/BCZM.asp

6
1. A[2, , lenA]B[1, , lenB]
2. A[1, , lenA]B[2, , lenB]
3. A[2, , lenA]B[2, , lenB]

www.ieee.org.cn/BCZM.asp

3-6
Int CalculateStringDistance(string strA, int pABegin, int pAEnd, string strB,
int pBBegin, int pBEnd)
{
if(pABegin > pAEnd)
{
if(pBBegin > pBEnd)
return 0;
else
return pBEnd pBBegin + 1;
}
if(pBBegin > pBEnd)
{
if(pABegin > pAEnd)
return 0;
else
return pAEnd pABegin + 1;
}
if(strA[pABegin] == strB[pBBegin])
{
return CalculateStringDistance(strA, pABegin +
pBBegin + 1, pBEnd);
}
else
{
int t1 = CalculateStringDistance(strA, pABegin
pBBegin + 2, pBEnd);
int t2 = CalculateStringDistance(strA, pABegin
pBBegin + 1, pBEnd);
int t3 = CalculateStringDistance(strA, pABegin
pBBegin + 2, pBEnd);
return minValue(t1,t2,t3) + 1;
}

1, pAEnd, strB,

+ 1, pAEnd, strB,
+ 2, pAEnd, strB,
+ 2, pAEnd, strB,

CalculateStringDistance(strA, 1, 2, strB, 1, 2)
3-4

www.ieee.org.cn/BCZM.asp

3-4

www.ieee.org.cn/BCZM.asp

--www.ieee.org.cn/BCZM.asp

3-8 h1h2

3-8

--www.ieee.org.cn/BCZM.asp

O(Length(h1) * Length(h2))

hash
hash hash hash
O(max(Length(h1)
+ Length(h2))) O(Length(h1))

3-9

3-9

--www.ieee.org.cn/BCZM.asp

O((Length(h1) + Length(h2))

1. ?
2.

www.ieee.org.cn/BCZM.asp

3-11

3-11

AB

www.ieee.org.cn/BCZM.asp

3-12

3-12

K
U V
1. RootUV
3-13

3-13

www.ieee.org.cn/BCZM.asp
2. RootK
3-14A

3-14

K Uk Vk dUk, Vk
Uk Vk K Rk Uk K
Rk dUk, R dUi, R1ik
max1 max2 R max1+max2+2 R
max{dU1, V1, , dUk, Vkmax1+max2+2}
3-15 O|E|= O
|V|-1 V E

3-15

3-11
//

www.ieee.org.cn/BCZM.asp
struct NODE
{
NODE* pLeft;
NODE* pRight;
int nMaxLeft;
int nMaxRight;
char chValue;
};

//
//
//
//
//

int nMaxLen = 0;
//
void FindMaxLen(NODE* pRoot)
{
//
if(pRoot == NULL)
{
return;
}
// 0
if(pRoot -> pLeft == NULL)
{
pRoot -> nMaxLeft = 0;
}
// 0
if(pRoot -> pRight == NULL)
{
pRoot -> nMaxRight = 0;
}
//
if(pRoot -> pLeft != NULL)
{
FindMaxLen(pRoot -> pLeft);
}
//
if(pRoot -> pRight != NULL)
{
FindMaxLen(pRoot -> pRight);
}
//
if(pRoot -> pLeft != NULL)
{
int nTempMax = 0;
if(pRoot -> pLeft -> nMaxLeft > pRoot -> pLeft -> nMaxRight)
{
nTempMax = pRoot -> pLeft -> nMaxLeft;
}
else
{
nTempMax = pRoot -> pLeft -> nMaxRight;
}
pRoot -> nMaxLeft = nTempMax + 1;
}
//
if(pRoot -> pRight != NULL)

www.ieee.org.cn/BCZM.asp
{
int nTempMax = 0;
if(pRoot -> pRight -> nMaxLeft > pRoot -> pRight -> nMaxRight)
{
nTempMax = pRoot -> pRight -> nMaxLeft;
}
else
{
nTempMax = pRoot -> pRight -> nMaxRight;
}
pRoot -> nMaxRight = nTempMax + 1;
}
//
if(pRoot -> nMaxLeft + pRoot -> nMaxRight > nMaxLen)
{
nMaxLen = pRoot -> nMaxLeft + pRoot -> nMaxRight;
}
}

1.

2.

3. return
3

www.ieee.org.cn/BCZM.asp

800 ?

1, 2, 3, N

1
1.

2.

www.ieee.org.cn/BCZM.asp

1
Fi i
i Fi i-1 i
Pi-1 i
Gi
Pi-1 i-2 i Pi-2 i-2
i i-1 i Qi-1

Fi= Gi* Pi -1= Gi* Qi -1* Pi -2= Gi* Qi -1 Q2* P1


Qi=N-i/N- i + 1, P1= N-1/ N, Gi= 1/N-i + 1
Fi= 1/N
2
i
n i
fn
n n=1 n>i i
12 2 n=i
i PK 1< n < i
n1< n < i
2, 3, , n-1
n n i
n jn < j < = N
j

i P(i )
M

fn1< n <i

www.ieee.org.cn/BCZM.asp

j n

f (n) = 1 /( N n + 1) * (1 + f (n + 1) + ... + f ( N ))(1 < n < i )

f (n) = f (n + 1)(1 < n < i 1)


n=2 1 f ( x) = 1( x > i ), f ( x) = 0( x = i ) 1<n, n+1<i-1

f (n) =

N i + 1
N i+2

www.ieee.org.cn/BCZM.asp

1.
2.

123

--www.ieee.org.cn/BCZM.asp

NM

12 NM

--www.ieee.org.cn/BCZM.asp

NM
1. N = 1M121M
M/2
2. NMNM12
k 12NMNM
122kNM
NM12
12
3. NMM12
1MN1MNM

122M
12 2M FM F M
4-1 4-2

4-1

--www.ieee.org.cn/BCZM.asp
4-2

4-1 4-2

12 2M-1
FM-1

12 2M-2 FM-2
FM-1 FM-2FM-1
FM-2 FM= FM-1+FM-2F1
=1F2=2

4. 1288NM
5. pqMN

You might also like