You are on page 1of 254

......................................................................................................................................................

5
..............................................................................................................................................7
..............................................................................................................................................9
C/C++.................................................................................................................10
1.1 .............................................................................................................10
1.2 .................................................................................................................................11
1.2.1 ...........................................................................................................11
1.2.2 ...........................................................................................................11
1.2.3 ...........................................................................................................12
1.3 C/C++...................................................................................................12
1.4 .................................................................................................................................14
1.5 .............................................................................................................15
1.5.1 ...........................................................................................................15
1.5.2 ...........................................................................................................17
1.5.3 ...........................................................................................................17
1.5.4 ............................................................................................................17
1.5.5 ................................................................................................................18
1.6 .................................................................................................................................22
1.7 ..........................................................................................................................23
1.7.1 if ................................................................................................................23
1.7.2 switch ..........................................................................................................24
1.8 ..........................................................................................................................26
1.8.1 for ...................................................................................................................27
1.8.2 while .............................................................................................................28
1.8.3 do-while .......................................................................................................28
1.8.4 break .............................................................................................................29
1.8.5 continue ........................................................................................................30
1.9 .................................................................................................................................30
1.9.1 ...........................................................................................................31
1.9.2 ...........................................................................................................32
1.9.3 ...............................................................................................32
1.9.4 ...................................................................................................33
1.10 ...............................................................................................................33
1.10.1 printf .............................................................................34
1.10.2 scanf .............................................................................35
1.11 ....................................................................................................35
1.12 ...............................................................................................................................37
1.12.1 ..............................................................................................................37
1.12.2 ..............................................................................................................39
1.12.3 ......................................................................................................40
1.12.4 ..............................................................................................................41
1.13 ............................................................................................................................41

1.13.1 ..........................................................................................................42
1.13.2 .................................................................................42
1.14 ...............................................................................................................................45
1.14.1 .................................................................................................45
1.14.2 .............................................................................................................48
1.14.3 ..................................................................................................................49
1.13.4 ..................................................................................................49
1.14.5 ..........................................................................................................50
1.14.6 ......................................................................................................52
1.14.7 void...............................................................................................................53
1.14.8 ..............................................................................................................54
1.14.9 ..........................................................................................57
1.14.10 ....................................................................................................59
1.15 ...............................................................................................................................60
1.15.1 .................................................................................................60
1.15.2 .................................................................................................61
1.15.3 ..................................................................................62
1.15.4 ..............................................................................................62
1.15.5 ..............................................................................................................62
1.15.6 ..........................................................................................63
1.15.7 .........................................................................65
1.16 .......................................................................................................................66
1.16.1 fopen ..............................................................................................66
1.16.2 fclose ...........................................................................................67
1.16.3 fscanf ...............................................................................................67
1.16.4 fgetcfputc.......................................................................68
1.16.5 fgets, fputs ...............................................................69
1.16.6 freadfwrite .....................................................................70
1.16.7 fseek ............................................................................73
1.17 C.........................................................................................................75
1.17.1 ..............................................................................................................75
1.17.2 ......................................................................................................75
1.17.3 ......................................................................................75
1.17.4 ..................................................................................................76
1.18 ...................................................................................................................77
1.19 C/C++.............................................................................................................78
1.19.1 ..........................................................................................78
1.19.2 ..................................................................................................78
1.19.3 ..........................................................................................................80
1.19.4 .............................................................................................81
.....................................................................................................................83
2.1 .............................................................................................................83
2.2 .....................................................................................................84
2.3 .........................................................................................................86

ii

2.4 .....................................................................................................................88
2.5 .............................................................................................................90
.....................................................................................................................................92
...................................................................................................................95
3.1 .................................................................................95
3.2 skew.................................................................................97
.....................................................................................................................................98
.....................................................................................................................100
4.1 ...............................................................................................100
4.2 : .......................................................................................................101
4.3 : 487-3279...........................................................................................................102
4.4 : ...................................................................................................................105
4.5 : .......................................................................................................108
............................................................................................................. 113
5.1 ...........................................................................................................113
5.2 ...........................................................................................................116
5.3 ...........................................................................................................120
5.4 ...............................................................................................................122
5.5 ...........................................................................................124
...................................................................................................................................128
................................................................................................................................129
6.1 .......................................................................................................129
6.2 ...........................................................................................................131
6.3 ...............................................................................................................134
6.4 ...................................................................................................................137
...................................................................................................................................140
.....................................................................................................................144
7.1 ........................................................................................................144
7.2 ........................................................................................................146
7.3 ........................................................................................................149
7.4 ................................................................................................................152
...................................................................................................................................156
..............................................................................................................................157
8.1 ........................................................................................................................157
8.2 ........................................................................................157
8.3 ................................................................159
8.4 ....................................................................161
8.5 ................................................................................163
8.6 ............................................................................167
...................................................................................................................................171
................................................................................................................................173
9.1 ............................................................................................................173
9.2 ....................................................................................................173
9.3 ................................................................................................................175
iii

9.4 ....................................................................................................176
9.5 ................................................................................................................178
9.6 ................................................................................................................179
9.7 ........................................................................................................181
9.8 ............................................................................................................185
...................................................................................................................................188
.........................................................................................................................193
10.1 ..........................................................................................................193
10.2 ..........................................................................................197
10.3 ..............................................................................................198
10.4 Help Jimmy ......................................................................................................199
10.5 ..............................................................................................203
10.6 ..................................................................................................205
...................................................................................................................................208
............................................................................................................................ 211
11.1 ......................................................................................211
11.2 ..............................................................................214
12.3 ......................................................................................217
12.4 ......................................................................................221
12.5 ..................................................................224
.......................................................................................................................................230
.........................................................................................................................231
12.1 ..............................................................................................................232
12.2 ..............................................................................................235
12.3 ..................................................................................................................239
.......................................................................................................................................245
...............................................................................246
I. POJ.......................................................................................................246
II. POJ.........................................................................................................247
II-1 ...............................................................................................................247
II-2 ...........................................................................................247
II-3 ...........................................................................................248
II-4 ...........................................................................................................248
II-5 ...............................................................................................................248
III POJ.........................................................................248
III-1 ..................................................................................................248
III-2 .............................................................................................................248
III-3 ..........................................................................249
POJ.............................................................................................250

iv

---

"
"(Peking University Online Judge System,POJ)
http://acm.pku.edu.cn/JudgeOnline2000
ACM
A+B Problem
POJ
POJ
POJPOJ
POJ
POJPOJ
POJ

POJ

Geo-Union

ACM

p_HPF Harmonia
5

China GridCGSP
CGSP

ACM

C/C++

POJ,
2006
POJ
20 POJ
-

acm

C/C++
C/C++
.cpp C++
C++ C 90%
C
C++ C/C++
C/C++ 32

C/C++

1.1
Hello World Hello
World!
#include <stdio.h>
void main(){
printf("Hello World!\n");
}

1#include <stdio.h>
include C <stdio.h>
C
stdio.h
printf
2void main(){
printf("Hello World!\n");
}
10

main void main(){}


main
printf("Hello World!\n");
Hello Word!
\n
printf

1.2

1.2.1

int number;
numberint;

C/C++1)
2)
team TEAM

int numberOfStudents = 80;

1.2.2

number = 36 ;
36 number
int temp;
int count;
temp = 15;
count = temp;
count = count +1;
temp = count;

11

1.2.3

int total = 0 ;
int p1 = 5000 ;
int p2 = 300 ;
int p3 = 1000 ;
int p4 = 1000 ;
total = p1 + p2 + p3 + p4 ;
p1

p2

p3p4
totaltotal 7300

1.3 C/C++

int nNumber;
int nNumber nNumber
nNumber
(
)int
C/C++
int int -231 231-1 4
long int 4
shortshort 2
-215 215-1
unsigned int unsigned int 4
0 232-1
unsigned long unsigned int
unsigned short unsigned short 2
0 216-1

char char a,0 1


Ascii a Ascii 97 16 0x61

char c = a;
c 16 0x61 01100001
unsigned charunsigned char 1
float float 4
double double 8
float
int

double
shortunsigned charC++

T1 T2T1
12

T2 T1

T1 T2

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

#include <stdio.h>
int main()
{
int n1 = 1378;
short n2;
char c = 'a';
double d1 = 7.809;
double d2;
n2 = c;
//n2 97
printf("c=%c,n2=%d\n", c, n2);
c = n1;
//c b
printf("c=%c,n1=%d\n", c, n1);
n1 = d1;
//n1 7
printf("n1=%d\n", n1);
d2 = n1;
//d2 7
printf("d2=%f", d2);
return 0;
}

c=a,n2=97
c=b,n1=1378
n1=7
d2=7.000000
printf 2.10.1 printf
9 c a Ascii 97
n2 97
11 char int
int char c char 1
n1 3 n1 c
n1 1378 562 0x62c
0x62 9898 b Ascii c
b n1
13 7.809 n1 C/C++
n1 7d1

13

1.3.1: char c w
c 4 c ae
Ascii

1.4

#define
123-56038
-1 1.523.6 0.0 -0.6789 100.456 'a''p'
'0' '' '#'
abc
definitely
1234

0.6
AE4%(Ap)

#define

#define MAPLENGTH 100


#define MAPWIDTHTH 80
void main( ){
int mapSize;
mapSize = MAPLENGTH * MAPWIDTH ;
printf(The map size is %d\n, mapSize);
}
MAPLENGTH 100
MAPLENGTH 100MAPWIDTH
80 8000
C/C++
0 0123 1230987
0 8 9
0x0x12 16 12 18
0xfd06780xff44f a 10b 11
c 12d 13e 14f 15 16
4
'\'' \ '\\'

1.4:

14

1.5
C/C+++
- * /

n+5

4-3+1

4-3+1 2 f f
f
/C++

1.5.1
(+)(-)(*)(/)(%)(++)
(--)

1.5.1.1
%
a % b a b

1.5.1.2
C/C++ ab
a/b a b 5/2 2 2.5

1. int main()
2. {
3.
int a = 10;
4.
int b = 3;
5.
double d = a/b;
6.
printf("%f\n", d);
7.
d = 5/2;
8.
printf("%f\n", d);
9.
d = 5/2.0;
10.
printf("%f\n", d);
11.
d = (double)a/b;
12.
printf("%f\n", d);
13.
return 0;
14. }

3.000000
2.000000
15

2.500000
3.333333
5 ab a/b 3 d
3.0
7 5 d 2.0
9 5 2 5 2
5/2.0
2.5
11 a b
(double)
double
a b

1.5.1.3
++ 1

1:
++;
2
++;
1
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
main()
{
int n1 , n2 = 5;
n2 ++;
++ n2;
n1 = n2 ++;
n1 = ++ n2;
}
5 n2 6
6 n2 7
7 n2 n1 n2 7
n1 7n2 8n2++ n2 1
8 n2 1 n2 n1 8
n1 9n2 9++n2 n2 1
7 8 ++
-- 1++

16

1.5.2
(=)(+=, -=, *=, /=, %=)
(&=, |=, ^=, >>=, <<=)
a = b a a
int a, b;
a = b = 5;
b 5 b=5 5 a
a += b a = a + b
-=*=/=%= +=

1.5.3
(>)(<)(==) (>=)
(<=)(!=)
0 00 0

3>5 03==3
0 0 C/C++
C/C++ 0
0 2.7

main()
{
int n1 = 4, n2 = 5, n3;
n3 = n1 > n2;
// n3 0
n3 = n1 < n2;
// n3 0
n3 = n1 == 4;
// n3 0
n3 = n1 != 4;
// n3 0
n3 = n1 == 5;
// n3 0
}

1.5.4
(&&)(||)(!)

exp1 exp2 0
exp1 && exp2

exp1 && exp2 n = 4n > 4 && n < 5

n >= 2 && n < 5


exp1 exp2 0exp1 || exp2
exp1 || exp2 n = 4n > 4 || n < 5
n <= 2 || n > 5
17

exp ! exp exp ! exp


! (4 < 5 )

1.5.5
bit
1C/C++
C/C++
&

<<

>>

longintshortunsigned int

1.5.5.1
"&"
1 1 0
21 & 18 16( 10000)
21
0000 0000 0000 0000 0000 0000 0001 0101
18 :
0000 0000 0000 0000 0000 0000 0001 0010
0000 0000 0000 0000 0000 0000 0001 0000
0
int n 8 0
n = n & 0xffffff00;

n &= 0xffffff00;
n short
n &= 0xff00;
int n 7 0 1
n & 0x80 0x80

1.5.5.2
"|"
0 0 1
21 | 18 23( 10111)
1 int
n 8 1
n |= 0xff;

18

1.5.5.3
"^"
1 0
21 ^ 18 7( 111)
a^b=c c^b == a c^a==b

2.5.5.3

1.5.5.4
"~" 0 11 0
~21 0xffffffea
printf("%d,%u,%x", ~21, ~21, ~21);
:
-22,4294967274,ffffffea

1.5.5.5
<<
0

9 32
0000 0000 0000 0000 0000 0000 0000 1001
9<<4 4
0000 0000 0000 0000 0000 0000 1001 0000
144
n

1 2 n 2

1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
main()
{
int n1 = 15;
short n2 = 15;
unsigned short n3 = 15;
unsigned char c = 15;
19

8.
9.
10.
11.
12.
13. }

n1 <<= 15;
n2 <<= 15;
n3 <<= 15;
c <<= 6;
printf( "n1=%x,n2=%d,n3=%d,c=%x,c<<4=%d", n1, n2, n3, c, c << 4);

:
n1=78000,n2=-32768,n3=32768,c=c0,c<<4=3072
12 printf 2.10.1 printf
8 n1 15 32 n1 n1
0x78000
9 n2 15 n2 short 16
0000 0000 0000 1111 15 ()3 1
n2 1000 0000 0000 0000 n2 short n2
1 n2 12 32768
10 n3 15 n3 1000 0000 0000 0000
n3 12 n3 32768
11 c 6 c unsigned char 8
00001111 6 11000000 12 16 c0
12 c<<4 c int
(32 16 0000 0000 0000 00c0 ) 4
0000 0000 0000 0c00 3072
c<<4 c c+4
c

1.5.5.6
>>>>
>>
longintshortchar
C/C++ 1
1 0 0
unsigned longunsigned int unsigned short unsigned char
0

1. #include <stdio.h>
2. main()
3. {
4.
int n1 = 15;
5.
short n2 = -15;
6.
unsigned short n3 = 0xffe0;
7.
unsigned char c = 15;
20

8.
n1 = n1>>2;
9.
n2 >>= 3;
10.
n3 >>= 4;
11.
c >>= 3;
12.
printf( "n1=%x,n2=%d,n3=%x,c=%x", n1, n2, n3, c);
13. }

n1=3,n2=-2,n3=ffe,c=1
8 n1 0xf 2 0x3
9 n2 16 1111 1111 1111
0001 1 1
1111 1111 1111 1110 16 -2
10 n3 16 0xffe0 1
0 4 n3 0xffe
11c 0xf 3 1
n

n 2
1.5.5.6 int a n(0 <= n <= 31)
a n

2.5.5.7 sizeof
sizeof C/C++

sizeof()
sizeof(n) n n short
sizeof(n) 2

sizeof()
sizeof(int) 4 int 4

1.5.5.8

()
(int) (double)(char)

1.
2.

double f = 9.14
int n = (int) f ;
21

3.
4.

f=n/2;
f = (double) n / 2 ;

2 f 9.14 int 9 n
(int)

3 f 4.0 n/2 4
4 (double) n 2
f 4.52.5.1.2
11

1.5.5.9

4 & 2 + 5+& 2 + 5
4 & 7 4
( 4 & 2 ) + 5 5 4 & 2

.[]()

+-

>>

<< >>=

<

<=

==

!=

&

10

11

&&

12

||

14

- ~ ! ++ --
sizeof
/

%
<<=

> >=

1.6

22


C++ /**/

/* mp3
author : Guo Wei
programed on 2004.5.18
*/
main() {
int nBitrate; /* kb */
int nSize;
/* */

////

main() {
int nBitrate; // kb
int nSize;
//

1.7
C/C++

if
switch

1.7.1

if

if
if ( ) /
/
/
23

if ( ) / 1 else / 2
/ 1 / 2
,/ 1 /
2
if :
if ( i > 0 )
y = x / i;
else {
x = i;
y = - x;
}
i, x, y i 0, y x/i i
0, x iy -x if

if else
if
if ( i > 0 )
if ( j > i )
x = j;
else
x = i;

/* */

else if
if ( i > 0 ) {
if ( j > i )
x = j;
}
else
x = i;

1.7.2

/* */

switch

switch case switch


switch ( ){
case 1 : / 1
case 2 : / 2
24


default : / n
}
switch case , case
switch case
case /
switch
break break
default switch
case
case / case /
break switch
break switch
switch ( ){
case 1 : / 1break
case 2 : / 2break

default : / n
}
default switch
default case
switch case if case

switch :
switch( c ) {
case 'A':
capa++;
case 'a':
lettera++;
default :
total++;
}

25

break c 'A' switch


c 'a' lettera total 1 c 'a''A'
total 1 break
switch( i ) {
case -1:
n++;
break;
case 0 :
z++;
break;
case 1 :
p++;
break;
}
break
switch i 1, n 1 i 0, z 1 i
1, p 1 break

case
case
case
case
case
case

'a' :
'b' :
'c' :
'd' :
'e' :
'f' :
x++;

'a' 'f', x 1

1.8
n

for; while; do while; break; continue;

26

1.8.1 for
for for
for
for
for ( ; ; ) /
for :
1.


2.

, /


, for

break return
, for

break, goto, return for continue


for break for
break return

for( )
for
for ( i = n2 = n3 = 0 i <= 100 i++ ) {
if ( i % 2 == 0 )
n2 ++
else if (i % 3 == 0 )
n3 ++
}
0 100 0
3 i n2 n3 0 i 100 for
i n2 1 n3 1
i++ i 100 for i
100
27

1.8.2 while
while

while ( ) /
while
1.

2.
while while

3.
/ 1
while :
int i = 100;
int sum = 0;
while ( i > 0 ) {
sum = sum + i*i
i--;
}
1 100 sum i 0
i 0 sum i i 1
i 0 i 0 while

1.8.3 do-while
do-while

do / while ( ) ;
do-while / do /
do-while

1.
do /
do-while
2.
1 do-while
do-while break, goto, return
28

do-while :
int i = 100
int sum = 0
do {
sum = sum + i*i
i--
} while ( i > 0 );
do-while while 1 100
i sum do-while i sum
i 1 i 0 i 0 do
do-while

1.8.4 break
break doforswitch while

break;
break :
for ( i = 0; i < 10; i++ ){

/* Execution returns here when


break

statement is executed */
for
(
j
=
1;
j++) {
if ( i+j % 5 ==0 ) {
printf(i= %d j=%d\n, i, j);
break;
}
}
}

<=

5;

i 0 9 j 1 5 j i+j 5
i j j i

1
2
3
4
5

4
3
2
1
5
29

6
7
8
9

4
3
2
1

1.8.5 continue
dofor while continue
continue
continue;
dofor while
do while ,
for

continue :
int i=100;
int x=0;
int y=0;
while ( i > 0 ) {
i--;
x = i % 8;
if ( x == 1 )
continue;
y = y + x;
}
i 99 0 8 1 8
while i 0 i 0
i 1 i 8 x, if x 1 x 1
while i 0 x 1 x y
x 0

1.9
C/C++
(
)

30

1.9.1

[ 1 1 2 2]{
1 //
2 //

return // void
}

int add(int x, int y){


return x+y;
}
add x y

x y

int multiple(int x, int y); //


void main(){
int a=0, b=0;
scanf(%d %d, &a, &b);
printf(%d\n, multiple(a, b)); //
}
int multiple(int x, int y){
//
return x * y;
}
31

1.9.2

int add(int x, int y){


return x+y;
}
void main(){
int n1=5, n2=6, n3;
n3=add(n1, n2);
printf(%d\n, n3);
}
add n1 n2 n3 n3

n3 = add(n1, n2) + 7

1.9.3
A A
B B A
A B B
A

1.9.3.1

int max(int a, int b){


if(a>=b) return a;
else return b;
}
void main( ){
int x=0, y=0, z=0;
x = 20;
y = 45;
int z = max(x, y);

}
32

x, y, z
x 20y 45
=
max max a, b

45 20 main
max b 45 return mainmain
max 45 z z 45
max ab xy

1.9.3.2

int min(int x, int y)


// int min
double calculate( int a, double b);
// double double calculate
char judge(void);
// char judge
void doit(int times);
// void doit

1.9.4
C/C++
C/C++ C/C++
C/C++ printf
C/C++

#define .h
#include
printf stdio.h

#include <stdio.h>

1.10
C/C++
stdio.h

33

printf
scanf

1.10.1 printf
printf printf

printf( 1 2)

printf( x = %d , 50);
x = %d 50
x = 50
%d%

%d
%
%d
%c
%s
%x
%u
%f

printf(Name is %s, Age = %d, weight = %f kg, %c, code = %x ,


Tom, 32, 71.5, M, 32 );

Name is Tom, Age = 32, weight = 71.500000 kg, M, code = 20


32 20%x
3220

%d%
%

printf(a%%d);

a%d
\n
printf(Whats up?\nGreat!\nLets go!);

Whats up?
34

Great!
Lets go!

1.10.2 scanf
scanf
scanf( 1, 2);
scanf
&

#include <stdio.h>
main()
{
char c;
int n;
scanf("%c%d",&c, &n);
printf("%c,%d", c, n);
}
scanf %c%d
scanf
scanf
c n t456 t 456 t
456
t,456
t c 456 n

#include <stdio.h>
main()
{
int nHeight, nWidth;
printf("Please enter the height:\n");
scanf("%d",& nHeight);
printf("Please enter the width:\n");
scanf( "%d",& nWidth);
printf("The area is: %d", nHeight * nWidth);
}

1.11

35

2.11.cpp
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.

int n1 = 5, n2 = 10;
void Function1()
{
int n3 =4;
n2 = 3;
}
void Function2()
{
int n4;
n1 = 4;
n3 = 5; //
}
int main()
{
int n5;
int n2;
if( n1 == 5 ) {
int n6;
n6 = 8;
}
n1 = 6;
n4 = 1; //
n6 = 9;
//
n2 = 7;
return 0;
}

n1n2 510
21n3 Function1 11
22

19 n6
19 20 23 n6

16 n2 n2
24 n2 n2

36

1.12
1.12.1

100
100
100 int n1, n2, n3 n100 100

C/C++
C/C++

[];

int an[100];
an 100 int
an 100

T array[ N ]; // T chardoubleint N
//
arrayarray N
T N array
N sizeof(T)

[]

array ij int

array[5]
array[i+j]
array[i++]

C/C++ 0 :
T array[N];
37

array[N] N array[0], array[1],


array[2]array[N-1]array[i](i ) T array[0]
n, array[i] n + i * sizeof(T)
100
100

N N (
0 ) 0 N-1 1
N-2 2
2.12.1.cpp
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

#include <stdio.h>
#define MAX_NUM 100
int main()
{
int i, j;
int an[MAX_NUM];
// 100
for( i = 0;i < MAX_NUM ;i ++ )
scanf("%d", & an[i]);
//
for( i = 0; i < MAX_NUM - 1; i ++ ) { // i i
int nTmpMin = i;
// i MAX_NUM-1
//
for( j = i; j < MAX_NUM; j ++ ) {
if( an[j] < an[nTmpMin] )
nTmpMin = j;
}

19.
20.
21.
22.
23.
24.
25.
26.
27. }

// i i i
//
int nTmp = an[i];
an[i] = an[nTmpMin];
an[nTmpMin] = nTmp;
}
// 100
for( i = 0;i < MAX_NUM ;i ++ )
printf("%d\n", an[i]);
return 0;

38

1.12.1

C/C++

1.12.2

C/C++

T array[N][M]; // T char , double, int MN


//
arrayarray NM
T NM
array NMsizeof(T)
array
[][]
0
array N M M i
array[i][0]array[i][1]array[i][M-1]
j array[0][j]array[1][j]array[N-1][j]
array[0][0] array[0][0] n
array[i][j](i,j ) n + i * M * sizeof(T) + j * sizeof(T)
int a[2][3] a[0][0]
100 a[0][1] 104

1-12-2
int
a[2][3]a[0]a[1]
N M N M

39

#define ROW 20
#define COL 30
int a[ROW][COL];
for( int i = 0; i < ROW -1 ; i ++) {
for( int j = 0; j < COL -1 ; j ++ )
printf(%d , a[i][j]);
printf(\n);;
}
a 0 1
2
1.12.2 a 0 1

1.12.3

[]={}
{ }

int a[10]={ 0,1,2,3,4,5,6,7,8,9 };


a[0]=0;a[1]=1...a[9]=9;
{ }
0

int a[10]={0,1,2,3,4};
a[0]a[4]5 5 0

int a[]={1,2,3,4,5};
a 5
int a[5][3]
int a[5][3]={ {80,75,92},{61,65,71},{59,63,70},{85,90},{76,77,85} };
{}, {80,75,92} 0
a[0][0] = 80, a[0][1] = 75, a[0][2] = 92

40

1.12.4

1.
2.
3.
4.
5.

int an[10];
an[-2] = 5;
an[200] = 10;
an[10] = 20;
int m = an[30];

a[-2]
a n a[-2] n + (-2) * size(int) int
n-8 int 2
n 8 5 4
n 8

n-8
2 2
2

345 an 10 an[0]
an[9]an[10] an 5
m
an[30] 5
an[-2] = 5

if

1.13
C/C++
"CHINA" C program
41

\0

C/C++
2.17.3

1.13.1
"CHINA" C program
"$12.5" a
1
\0 \0 Ascii 0C/C++
\0

"C program"
C

\0

1-13-1-1
""
\0
\"\
\\

printf( "He said: \"I am a stu\\dent.\"");

He said: "I am a stu\dent."

1.13.2

char szString[10];

\0
1
C/C++

2.13.2.cpp
1.

#include <stdio.h>
42

2. #include <string.h>
3. int main() {
4.
char szTitle[] = "Prison Break";
5.
char szHero[100] = "Michael Scofield";
6.
char szPrisonName[100];
7.
char szResponse[100];
8.
printf("What's the name of the prison in %s?\n", szTitle);
9.
scanf( "%s", szPrisonName);
10.
if( strcmp( szPrisonName, "Fox-River") == 0 ) {
11.
printf("Yeah! Do you love %s?\n", szHero);
12.
}
13.
else {
14.
strcpy( szResponse, "It seems you haven't watched it!\n");
15.
printf( szResponse);
16.
}
17.
szTitle [0] = 't';
18.
szTitle [3] = 0; // szTitle [3] = \0;
19.
printf(szTitle);
20.
return 0;
21. }
4: szTitle 13
"Prison Break"\0 szTitle
P

\0

1-13-1-2
5: 100 szHero 17
Micheal Scofield\0

8
What's the name of the prison in Prison Break?
9: szPrisonName
\0 99 \0
scanf %s
scanf
Fox River szPrisonName Fox
Fox River

szPrisonName 9
gets( szPrisonName);
Fox River szPrisonName Fox
River
gets
char *gets(char *s);
43

s s
\0
10 string.h strcmp
0
11: :
Yeah! Do you love Michael Scofield?
14: strcpy "It seems you haven't watched it!"
szResponse
\0
15:
It seems you haven't watched it!
1718 szTitle
t

\0

\0

1-13-1-3
19 C/C++\0

tri

char szFriends[6][30] = { "Joey", "Phoebe", "Monica", "Chandler", "Ross",


"Rachel" };

printf(szFriends[0]);
:
Joey
printf(szFriends[5]);
:
Rachel

1.13.3
int MyItoa( char * s ) ;
s s
1234 1234 s
s s

44

1.14
1.14.1

* ;
int * p;
char * pc;
float * pf;

// p p int *
// pc , pc char *
// pf pf float *

10000
int * p = ( int * ) 10000;
p 10000
p 10000
p p
p 10000
* p * p p

* p = 5000; // 10000 5000


int n = * p; // 10000 n
// n = 5000
* p
int

int * p 4 sizeof(int) = 4

T * p ; // T int, double ,char


//T
p ( )p T ** p
T* p
p sizeof(T)

* p p T
45

* p*

T sizeof(T *) 4
4
32
CPU 4G 2 32 4
64 C/C++ 8
int * p = (int *) 10000

10000 10000
T x T *
p( p x)* p p x* p
x
char
char
* pc
char
pc =

ch1 = A;
* pc = &ch1;
= B;
ch2 = * pc;
& ch2;

* pc = D;

//
//
//
//
//
//
//

(1)
(2) pc ch1
(3) ch1 = B
(4) ch2 = ch1
(5) pc ch2

6 ch2 = D

(2) ch1 pc
pc ch1
&

T x&x x &x
T *
(3) pc b pc
ch1
* pc ch1(3) ch1 b
(4) * pc ch1 4 ch1 ch2

BasicJava C/C++
C/C++

swap swap(a, b) a, b
C C++
a, b int swap
void swap(int n1, int n2)
46

{
int nTmp = n1;
n1 = n2;
n2 = nTmp;
}
swap(a, b) ab n1
n2 ab n1, n2 ab
swap C
void swap( int * pn1, int * pn2)
{
int nTmp = * pn1; // pn1 nTmp
* pn1 = * pn2;
// pn2 pn1
* pn2 = nTmp;
// nTmp pn2
}
int ab
swap(& a, & b);
& a a ( int * )swap pn1
a pn1 a* pn1 a, pn2 b, * pn2
b ab

int * pn, char * pc, char c = 0x65;


pn = pc;
//(1)
pn = & c;
//(2)
pn = (int * ) & c;
//(3)
int n = * pn;
//(4)
* pn = 0x12345678;
//(5)
(1)(2)
int * char * 3
& c char * pn (3)
pn c

1.14.1(4) n 0x65 5

47

1.14.2

1)
2
3
4
5
p1p2 p1< p2
p1<p2p1>p2, p1==p2
T * p1 p2p1 p2
int p1 p2 T

p1 p2 = ( p1 p2 ) / sizeof( T)
p T * n
p + n T *
p + n sizeof(T)
n+pp+n
p T * n
p - n T *
p - n sizeof(T)
*( p + n) , * (p n)

1.14.2.1 p T * p++ ++pp----p

2.14.2.cpp
1.

#include <stdio.h>
48

2. int main()
3. {
4.
int * pn1, * pn2;
5.
int n = 4;
6.
char * pc1, * pc2;
7.
pn1 = (int *) 100;
// pn1 100
8.
pn2 = (int *) 200;
// pn2 200
9.
printf("%d\n", pn2 - pn1);
// 25, (200-100)/sizeof(int) = 100/25 = 4
10.
pc1 = (char * ) pn1;
// pc1 100
11.
pc2 = (char * ) pn2;
// pc2 200
12.
printf("%d\n", pc1 - pc2 );
// -100,(100-200)/sizeof(char) = -100
13.
printf("%d\n", (pn2 + n) - pn1); // 29
14.
int * pn3 = pn2 + n;
// pn2 + n pn3
15.
printf("%d\n", pn3 - pn1);
// 29
16.
printf("%d", (pc2 - 10) - pc1);
17.
return 0;
18. }
13 pn2 + n int *
pn2 + n sizeof(int) = 200 + 4 4 216
pn2 + n- pn1 int *
(216-100)/sizeof(int) = 116/4 = 29

1.14.2.2 16

1.14.3
C/C++NULL NULL
0 0

int * pn = NULL; char * pc = NULL;

1.13.4

T * p;

49

T char *, int *

int * * p;
p p int **
* p
int * p p
int * p int *

T ** p; // T
p p T *** p
T ** p T *
int *** p; int **** p; int ***** p ; *
T sizeof(T *) 4
4

int * array[5];
array int *

1.14.5

T array[N]
array T * array T *
array array
int array[5];
array int *

int * array[5];
array int **

1.
2.
3.

2.14.5.1.cpp
#include <stdio.h>
int main()
{
50

4.
int an[200];
5.
int * p ;
6.
p = an;
// p an p an[0]
7.
* p = 10;
// an[0] = 10
8.
*( p + 1 ) = 20;
// an[1] = 20
9.
p[0] = 30;
// p[i] *(p+i) an[0] = 30
10.
p[4] = 40;
// a[4] = 40
11.
for( int i = 0;i < 10;i ++)
// an 10
12.
*( p + i) = i;
13.
p++;
// p a[1]
14.
printf("%d\n", p[0]);
// a[1] 1p[0] * p
15.
p = an + 6;
// p a[6]
16.
printf("%d\n", * p);
// 6
17.
return 0;
18. }

1
6
8p+1 int *
p+sizeof(int) p a[0] p+1 a[1]
9 p[i] *(p+i) C/C++
p
BubbleSort

2.14.5.2.cpp
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

#include <stdio.h>
void BubbleSort( int * pa, int nNum)
{
for( int i = nNum -1 ; i > 0; i -- )
for( int j = 0; j < i; j ++)
if( pa[j] > pa[j+1] ) {
int nTmp = pa[j];
pa[j] = pa[j+1];
pa[j+1] = nTmp;
}
}
#define NUM 5
int main()
{
int an[NUM] = {5,4,8,2,1 };
BubbleSort(an, NUM);

// an
51

17.
for( int i = 0;i <NUM;i ++ )
18.
printf("%d\n", an[i]);
19.
return 0;
20. }
pa[0] pa[1]
pa[0]>pa[1] pa[0] pa[1] pa[1] pa[2] pa[1]>pa[2]
pa[1] pa[2] pa[nNum-2] pa[nNum-1]
pa[nNum-2]>pa[nNum-1] pa[nNum-2] pa[nNum-1]

BubbleSort void BubbleSort( int pa[], int nNum)

T array[M][N];
array[i](i ) array[i] T *array[i]
array + iN array array[0]

int array[4][5];

BubbleSort( array[1], 5);


array 1 BubbleSort(array[0], 3) 0 3

1.14.5 int

1.14.6
char * char *
char *
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
#include <string.h>
int main() {
char * p = "Tom \n";
char szName[20];
char * pName = szName;
scanf( "%s", pName);
printf( p);
52

9.
10.
11. }

printf( "Name is %s", pName);


return 0;

Jack
Tom
Name is Jack
printf, scanf %s
char *
7 pName szName
19 szName

char * p;
scanf( %s, p);
scanf p p

1.14.7 void
void * p;
p, void * void
void
double d = 1.54;
void * p = & d;
sizeof(void) void * p, * p
p
void
void C/C++
void *memcpy(void *dest, const void *src, unsigned int n);
string.h mem.h src n
dest dest
a1 a2 a2[0] = a1[0], a2[1]
= a1[1]a2[9] = a1[9]
int a1[10];
53

int a2[10];
memcpy( a2, a1, 10*sizeof(int));
MyMemcpy
void * MyMemcpy( void * dest , const void * src, int n)
{
char * pDest = (char * )dest;
char * pSrc = ( char * ) src;
for( int i = 0; i < n; i ++ ) { //
* (pDest + i) = * ( pSrc + i );
}
return dest;
}
1.14.7 MyMemcpy

1.14.8

()

(* )( 1, 2,);

( 1, 2,)

int (*pf)(int , char);


pf int
int char

();

54

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

#include <stdio.h>
void PrintMin(int a, int b)
{
if( a<b )
printf("%d", a);
else
printf("%d", b);
}
int main(){
void (* pf)(int , int);
// pf
int x = 4, y = 5;
pf = PrintMin;
// PrintMin pf
pf(x, y);
// pf PrintMin
return 0;
}

4
C/C++ qsort stdlib.h
void qsort(void *base, int nelem, unsigned int width,
int ( * pfCompare)( const void *, const void *));

base
nelem width
pfCompare
qsort
qsort pfCompare

C/C++ qsort qsort ,
pfCompare

qsort
int (const void * elem1, const void * elem2);
elem1 elem2 * elem1
* elem2
1) * elem1 * elem2

2) * elem1 * elem2 0
3) * elem1 * elem2

qsort unsigned int


55

82315 23158
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

#include <stdio.h>
#include <stdlib.h>
int MyCompare( const void * elem1, const void * elem2 )
{
unsigned int * p1, * p2;
p1 = (unsigned int *) elem1;
p2 = (unsigned int *) elem2;
return (* p1 % 10) - (* p2 % 10 );
}
#define NUM 5
int main()
{
unsigned int an[NUM] = { 8,123,11,10,4 };
qsort( an, NUM, sizeof(unsigned int), MyCompare);
for( int i = 0;i < NUM; i ++ )
printf("%d ", an[i]);
return 0;
}

10 11 123 4 8

qsort
MyCompare 0 qsort
0 0
6 elem1 const void * void
*elem1elem1 unsigned int
elem1 p1* p1
7
8 *p1 *p2

1.14.8.1 an MyCompare

1.14.8.2 qsort MySort


qsort MySort MySort

56

1.14.9
/C++

int n;
int a[n]; //

++

C++
newnew
P = new T;
T P T *
sizeof(T) P

int * pn;
pn = new int; //(1)
* pn = 5;
(1) 4 pn pn,

new
P = new T[N];
T P T * N
N sizeof(T)
P

int * pn;
int i = 5;
pn = new int[i * 20];
pn[0] = 20;

57

pn[100] = 30; //(1)


1
100 pn[100]

new NULL
int * pn = new int[200000];
if( pn == NULL )
printf( );
else
printf( );

C++ delete

delete
delete

int * p = new int;


* p = 5;
delete p;
delete p;
//
delete 4
delete p p

int * p = new int;


int * p2 = p;
delete p2;
delete p1;
delete delete
new
delete

58

delete [] ;

int * p = new int[20];


p[0] = 1;
delete [] p;
delete p
delete

new delete

1.14.10
bug bug
NULL

char * p;
scanf( %s, p);

// p

p scanf p

59

1.15
1.15.1

C/C++
Student Student
Worker Worker

C/C++
struct

struct {


struct Student {
unsigned ID;
char szName[20];
float fGPA;
};
Student

Student Student 3
ID unsigned szName
fGPA float

Student struct

60

1.15.2
C++
;

struct Student {
unsigned ID;
char szName[20];
float fGPA;
};

Student stu1, stu2;


stu1 stu2 Student
struct Student {
unsigned ID;
char szName[20];
float fGPA;
} stu1, stu2;
stu1, stu2 Student
stu 1
stu1 = stu2;

sizeof(Student) = 28
Student 28

struct Date {
int nYear;
int nMonth;
int nDay;
};
StudentEx :
struct StudentEx {
unsigned ID;
char szName[20];
float fGPA;

61

Date Birthday;
};
StudentEx StudentEx

1.15.2: StudentEx

1.15.3

.
StudentEx
StudentEx stu;
scanf(%f, & stu.fGPA);
stu.ID = 12345;
strcpy stu.szName, Tom;
printf(%f, stu.fGPA);
stu.Birthday.nYear = 1984;
unsigned * p = & stu.ID; //p stu ID

1.15.4
StudentEx

StudentEx stu = { 1234,Tom,3.78,{ 1984,12,28 }};


stu 1234Tom
3.78 1984
12 28

1.15.5

[];

62

StudentEx MyClass[50];
50
StudentEx MyClass StudentEx *

StudentEx MyClass[50] = {
{ 1234,Tom,3.78,{ 1984,12,28 }},
{ 1235,Jack,3.25,{ 1985,12,23 }},
{ 1236,Mary,4.00,{ 1984,12,21 }},
{ 1237,Jone,2.78,{ 1985,2,28 }}
};
MyClass 46
0
MyClass
MyClass[1].ID = 1267;
MyClass[2].Birthday.nYear = 1986;
int n = MyClass[2].Birthday.nMonth;
scanf(%s, MyClass[0].szName);

1.15.6

* ;

StudentEx * pStudent;
StudentEx Stu1;
pStudent = & Stu1;
StudentEx Stu2 = * pStudent;

->

(* ).

pStudent->ID;

(* pStudent).ID;
StudentEx
StudentEx Stu;
StudentEx * pStu;
63

pStu = & Stu;


pStu->ID = 12345;
pStu->fGPA = 3.48;
printf(%d, Stu.ID);
printf( %f, Stu.fGPA);

// 12345
// 3.48

ps ps 0 ps+1 1
ps+i i

void PrintStudentInfo( StudentEx Stu);


StudentEx Stu1;
PrintStudentInfo( Stu1);
PrintStudentInfo Stu Stu1
StudentEx
4

void PrintStudentInfo( StudentEx * pStu);


StudentEx Stu1;
PrintStudentInfo( & Stu1);
PrintStudentInfo pStu Stu1 pStu
Stu1
qsort Student

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM 4
struct Student {
unsigned ID;
char szName[20];
float fGPA;
};
Student MyClass[NUM] = {
{ 1234,"Tom", 3.78},
{ 1238,"Jack",3.25},
64

13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.

{ 1232,"Mary",4.00},
{ 1237,"Jone",2.78}
};
int CompareID( const void * elem1, const void * elem2)
{
Student * ps1 = (Student *) elem1;
Student * ps2 = (Student *) elem2;
return ps1->ID - ps2->ID;
}
int CompareName( const void * elem1, const void * elem2)
{
Student * ps1 = (Student *) elem1;
Student * ps2 = (Student *) elem2;
return strcmp( ps1->szName, ps2->szName);
}
int main()
{
int i;
qsort( MyClass, NUM, sizeof(Student), CompareID);
for( i = 0;i < NUM;i ++)
printf("%s ", MyClass[i].szName);
printf("\n");
qsort( MyClass, NUM, sizeof(Student), CompareName);
for( i = 0;i < NUM;i ++)
printf("%s ", MyClass[i].szName);
return 0;
};

Mary Tom Jone Jack


Jack Jone Mary Tom

1.15.7

StudentEx * pStu = new StudentEx;


pStu->ID = 1234;
delete pStu;
pStu = new StudentEx[20];
pStu[0].ID = 1235;
delete [] pStu;

65

1.16
,
C/C++stdio.h

1.16.1 fopen

FILE *fopen(const char *filename, const char *mode);


FILE stdio.h

r :
w:
a:
rb :
wb:
ab:
r+ :
r+b:

fopen FILE * FILE

fopen

, fopenNULL

FILE * fp = fopen( c:\\data\\report.txt, r);


c:\\data\\report.txt
fopen
FILE * fp = fopen( report.txt, r);
report.txtfopenNULL

66

fopenNULL

1.16.2 fclose
fclosefclose
int fclose(FILE *stream);
streamfopen

fopen

fclosestreamNULL,

1.16.3 fscanf
fscanf;
int fscanf(FILE *stream, const char *format[, address, ...]);
fscanfscanf----streamscanf
fscanfstream
fscanf EOFstdio.h

students.txt Ctmp:
Tom 08701342 male 1985 11 2 3.47
Jack 08701343 Male 1985 10 28 3.67
Mary 08701344 femal 1984 2 28 2.34

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

#include <stdio.h>
int main()
{
FILE * fp;
fp = fopen( "c:\\tmp\\students.txt", "r");
if( fp == NULL ) {
printf( "Failed to open the file.");
return ;
67

9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20. }

}
char szName[30], szGender[30];
int nId, nBirthYear, nBirthMonth, nBirthDay;
float fGPA;
while( fscanf( fp, "%s%d%s%d%d%d%f", szName, & nId, szGender, & nBirthYear,
& nBirthMonth, & nBirthDay, & fGPA) != EOF) {
printf( "%s %d %s %d %d %d %f\r\n", szName, nId, szGender, nBirthYear,
nBirthMonth, nBirthDay, fGPA);
}
fclose(fp);
return 0;

fprintfprintffscanf

int fprintf(FILE *stream, const char *format[, argument, ...]);

1.16.4 fgetcfputc
fgetc:
int fgetc(FILE *stream);

80-255fgetc
fgetcEOF(
-1)
fputc
int fputc(int c, FILE *stream);
ccint
8EOF
MyCopy.exe
DOS MyCopy 1 2
DOS
MyCopy c:\tmp\file1.dat d:\tmp2.dat
Ctmpfile1.datd
tmp2.dat
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main(int argc, char * argv[])
{
FILE * fpSrc, * fpDest;
fpSrc = fopen( argv[1], "rb");
if( fpSrc == NULL ) {
68

7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22. }

printf( "Source file open failure.");


return 0;
}
fpDest = fopen( argv[2], "wb");
if( fpDest == NULL) {
fclose( fpSrc);
printf( "Destination file open failure.");
return 0;
}
int c;
while( ( c = fgetc(fpSrc)) != EOF)
fputc( c, fpDest);
fclose(fpSrc);
fclose(fpDest);
return 0;

2 main argc argv 5 10


argv argc argv 2.18

5 10
17 c = fgetc(fpSrc) c
fgetc fgetc EOF

1.16.5 fgets, fputs


fgets;
char *fgets(char *s, int n, FILE *stream);
s
\0ns(\0)
fgetschar *s
NULL
fputs;
int fputs(const char *s, FILE *stream);
ss
students.txt student2.txt

1.
2.
3.
4.
5.

#include <stdio.h>
#define NUM 200
int main()
{
FILE * fpSrc, * fpDest;
69

6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24. }

fpSrc = fopen( "students.txt", "r");


if( fpSrc == NULL ) {
printf( "Source file open failure.");
return 0 ;
}
fpDest = fopen( "students2.txt", "w");
if( fpDest == NULL) {
fclose( fpSrc);
printf( "Destination file open failure.");
return 0;
}
char szLine[NUM];
while( fgets(szLine, NUM-1, fpSrc)) {
fputs(szLine, fpDest);
}
fclose(fpSrc);
fclose(fpDest);
return 0;

fgets 199150

1.16.6 freadfwrite
fread
unsigned fread(void *ptr, unsigned size, unsigned

n, FILE *stream);

nsize, n*size
ptr 0
fwrite
unsigned fwrite(const void *ptr, unsigned size, unsigned

n, FILE *stream);

ptr n*size
size

size1n

freadfread
100125050

freadfwrite
C/C++
70

freadfwritestudents.dat

struct Student{
char szName[20];
unsigned nId;
short nGender;

//

short nBirthYear, nBirthMonth, nBirthDay;


float fGPA;
};
students.txt
students.datstudents.dat1985
students2.dat

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

#include <stdio.h>
#include <string.h>
struct Student{
char szName[20];
unsigned nId;
short nGender; //
short nBirthYear, nBirthMonth, nBirthDay;
float fGPA;
};
int main()
{
FILE * fpSrc, * fpDest;
struct Student Stu;
fpSrc = fopen( "c:\\tmp\\students.txt", "rb");
if( fpSrc == NULL ) {
printf( "Failed to open the file.");
return 0;
}
fpDest = fopen( "students .dat", "wb");
if( fpDest == NULL) {
fclose( fpSrc);
printf( "Destination file open failure.");
return 0;
}
char szName[30], szGender[30];
int nId, nBirthYear, nBirthMonth, nBirthDay;
float fGPA;
71

29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62. }

while( fscanf( fpSrc, "%s%d%s%d%d%d%f", szName, & nId,


szGender, & nBirthYear, & nBirthMonth, & nBirthDay, & fGPA) != EOF) {
strcpy(Stu.szName, szName);
Stu.nId = nId;
if( szGender[0] == 'f' )
Stu.nGender = 0;
else
Stu.nGender = 1;
Stu.nBirthYear = nBirthYear;
Stu.nBirthMonth = nBirthMonth;
Stu.nBirthDay = nBirthDay;
fwrite( & Stu, sizeof(Stu), 1, fpDest);
}
fclose(fpSrc);
fclose(fpDest);
fpSrc = fopen( "students.dat", "rb");
if( fpSrc == NULL ) {
printf( "Source file open failure.");
return 0;
}
fpDest = fopen( "students2.dat", "wb");
if( fpDest == NULL) {
fclose( fpSrc);
printf( "Destination file open failure.");
return 0;
}
while(fread( & Stu, sizeof(Stu), 1 , fpSrc)) {
if( Stu.nBirthYear >= 1985 )
fwrite( & Stu, sizeof(Stu), 1, fpDest);
}
fclose( fpSrc);
fclose( fpDest);
return 0;

students.txt students.dat

N
log2Nlog2N
jack

08701342
4unsigned4

72


22

1.16.6

.htm

1.16.7 fseek

200199
1000
200100100020
2030

FILE

nn

01
fseek

int fseek(FILE *stream, long offset, int whence);


streamwhenceoffsetwhence
stdio.h
SEEK_SET:
SEEK_CUR:
SEEK_END:
fp
fseek(fp, 200, SEEK_SET);
200200
73

fseek(fp, 0, SEEK_SET);

fseek(fp, -100, SEEK_END);


100
fseek(fp, 100, SEEK_CUR);
100
fseek(fp, -100, SEEK_CUR);
100
students.dat4100
20
2.16.7.cpp
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

#include <stdio.h>
#include <string.h>
#define NUM 10
#define NAME_LEN 20
struct Student{
char szName[NAME_LEN];
unsigned nId;
short nGender; //
short nBirthYear, nBirthMonth, nBirthDay;
float fGPA;
};
int main()
{
FILE * fpSrc;
Student aStu[NUM];
fpSrc = fopen( "c:\\tmp\\students4.dat", "r+b");
if( fpSrc == NULL ) {
printf( "Failed to open the file.");
return 0;
}
fseek( fpSrc, sizeof(Student)* 4 , SEEK_SET);
fread( aStu, sizeof(Student), 7, fpSrc);
fseek( fpSrc, sizeof(Student) * 20, SEEK_SET);
fwrite( aStu, sizeof(Student), 7, fpSrc);
fclose( fpSrc);
return 0;
}

74

1.17 C
C
C++ C

1.17.1
math.h
abs(x)
x
cos(x)
x()
fabs(x)
x
ceil(x)
x
floor(x)
x
log(x)
x
log10(x)
x ( 10)
pow(x, y)
x y
sin(x)
x()
sqrt(x)
x

1.17.2
ctype.h
int isdigit(int c)
c
int isalpha(int c)
c
int isalnum(int c)
c
int islower(int c)
c
int islower(int c)
c
int isupper(int c)
c
int toupper(int c)
c
int tolower (int c)
c

1.17.3
string.h
char * char *
char * strchr(char * s, int c)
s c, s ,
NULL

75

char * strstr(char * s1, char * s2)


s2 s1 s1 s2
NULL
char * strlwr(char * s)
s
char * strupr( char * s)
s
char * strcpy( char * s1, char * s2)
s2 s1
char * strncpy( char * s1, char * s2, int n)
s2 s1 n
n s1 \0
char * strcat( char * s1, char * s2)
s2 s2
int strcmp( char * s1, char * s2)
0 s1 s2
0 0 s1
s2
int stricmp( char * s1, char * s2)
strcmp
int strlen(const char *string)

char * strncat(char *strDestination, const char *strSource, size_t count)


strSource count strDestination
int strncmp(const char *string1, const char *string2, size_t count)
count
char * strrev(char *string)
string
void * memcpy( void * s1, void * s2, int n)
s2 n s1
void * memset( void * s, int c, int n)
s n c

1.17.4

stdlib.h :
int atoi(char *s)
s s 1234,
1234
double atof(char *s)
s
char *itoa(int value, char *string, int radix);
value radix string
76

char szValue[20];
itoa( 32, szValue, 10); szValue 32
itoa( 32, szValue, 16); szValue 20

1.18

listfile.exe Windows
DOS
listfile
listfile
listfile file1.txt file1.txt
listfile
DOS
file1.txt
listfile file1.txt file2.txt
main

int main(int argc, char * argv[])


{

}
argc C/C++
argc 1argv
char*
argv[0]
argv[1]argv[2]
2.18.cpp
1. #include <stdio.h>
2. int main(int argc, char * argv[])
3. {
4.
for(int i = 0;i < argc; i ++ )
5.
printf( "%s\n", argv[i]);
6.
return 0;
7. }
2.18.exe:
2.18 para1 para2 s.txt 5 4

2.18
para1
para2
77

s.txt
5
4

1.19 C/C++

1.19.1

1)

2)
3)

Number, :
int nDoorNum Number Num
4
int nTotalStudents, nStudents ; nStudent

5) Is
int
IsCanceled();
int isalpha(); // C
BOOL
IsButtonPushed();

1.19.2

void main()

78

{
int t, x, y;
cin>>t;
while (t>0)
{
min=60000;
cin>>N>>x>>y>>max; plat[0].x1=x;
plat[0].x2=x; plat[0].h=y;
for (int i=1;i<=N;i++)
{
cin>>plat[i].x1>>plat[i].x2>>plat[i].h;
plat[i].t1=-1;
plat[i].t2=-1;
if (plat[i].h>y) {i--; N--; }
}
plat[0].t1=0;plat[0].t2=0;
qsort((void*)(&plat[1]), N, sizeof(plat[0]), compare);
tryway(0);
t--;
cout<<min<<endl;
}
}

1)
4
Tab Tab
Tab Tab
2) :

if( Condition1() && Condition2()


&& Condition3() ) {
}
3) {, }
{}
if ( condition1() ) {
DoSomething();
}

if ( condition1() )
{
DoSomething();
79

{
4) 1
int nAge = 5;
nAge = 4;
if( nAge >= 4 )
printf( %d, nAge);
for( i = 0; i < 100; i ++ );

1.19.3

/******************************************************************
** :
** Copyright (c) 1998-1999 *********
** :
** :
** :
** :
** :
**
** :
**---------------------------------------------------------------------------******************************************************************/

/*****************************************************************
** :
** : a,b,c
**
a--**
b--**
c--** : x--**
x 1, ...
**
x 0, ...
** :
80

** :
** :
** :
** :
** :
** :
**
****************************************************************/

1.19.4
1#define
#define MAX_STUDENTS 20
struct SStudent aStudents [MAX_STUDENTS];

struct SStudent aStudents [20];

#define TOTAL_ELEMENTS 100


for( i = 0; i < TOTAL_ELEMENTS; i ++)
{
}
2 sizeof()

int nAge;
for( j = 0; j < 100; j++ )
fwrite( fpFile, & nAge, 1, sizeof(int));

for( j = 0; j < 100; j++ )


fwrite( fpFile, & nAge, 1, 4);
4
n = k +++ j; //
n = ( k ++ ) + j; //
5
n = ( k ++ ) + j;
n = k + j;
k ++;
6 if else { }
if( Condition1() )
if( condition2()
81

DoSomething();
else
NoCondition2();

if( Condition1() ) {
if( condition2()
DoSomething();
else
NoCondition2();
}
7) 100

8)
9)
8)
9
10)
11)

82

C/C++

2.1

2 4
a

1 n n 1
a (a < 32768)

n
2 0

2
3
20

0 0
5 10

N N 0 0 N 4
N / 4 N / 2 N 4 N/4+1 N/2

1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main( )
{
int nCases, i, nFeet; //nCases nFeet
scanf("%d", &nCases);
for(i = 0; i < nCases; i++){
scanf("%d", &nFeet);
83

8.
if(nFeet %2 != 0) //
9.
// 2 4
10.
printf("0 0\n");
11.
else if (nFeet%4 != 0) // 4
12.
// 2
13.
printf("%d %d\n", nFeet / 4 + 1, nFeet / 2);
14.
else printf("%d %d\n", nFeet / 4, nFeet / 2);
15.
}
16. }

1
2
3
4
5

2.2

8 * 8 3-1

2-1

84


t0 <= t <= 20
"-"
"a""h""1""8"

,
"Inf".

2
a1 c3
f5 f8

2 1 2 1
3 1 1 Inf

x y
min(x,y)+abs(x-y)
xy x y
1x y x 0 y 0
2(x y) 1
x y 0 2(x y 0)

1x y
2x y

1. #include <stdio.h>
2. #include <math.h>
3. void main( )
4. {
5.
int nCases, i;
6.
scanf("%d", &nCases);
7.
for(i = 0; i < Casesn; i++){
8.
char begin[5], end[5]; // begin end
9.
scanf("%s %s", begin, end);
10.
int x, y; // x y x y
85

11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26. }

x = abs(begin[0] - end[0]);
y = abs(begin[1] - end[1]);
if(x == 0 && y == 0) printf("0 0 0 0\n"); // 0
else{
if(x < y) printf("%d", y);
//
else
printf("%d", x);
if(x == y || x == 0 || y == 0) printf(" 1");//
else
printf(" 2");
if(x == 0 || y == 0) printf(" 1"); //
else
printf(" 2");
if(abs(x - y) % 2 != 0) printf(" Inf\n"); //
else if(x == y)
printf(" 1\n");
else
printf(" 2\n");
}
}

1)
2)
3) Inf inf;
4)

2.3

L 1
0 L
012L

L1 <= L <= 10000 M1 <= M <= 100L


M L M M

500 3
150 300
100 200
86

470 471

298

1~10000
100
L 500
501 501 501
1 501
true
false
false
true
500
500 10000 10001

1. #include <stdio.h>
2. void main()
3. {
4.
int L, i, j, n; //L n i j
5.
bool trees[10001]; //
6.
for(i = 0; i < 10001; i++) //
7.
trees[i] = true;
8.
scanf("%d%d",&L,&n);
9.
for(i = 0; i < n; i++){
10.
int begin, end; // begin,end
11.
scanf("%d%d", &begin, &end);
12.
for(j = begin; j <= end; j++) // false
13.
trees[j] = false;
14.
}
15.
int count = 0; // count
16.
for(i = 0; i <= L; i++)
17.
if(trees[i]) count ++;
18.
printf("%d\n", count);
19. }

trees[10000]
trees 0

87

L
2.3.1 13
13 ( L M
)
2.3.2 L 40 trees

2.4

Alex N * M
P

k k

BEG
GEE

NM P (2 <= M, N <= 10, 0 <= P <=100) N


M P

3 3 2
EBG
GEE
88

EGE
BEG
GEE

EEG

26

1. #include <stdio.h>
2. void main()
3. {
4.
int characters[26];
5.
int n, m, p; // n*m p
6.
int i, j;
//
7.
for(i = 0; i < 26; i++)
//
8.
characters[i] = 0;
9.
scanf("%d%d%d", &n, &m, &p);
10.
for(i = 0; i < n; i++){// n*m
11.
char str[11];
12.
scanf("%s", str);
13.
for(j = 0; str[j] != '\0'; j++)
14.
characters[str[j] - 'A'] ++;
15.
}
16.
for(i = 0; i < p; i++){ // p
17.
//
char str[200];
18.
19.
scanf("%s", str);
20.
for(j = 0; str[j] != '\0'; j++)
21.
characters[str[j] - 'A'] --;
22.
}
23.
for(i = 0; i < 26; i++){ // 0
24.
if(characters[i] != 0)
25.
for(j = 0; j < characters[i]; j++)
89

26.
27.
28.
29. }

printf("%c", i + 'A');
}
printf("\n");

p M

2.5

h
1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 6*6*h

1*1 6*6 6 0

6 0

0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0

2
1

4 3*3 6*6 4 3*3


6*6 1 2 7 1*1 5 2*2
1 3*3 1
6*6
5*5 11 1*1
4*4 5 2*2
3*3 3*3 5*5 4*4
90

3*3 3*3
4 3*3 2*2
1*1 2*2 2*2
2*2 2*2 1*1

3*3 3*3
4 3*3 4 1
5 2*2 7 1*1 3*3 4 2
3 2*2 6 1*1 3*3 4 3
1 2*2 5 1*1 3*3 2*2
2*2 2*2 2*2
1*1 2*2 2*2
2*2 1*1 1*1 1*1
1*1 1*1 1*1
1*1

#include <stdio.h>
void main()
{
int N, a, b, c, d, e, f, y, x//N y 2*2
// x 1*1
int u[4]={0, 5, 3, 1};
// u 3*3 4 4 +1, 4 +2, 4 +3
// 3*3 2*2

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
while(1){
11.
scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
12.
if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) break;
13.
N = f + e + d + (c + 3) / 4;
14. // - (c+3)/4 c 4 ,
15.
y = 5 * d + u[c % 4];
if(b > y) N += (b - y + 8 ) / 9;
16.
17.
x = 36 * N - 36 * f - 25 * e - 16 * d - 9 * c - 4 * b;
18.
if(a > x) N += ( a - x + 35 ) / 36;
19.
printf("%d\n", N);
20.
}
21. }

output limit exceeded

91

2
a 5 5 a

3
2 15
1 4 3 2 9 7 18 22 3 2 1
4 2 18 9

0 255

5
CT N*N0<N<100
0-255
50

6
4

1) 8000 80 >80

1 1
2) 4000 85 >85

80 >80
3) 2000 90 >90
4) 1000 85 >85

92

5) 850 80 >80

87 82
4850

8
Julius Caesar
5 A F

AB C D E FG H I J K LM N O PQ RSTUVWXYZ

VWXYZAB C D E F G H I J K LM N O PQ R STU

2-2

10.
Bob Alice
n a1 ; . . .; an, 0 n
93

i
ai , ai i
k
n n , n
Alice Bob k

94

60 12
B bmbm-1b m-2 ... b1 dnd n-1d n-2 ... d1 B
i basei(1 i m) basei bi
dnd n-1d n-2 ... d1 bmbm-1b m-2 ... b1

B basei(1 i m) Ci-1 C
dnd n-1d n-2 ... d1 C b1 dnd n-1d n-2 ... d1 C
C b2 bm
B dnd n-1d n-2 ... d1 B m
bmbm-1b m-2...b1

3.1

6*9 = 42 13 , 6(13) * 9(13) =


42(13) 42(13) = 4 * 131 + 2 * 130 = 54(10) pq
r B(2<=B<=16) p * q = r. B ,
p = 11, q = 11, r = 121. 11(3) * 11(3) = 121(3) 11(3) = 1 * 31 + 1 * 30 =
4(10) 121(3) = 1 * 32 + 2 * 31 + 1 * 30 = 16(10) 10, 11(10) * 11(10) =
121(10) 3 0

T T pq
r pqr 1 pqr 1,000,000

p * q = r B
B 0

3
6 9 42
11 11 121
222
95


13
3
0

B
B
pqr pqr

1. #include <stdio.h>
2. #include <string.h>
3.
4. long b2ten(char* x, int b) {
5.
int ret = 0;
6.
int len = strlen(x);
7.
for (int i = 0; i < len; i++) {
8.
if (x[i]-'0' >= b) return -1;
9.
ret *= b;
10.
ret += x[i]-'0' ;
11.
}
12.
return (long)ret;
13. }
14.
15. void main( ) {
16.
int n;
17.
char p[8],q[8],r[8];
18.
long pAlgorism, qAlgorism, rAlgorism;
19.
scanf("%d", &n);
20.
while(n--) {
21.
scanf("%s%s%s", p, q, r);
22.
for(int b = 2; b <= 16; b++ ) {
23.
pAlgorism = b2ten(p, b);
24.
qAlgorism = b2ten(q, b);
25.
rAlgorism = b2ten(r, b);
26.
if (pAlgorism == -1 || qAlgorism == -1 || rAlgorism == -1) continue;
27.
if (pAlgorism * qAlgorism == rAlgorism) {
28.
printf("%d\n",b);
29.
break;
30.
}
31.
}
32.
if (b == 17) printf("0\n");
33.
}
34. }

96


a) b(2 b 16) b
b b2ten x
b b -1 bx
b
b) b 0

3.2 skew

skew binary , k xk xk(2k+1-1) 0


1 2, , 10120(skew) = 1(25-1) + 0(24-1) + 1(23-1) + 2(22-1)
+ 0(21-1) = 31 + 0 + 7 + 6 + 0 = 44. skew 01210111220100101
102

n n = 0 n
skew

n 231-1 =
2147483647

10120
200000000000000000000000000000
10
1000000000000000000000000000000
11
100
11111000001110000101101102000
0

44
2147483646
3
2147483647
4
7
1041110737

skew skew
k skew 2k-1
n 231-1 skew 31
base[31] skew 2 .. 31
skew
97

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.

base[0] = 1
base[k] = 2k+1 - 1 = 2 * (2k - 1) + 1 = 2 * base[k -1] + 1

#include <stdio.h>
#include<string.h>
int main()
{
int i,k,base[31],sum;
char skew[32];
base[0]=1;
for( i = 1; i < 31; i++) base[i] = 2 * base[i-1] + 1;
while(1) {
scanf("%s", skew);
if (strcmp(skew,"0") == 0)
break;
sum = 0;
k = strlen(skew);
for( i = 0; i < strlen(skew); i++) {
k--;
sum += (skew[i] - '0') * base[k];
}
printf("%d\n",sum);
}
return 0;
}

1(ai2734)

2(ai2735)

32 16 (ai2798)
2 2 16 16
A-F 10-15

4(ai2765)
0.75
0.963125 (7/8 + 5/64) n
3n (0, 1)

98


d1d2d3 ... dk [8]
= (d1+(d2+(d3 +( ... dk*0.125 ...) *0.125)*0.125)*0.125 [10]
= (d1*103*(k-1)+(d2*103*(k-2)+(d3*103*(k-3)+( ... dk*125 ...) *125)*125)*125*10-3*k [10]
D1D2D3 ... Dm
0.D1D2D3 ... Dm 42

99


4.1
4
5
6 5

13~16
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

#include <string.h>
#include <stdio.h>
char str1[] = "The quick brown dog jumps over the lazy fox";
char str2[50] = "The QUICK brown dog jumps over the lazy fox";
char str3[40] = "The QUICK brown dog jumps over the lazy fox";
// 43 44
char str4[50];
void main( void )
{
int result;
str4 = "The QUICK brown DOG jumps over the lazy fox";
//
str4 = str2; //
str4 = str1; //
printf( "Compare strings:\n\t%s\n\t%s\n\n", str1, str2 );
result = strcmp( str1, str2 );
printf( "strcmp: result=%d\n", result);
result = stricmp( str1, str2 );
printf( "stricmp: result=%d\n", result);
printf( "strcspan: %d\n", strcspan(st1, "bdog"));
scanf("%s", str4);
//
printf("#%s#\n", str4); //
str2[19] = '\0'; //
printf("%s\n", str2); //
return;
}

hello world

100

43 43 0
Comparing strings:
The quick brown dog jumps over the lazy fox
The QUICK brown dog jumps over the lazy fox
strcmp: result=1
stricmp: result=0
#hello#
The QUICK brown dog

4.2 :

a-z 26
1 n 1 a-z 26
1000
n
ascii

2
abbccc
adfadffasdf

c 3
f 4

(1) scanf

(2)
scanf
strlen
(3) ascii

101


1000 1001 str
'\0' 26 sum
c sum[c-'a']

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.

#include <stdio.h>
#include <string.h>
void main( )
{
int cases, sum[26], i, max;
char str[1001];
scanf("%d", &cases);
while (cases > 0) {
scanf("%s", str);
for(i = 0; i < 26; i++)
sum[i]=0;
for(i = 0; i < strlen(str); i++)
sum[str[i] - 'a']++;
max = 0;
for( i = 1; i < 26; i++)
if (sum[i] > sum[max]) max = i;
printf("%c %d\n", max+'a', sum[max]);
cases--;
}
}

(1) str 1000 1001


'\0' N
N+1
(2) 15~17
ascii
17 sum[i]>sum[max] sum[i]sum[max]
max ascii

4.3 : 487-3279

Waterloo TUT-GLOP
310-GINO
Gino's pizza
Pizza Hut 3-10-10-10
pizza
102

A, B, C 2
D, E, F 3
G, H, I 4
J, K, L 5
M, N, O 6
P, R, S 7
T, U, V 8
W, X, Y 9
Q Z TUT-GLOP
888-4567310-GINO 310-44663-10-10-10 310-1010

100000

Q Z

No duplicates.

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9487-3279

310-1010 2
487-3279 4
888-4567 3

(1)
8 4 '-'
(2)

103

telNumbers[100000][9]

telNumbers telNumbers

C/C++ sort strcmp


telNumbers

1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <string.h>
4.
5. char map[] = "22233344455566677778889999";
6. char str[80], telNumbers[100000][9];
7.
8. int compare(const void *elem1,const void *elem2) {
9.
// sort
10.
return (strcmp((char*)elem1, (char*)elem2));
11. };
12.
13. void standardizeTel(int n) {
14.
int j, k;
15.
16.
j = k = -1 ;
17.
while ( k<8 ) {
18.
j++;
19.
if ( str[j] == '-' )
20.
continue;
21.
k++;
22.
if (k==3) {
23.
telNumbers[n][k]='-';
24.
k++;
25.
}
26.
if(str[j]>='A' && str[j]<='Z') {
27.
telNumbers[n][k]=map[str[j]-'A'];
28.
continue;
29.
};
30.
telNumbers[n][k]=str[j];
31.
}
32.
telNumbers[n][k]='\0';
33.
return;
34. }
35.
36. void main()
37. {
38.
int n,i,j;
39.
bool noduplicate;
40.
41.
scanf("%d",&n);
42.
for(i=0;i<n;i++){// telNumbers
43.
scanf("%s",str);
104

44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64. }

standardizeTel(i);
// str telNumbers i
}
qsort(telNumbers,n,9,compare);//
noduplicate = true;
i=0;
while(i<n){//
j=i;
i++;
while(i<n&&strcmp(telNumbers[i], telNumbers[j])==0) i++;
if(i-j>1) {
printf("%s %d\n", telNumbers[j], i-j);
noduplicate = false;
}
}
if ( noduplicate )
printf("No duplicates.\n");

(1) map map[j] j+'A'


map

(2) C/C++
sort strcmp
(3)

standardizeTel

map telNumbers

(1)
'-'
(2) "No duplicates."

4.4 :

x
yx y x
105

y
t (1 <= t <= 10)t
n (1 <= n <= 100) n n
1 100
x
0

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

2
2

x0 x x' x
x x0 x' x0 x0 x x

x0 x0
x0

(1) x0

(2)
strlen
strncpy
strcpy
strstr
strrev

1. #include <stdio.h>
2. #include <string.h>
3.
4. int t, n;
5. char str[100][101];
106

6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.

int searchMaxSubString(char* source) {


int subStrLen = strlen(source), sourceStrLen = strlen(source);
int i, j;
bool foundMaxSubStr;
char subStr[101], revSubStr[101];
while ( subStrLen > 0 ) {//
for (i = 0; i <= sourceStrLen - subStrLen; i++) {
// subStrLen
strncpy(subStr, source+i, subStrLen);
strncpy(revSubStr, source+i, subStrLen);
subStr[subStrLen] = revSubStr[subStrLen] = '\0';
strrev(revSubStr);
foundMaxSubStr = true;
for( j = 0; j < n; j++)
if ( strstr(str[j], subStr) == NULL && strstr(str[j], revSubStr) == NULL ) {
foundMaxSubStr = false;
break;
}
if (foundMaxSubStr) return(subStrLen);
}
subStrLen--;
}
return(0);
}
void main()
{
int i, minStrLen, subStrLen;
char minStr[101];
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
minStrLen = 100;
for (i = 0; i < n; i++) {//
scanf("%s", str[i]);
if ( strlen(str[i]) < minStrLen ) {//
strcpy(minStr, str[i]);
minStrLen = strlen(minStr);
}
}
subStrLen = searchMaxSubString(minStr);//
printf("%d\n", subStrLen);
}
}

y y
x

107


strncpy '\0'

4.5 :

Julius Caesar
Caesar Caesar
5
A F

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
100 3
START
1 200 Caesar
END
ENDOFINPUT
Caesar

START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS
WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES


I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

(1) scanf "START"


"END"
(2)

108


scanf

strcmp
strlen
strcat

1. #include <stdio.h>
2. #include <string.h>
3. void decipher(char message[]);
4. void main()
5. {
6.
char message[201];
7.
gets(message);
8.
while(strcmp(message, "START")==0){
9.
decipher(message);
10.
printf("%s\n",message);
11.
gets(message);
12.
}
13.
return;
14. }
15.
16. void decipher(char message[])
17. {
18.
char plain[27]="VWXYZABCDEFGHIJKLMNOPQRSTU";
19.
char cipherEnd[201];
20.
int i, cipherLen;
21.
gets(message);
22.
cipherLen = strlen(message);
23.
for ( i=0; i<cipherLen; i++ )
24.
if ( message[i]>='A' && message[i]<='Z' ) message[i] = plain[message[i]-'A'];
25.
26.
gets(cipherEnd);
27.
28.
return;
29. }

(1) cipher
",""BFW"
cipher "BFW," cipher

(2)

1 (ai2743)
109

s1 s2 100
0
2 All in All (ai1936)
s t s t t
ss t ASCII 100000
3 (ai2818)
Bob Alice
n a1an, 0 n
i
ai , ai i
k n n ,
n Alice Bob
k 0 < n <= 200
4 W (ai2819)
, k1k2 k3[a-i] [j-r]
, ([s-z] )
ki
ki
the_quick_brown_foxki 23 1 _icuo_bfnwhoq_kxert

[a-i]{i,c,b,f,h,e}{2,3,7,8,11,17}
k1=2 , {h,e,i,c,b,f}

_ 80ki
1-100
5 (ai2820)

110


a)
'A' 'Y''Z' 'A'
"VICTORIOUS" "WJDUPSJPVT"
b) <2, 1, 5, 4, 3, 7, 6, 10, 9, 8>
"VICTORIOUS" "IVOTCIRSUO"

"VICTORIOUS"
"JWPUDJSTVP"

100
6 (ai2804)

100000
100000
10

eh
sort bsearch

7 (ai2797)
"carbon":
"c", "ca", "car", "carb", "carbo", "carbon",
, "carbohydrate"
"carb""carbohydrate" "carboh",
"carbo" ()"carbo"
carbohydrate
cart
carbonic
caribou
carriage
car
"car""car". "car"
"car" , car"carriage""car"
,
2 1000 1 20
111

8 (ai2799)
n(n<=10000) n
50

112

5.1

4 100
400 1900 2000

a(0 < a < 3000)

a Y N

2006

0 3000 3200
a a


a % 4 ! = 0 a
a % 100 == 0 && a % 400 != 0 a
a

(a % 400 == 0 || (a % 4 == 0 && a % 100 != 0)), a a

1.
2.
3.
4.
5.

#include <stdio.h>
void main()
{
int a; //
scanf("%d", &a);
113

6.
7.
8.
9.
10.
11.
12. }

1.
2.
3.
4.
5.
6.
7.
8.
9.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.

if(a % 4 != 0)
printf("N\n");
else if(a % 100 == 0 && a % 400 != 0)
printf("N\n");
else
printf("Y\n");

#include <stdio.h>
void main(){
int a;
scanf("%d", &a);
if((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)
printf("Y\n");
else
printf("N\n");
}

#include <stdio.h>
void main()
{
int year, a, b, c;
scanf("%d", &year);
a = year % 4;
b = year % 100;
c = year % 400;
if (a != 0){
printf("N\n");
}
if (a == 0 && b != 0){
printf("Y\n");
}
if(b == 0 && c != 0){
printf("N\n");
}
if(c == 0){
printf("Y\n");
}
}
1. a,b,c
2 && Y N

114

1.
2.
3.
4.
5.
6.
7.
8.
9.

#include<stdio.h>
void main()
{
int n;
scanf("%d", &n);
if(n % 400 == 0)
printf("Y");
else if(n % 4 == 0) printf("Y");
else printf("N");
}
100 400

1. #include<stdio.h>
2. void main()
3. {
4.
int n;
5.
scanf("%d", &n);
6.
if(n / 4 == 0){
7.
if(n / 400 == 0) printf("Y\n");
8.
else if(n / 100 == 0) printf("N\n");
9.
else
printf("Y\n");
10.
}else
printf("N\n");
11.
12. }

c c++
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void main()
{
int year;
scanf("%d", &year);
bool judge;
judge = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
if (judge) printf("%c",'Y');
else
printf("%c",'N');
printf(\n);;
}
1. <stdio.h> c <iostream.h> c++

c c++
2. prinf Y N %c Y
115

5.2

10 20
40 80

n n 5

2
11112
2 28 10 3 2

2
40

n 2 n

n
n 1.
2.
3 m
4 2 m x;
5. x;

1.
2.
3.
4.
5.

// c061000208013
#include <stdio.h>
void main( )
{
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n;
116

6.
scanf("%d", &n);
7.
for(int i = 0; i < n; i ++){
8.
int month_1, day_1, month_2, day_2, num; //
9.
scanf("%d%d%d%d%d", &month_1, &day_1, &num,&month_2, &day_2);
10.
int sum = 0;
11.
for(int k = month_1; k < month_2; k ++){
12.
sum += days[k - 1];
13.
}
14.
sum -= day_1;
15.
sum += day_2;
16.
17.
long nNum = num;
18.
for(k = 0; k < sum; k ++){
19.
nNum *= 2;
20.
}
21.
printf("%d\n", nNum);
22.
}
23. }
24.
// c060100548302
1. #include<stdio.h>
2. int month[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
3. void main()
4. {
5.
int times;
6.
scanf("%d", &times);
7.
int mon1, date1, mon2, date2, num1;
8.
while(times --){
9.
scanf("%d%d%d%d%d", &mon1, &date1, &num1, &mon2, &date2);
10.
int days = date2 - date1;
11.
for(int i = mon1; i < mon2; i++){
12.
days += month[i];
13.
}
14.
long num = num1;
15.
for(int j = 0; j < days; j++){
16.
num *= 2;
17.
}
18.
printf( "%d\n", num );
19.
}
20. }

1. #include<stdio.h>
2. #include<math.h>
117

3. int dayofmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
4.
5. void main(){
6.
int n;
7.
scanf("%d", &n);
8.
int a ,b ,num ,c ,d ,i,k;
9.
for(i = 0; i < n; i++){
10.
scanf("%d %d %ld %d %d", &a, &b, &num, &c, &d);
11.
int days = 0;
12.
if(a == c)
13.
{
14.
days = d - b;
15.
}
16.
else if(a != c){
17.
for(k = a; k<c; k++){
18.
days = days + dayofmonth[k - 1];
19.
}
20.
days = days - b + d;
21.
}
22.
num = num * pow(2, days); // pow math.h
23.
printf("%ld\n", num);
24.
}
25. }
1. for(k = a; k < c; k++) a == c if(a == c)
2 a,b,c,d mon1,day1,mon2,day2

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

:
#include<stdio.h>
void main()
{
int n;
scanf("%d", &n);
int month[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month1, day1, month2, day2, days=0, num;
for(int i = 0; i < n; i++){
scanf("%d %d %d %d %d", &month1, &day1, &num, &month2, &day2);
for(int i = month1; i < month2; i++){
days += month[i];
}
days += day2;
days -= day1;
for(int j = 0; j < days; j++){
num = 2 * num;
118

17.
}
18.
printf("%d\n", num);
19.
} // for
20. } // main
days 0 days
for(int i = 0; i < n; i++){

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

#include<stdio.h>
void main()
{
int a[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int mo1, mo2, da1, da2, num, all, day1, day2, day, i, n;
scanf("%d", &n);
for(i = 0; i < n; i++){
day1 = 0, day2 = 0;
scanf("%d%d%d%d%d", &mo1, &da1, &num, &mo2, &da2);
for(i = 0; i < mo1 - 1; i++) day1 = day1 + a[i];
for(i = 0; i < mo2 - 1; i++) day2 = day2 + a[i];
day = day2 + da2 - day1 - da1;
all = num;
for(i = 0; i < day; i++) all *= 2;
printf("%d\n", all);
}
}
i j i

#include<stdio.h>
#include<math.h>
void main(){
int n, month1, day1, month2, day2;
long num1, num2;
int totalDays = 0;
int days[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
scanf("%d", &n);
while(n--){
scanf("%ld %ld %ld %ld %ld", &month1, &day1, &num1, &month2, &day2);
if(month1 == month2)
totalDays = day2 - day1;
else{
totalDays = day2 + days[month1] - day1; // days[month1]
for(int i = month1; i < month2; i++)
// days[month1]
119

16.
17.
18.
19.
20.
21.
22.
23. }

totalDays += days[i];
}
num2 = num1;
for(int j = 0; j < totalDays; j++)
num2 *= 2;
printf("%ld\n", num2);
}

Presentation Error
5.2:

5.3

, 4 100
400 1700, 1800, 1900 2100
1600, 2000 2400 2000 1 1

2000 1 1
1, 9999

YYYY-MM-DD
DayOfWeek, DayOfWeek "Sunday", "Monday",
"Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday

1730
1740
1750
1751
-1

2004-09-26 Sunday
2004-10-06 Wednesday
120

2004-10-16 Saturday
2004-10-17 Sunday

12

2000 1 1 7
year, month, date
n n n
n m year
m m m
k, month k
k+1 date

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.

#include <stdio.h>
int type(int );
char week[7][10]={"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday"};
int year[2]={365,366}; //year[0]year[1]
int
month[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31};
//month[0]month[1]
void main()
{
int days, dayofweek; //days dayofweek
int i = 0, j = 0;
while (scanf("%d", &days) && days != -1) {
dayofweek = days % 7;
for(i = 2000; days >= year[type(i)]; i++)
days -= year[type(i)];
for(j = 0; days >= month[ type(i) ][ j ]; j++)
days -= month[ type(i) ][ j ];
printf("%d-%02d-%02d %s\n", i, j + 1, days + 1, week[dayofweek]);
}
}
int type(int m){ // m 1 0
if(m % 4 != 0 || (m % 100 == 0 && m % 400 != 0))
return 0; //
else return 1;
//
}

121


2000

5.4

M.A
365 Haab
Haab 19 18 20 pop, no,
zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu.
0 19 Haab uayet 5 0
4

Tzolkin(holly
) 13 20
1~13 20 imix,
ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban,
eznab, canac, ahau
1 imix, 2 ik, 3 akbal, 4 kan, 5 chicchan, 6 cimi, 7 manik, 8 lamat, 9 muluk, 10
ok, 11 chuen, 12 eb, 13 ben, 1 ix, 2 mem, 3 cib, 4 caban, 5 eznab, 6 canac, 7 ahau, 8 imix, 9
ik, 10 akbal . . .
Haab Tzolkin 01
0

Haab: 0. pop 0
Tzolkin: 1 imix 0
M.A. Haab Tzolkin

Haab
.
Haab
5000

Tzolkin

Haab

3
10. zac 0
0. pop 0
10. zac 1995
122


3
3 chuen 0
1 imix 0
9 cimi 2801

Haab Tzolkin
Haab 365 19 18 20 19
5 19 0
0~18
*20++1 Tzolkin 260
1~13 20
Tzolkin
260 13 20

Haab k
k 260 Tzolkin k 260 m m 13 20
d sd Tzolkin s
1 0 260 259 260
Tzolkin m 13 0 12 1 Tzolkin
m 20 0~19 20
20 0~19

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

#include <stdio.h>
#include <string.h>
const int NAMELEN = 10;
char month1[19][NAMELEN]
={"pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac",
"ceh","mac","kankin","muan","pax","koyab","cumhu","uayet"};
char month2[20][NAMELEN]
={"imix","ik","akbal","kan","chicchan","cimi","manik","lamat","muluk",
"ok","chuen","eb","ben","ix","mem","cib","caban","eznab","canac","ahau"};
void main( )
{
int nCases, i, m;
scanf("%d", &nCases);
printf("%d\n", nCases);
for (i = 0; i < nCases; i++){
int day, year, dates;
char month[NAMELEN];
scanf("%d. %s %d", &day, month, &year); // Haab
123

18.
for(m = 0; m < 19; m++)
19.
if (!strcmp(month1[m], month)) break; //
20.
dates = year * 365 + m * 20 + day;
// 0
21.
printf("%d %s %d\n", 1 + dates % 13, month2[dates % 20], dates / 260); //
22.
}
23. }
24.

Tzolkin 21 1+dates%13
(dates+1)%13 1~13 0~12

5.5

19
12

1878 Sir Sanford Fleming


24
15 360 24 15
Sir Sanford
Fleming
1883 11 18 Fleming 1884

0
1895
1918
Fleming 5
Coordinated Universal TimeUTC
8
1 3 Fleming
Fleming 24
UTC
24

UTC Coordinated Universal Time


GMT Greenwich Mean Time, UTC
BST British Summer Time, UTC+1 hour
124

IST Irish Summer Time, UTC+1 hour


WET Western Europe Time, UTC
WEST Western Europe Summer Time, UTC+1 hour
CET Central Europe Time, UTC+1
CEST Central Europe Summer Time, UTC+2
EET Eastern Europe Time, UTC+2
EEST Eastern Europe Summer Time, UTC+3
MSK Moscow Time, UTC+3
MSD Moscow Summer Time, UTC+4
AST Atlantic Standard Time, UTC-4 hours
ADT Atlantic Daylight Time, UTC-3 hours
NST Newfoundland Standard Time, UTC-3.5 hours
NDT Newfoundland Daylight Time, UTC-2.5 hours
EST Eastern Standard Time, UTC-5 hours
EDT Eastern Daylight Saving Time, UTC-4 hours
CST Central Standard Time, UTC-6 hours
CDT Central Daylight Saving Time, UTC-5 hours
MST Mountain Standard Time, UTC-7 hours
MDT Mountain Daylight Saving Time, UTC-6 hours
PST Pacific Standard Time, UTC-8 hours
PDT Pacific Daylight Saving Time, UTC-7 hours
HST Hawaiian Standard Time, UTC-10 hours
AKST Alaska Standard Time, UTC-9 hours
AKDT Alaska Standard Daylight Saving Time, UTC-8 hours
AEST Australian Eastern Standard Time, UTC+10 hours
AEDT Australian Eastern Daylight Time, UTC+11 hours
ACST Australian Central Standard Time, UTC+9.5 hours
ACDT Australian Central Daylight Time, UTC+10.5 hours
AWST Australian Western Standard Time, UTC+8 hours

N N N
a.m./p.m
midnight 12 12:00 a.m.
noon 12 12:00 p.m.

4
noon HST CEST
11:29 a.m. EST GMT
6:01 p.m. CST UTC
12:40 p.m. ADT MSK

125


midnight
4:29 p.m.
12:01 a.m.
6:40 p.m.

a.m/p.m. noon midnight


noon midnight

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.

#include <stdio.h>
#include <string.h>
int difference(char* zone1, char* zone2){ //
char* zone[32]={"UTC",
"GMT","BST","IST","WET","WEST",
"CET","CEST","EET","EEST","MSK",
"MSD","AST","ADT","NST","NDT",
"EST","EDT","CST","CDT","MST",
"MDT","PST","PDT","HST","AKST",
"AKDT","AEST","AEDT","ACST","ACDT",
"AWST"};
float time[32]={0,0,1,1,0,1,1,2,2,3,3,4,-4,-3,-3.5,-2.5,-5,-4,-6,-5,-7,
-6,-8,-7,-10,-9,-8,10,11,9.5,10.5,8};
int i, j;
for (i = 0; strcmp(zone[i], zone1); i++); //
for (j = 0; strcmp(zone[j], zone2); j++); //
return (int)((time[i] - time[j]) * 60); //
}
void main()
{
int nCases;
scanf("%d", &nCases); //
for (int i = 0; i < nCases; i++){ //
char time[9]; //
int hours, minutes; //
scanf("%s", time); //
switch(time[0]){
126

28.
case 'n': hours = 12; // noon
29.
minutes = 0;
30.
break;
31.
case 'm': hours = 0;
// midnight
32.
minutes = 0;
33.
break;
34.
default : sscanf(time, "%d:%d", &hours, &minutes); //
35.
hours %= 12;
36.
scanf("%s", time);
// a.m. p.m.
37.
if (time[0] == 'p') hours += 12;
38.
}
39.
char timezone1[5], timezone2[5];
40.
scanf("%s%s", timezone1, timezone2); //
41.
int newTime;
//
42.
newTime = hours * 60 + minutes + difference(timezone2, timezone1);
43.
if (newTime < 0) newTime += 1440; //
44.
newTime %= 1440; //
45.
switch(newTime){
46.
case 0 : printf("midnight\n"); //
47.
break;
48.
case 720: printf("noon\n");
//
49.
break;
50.
default : hours = newTime / 60; //
51.
minutes = newTime % 60; //
if(hours == 0)
// , 0
52.
53.
printf("12:%02d a.m.\n", minutes);
54.
else if(hours < 12)
//
55.
printf("%d:%02d a.m.\n", hours, minutes);
56.
else if(hours == 12)
// , 0
57.
printf("12:%02d p.m.\n", minutes);
58.
else //
59.
printf("%d:%02d p.m.\n", hours%12, minutes);
60.
} // end of switch
61.
} //end of for
62. } // end of main

if else
12:01am 1 12:01pm
12 1 noon midnight

12 24
24 12
127


1 1
13 5 13 5
w
13 5 w=1..7

135781012 31 46911 30 2 28

2 2

10 100 100 10 10
10
0 1
0 0:0:0 1.1.2000 0:0:0 1.1.0

128

6.1

n, m ( 0 < m, n < 300)

00

62
12 4
83
00

5
1
7

n m
f(n,m)
n 1
m

anLoop n n nPtr
0
0
nPtr anLoop n-1
nPtr 0 anLoop
129

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 300
int aLoop[MAX_NUM + 10];
main()
{
int n, m, i;
while(1) {
scanf( "%d%d", & n, & m);
if ( n == 0 )
break;
for( i = 0; i < n; i ++ )
aLoop[i] = i+1;
int nPtr = 0;
for( i = 0; i < n; i ++ ) { // 1
//
int nCounted = 0;
while( nCounted < m ) { // m
while( aLoop[nPtr] == 0) //
nPtr = ( nPtr + 1 ) % n; //
nCounted ++;
//
nPtr = ( nPtr + 1 ) % n; //
}
nPtr --;
//
if( nPtr < 0 )
nPtr = n - 1;
if( i == n-1 ) //
printf("%d\n", aLoop[nPtr]);
aLoop[nPtr] = 0; //
}
}
}

n 0
( + 1 )% n

15 0 n-1 0 n
130

24 26 24
nPtr 0
6.1: f(n, m)

m
n

6.2

6-1-1

6-1-2

1)
131

2)
3)
4)

2 (2, 5), (3, 7), (4, 2), (5, 4)


13, 7, 15, 9 21 37

M, N K M * N1
<= M, N <= 20 K0 <= K <= 1000 M
N i + 1 j Pij0 <= Pij <= 500
(i, j)0

6 7 21
0000000
0 0 0 0 13 0 0
0000007
0 15 0 0 0 0 0
0009000
0000000

37

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
int T, M, N, K;
#define MAX_NUM 55
int aField[MAX_NUM][MAX_NUM];
main()
{
scanf("%d", &T);
for( int t = 0; t < T; t ++ ) {
scanf("%d%d%d", &M, &N , &K);
// aField[1][1] 0
132

14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52. }

for( int m = 1; m <= M; m ++ )


for( int n = 1 ; n <= N; n ++ )
scanf("%d", & aField[m][n]);
int nTotalPeanuts = 0; //
int nTotalTime = 0;
//
int nCuri = 0, nCurj; //
//nCuri 0
while( nTotalTime < K ) { //
int nMax = 0, nMaxi, nMaxj; //
//
for( int i = 1; i <= M; i ++ ) {
for( int j = 1; j <= N ;j ++) {
if( nMax < aField[i][j])
{
nMax = aField[i][j];
nMaxi = i;
nMaxj = j;
}
}
}
if( nMax == 0 ) //
break;
if( nCuri == 0)
nCurj = nMaxj; /*
nMaxj */
/* (nMaxi, nMaxj)
*/
if( nTotalTime + nMaxi + 1 + abs(nMaxi-nCuri) + abs(nMaxj-nCurj) <= K ) {
//
nTotalTime += 1 + abs(nMaxi-nCuri) + abs(nMaxj-nCurj);
nCuri = nMaxi; nCurj = nMaxj; //
nTotalPeanuts += aField[nMaxi][nMaxj];
aField[nMaxi][nMaxj] = 0; //
}
else
break;
}
printf("%d\n", nTotalPeanuts);
}

aField[0][0] aField[1][1]
1
1 aField[1][1] aField[i][j]
133

i C/C++ 0
0

6.3

s n (1 <= s <= 10, 0 <=


n <= 99999999) n s
0

s '-' s '|'
s+2 2s+3

2 12345
3 67890
00

-- --|
|
| | | |
|
|
| | | |
-- ---| |
|
|
|
| |
|
|
|
-- ----|
|
|
--|
|

--|
|
|

--|
|
|
|
|
|
--| |
|

--| |
| |
| |
--|

--|
|
|
|
|
|
|

|
134

|
|

|
|
---

| |
|
| |
|
| |
|
| |
|
-------

:
(digit) 0 1 2 9
(number)

1 7 7

6-3
8 7 13 6 1
36 s - s |
1 n 1 2 s+1
2 3 s+2 4 s+3
2s+2 5 6 2s+3
7 d m (m = 17) d m
m s - s | m

1
char n1[11] = {"- -- -----"};
n1[i](i = 09) 1 i n1[i] '-'
n1[i] 1 0, 2, 3, 5, 6, 7, 8, 9
2 '|'
char n2[11] = {"| ||| ||"};
2 0, 4, 5, 6, 8, 9
():
1.
2.
3.

#include <stdio.h>
#include <string.h>
char n1[11] = {"- -- -----"};

// 1 0, 2, 3, 5, 6, 7, 8, 9
135

4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.

char n2[11] = {"|


||| ||"}; // 2 0, 4, 5, 6, 8, 9
char n3[11] = {"||||| |||"}; // 3 0, 1, 2, 4, 7, 8, 9
char n4[11] = {" ----- --"}; // 4 2, 13, 4, 5, 6, 8, 9
char n5[11] = {"| | | | "}; // 5 0, 2, 6, 8
char n6[11] = {"|| |||||||"}; // 6 0, 1, 3, 4, 5, 6, 7, 8, 9
char n7[11] = {"- -- -- --"}; // 7 0, 2, 3, 5, 6, 8, 9
void main() {
int s;
char szNumber[20];
int nDigit , nLength, i , j , k;
while(1) {
scanf( "%d%s", &s, szNumber);
if (s == 0)
break;
nLength = strlen(szNumber);
for (i = 0 ; i < nLength ; i++) { // 1
nDigit = szNumber[i] - '0';
printf(" ");
for (j = 0 ; j < s ; j++) // s
printf("%c", n1[nDigit]);
printf(" ");
}
printf("\n");
for (i = 0 ; i < s ; i++) { // 2 3
for (j = 0 ; j < nLength ; j++) {
nDigit = szNumber[j] - '0';
printf("%c", n2[nDigit]);
for (k = 0 ; k < s ; k++)
printf(" "); // 2 3
printf("%c ", n3[nDigit]);
}
printf("\n");
}
for (i = 0 ; i < nLength ; i++) { // 4
printf(" ");
nDigit = szNumber[i] - '0';
for (j = 0 ; j < s ; j++)
printf("%c", n4[nDigit]);
printf(" ");
}
printf("\n");
for (i = 0 ; i < s ; i++) { // 5 6
for (j = 0 ; j < nLength ; j++) {
nDigit = szNumber[j] - '0';
136

48.
printf("%c", n5[nDigit]);
49.
for (k = 0 ; k < s ; k++)
50.
printf( " "); // 5 6
51.
printf("%c ", n6[nDigit]);
52.
}
53.
printf("\n");
54.
}
55.
for (i = 0 ; i < nLength ; i++) { // 7
56.
printf(" ");
57.
nDigit = szNumber[i] - '0';
58.
for (j = 0 ; j < s ; j++)
59.
printf("%c", n7[nDigit]);
60.
printf(" ");
61.
}
62.
printf("\n");
63.
printf("\n");
64.
}
65. }

int n1[10] = {1, 0, 1, 1, 0, 1, 1, 1, 1, 1 };


1 i 1
n1[i]'-' char
char n1[11] = {"- -- -----"};
i 1 s n1[i]

01

6.4

137

n 1 n n n
n=3 1 2 31 3 22 1 32 3 13 1 23 2 1

k 1
1 1 2 3n
n = 3k=2 2 3 1 1 3 1 2 2 3 2 1
3 2 1

m m
2 n( 1 <= n < 1024 ) k(1<=k<=64) n 12 n

n k

3
31
231
31
321
10 2
1 2 3 4 5 6 7 8 9 10

312
123
1 2 3 4 5 6 7 9 8 10

1 n n
10241024!
k
k
k 64

2 1 4 7 6 3 52 1 4 7 6 5 3
2 1 5 3 4 6 7
2 1 4 7 6 5 3 2 1 5 3 4 6 7

n a1, a2, a3an


aj 7
1) an aj aj-1 < a,
j

aj-1 4
2) aj aj+1 an aj-1 aj-1
, 5 4 2 1 5 7 6 4 3

3) j n n-j+1

7 6 4 32
1 5 3 4 6 7

138

a1, a2, a3an an, an-1,


an-2a1

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.

#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 1024
int an[MAX_NUM + 10];
//
int MyCompare( const void * e1, const void * e2)
{
return * ((int *) e1) - * ((int *) e2);
}
main()
{
int M;
int n, k, i, j;
scanf("%d", & M);
for (int m = 0; m < M; m ++ ) {
scanf("%d%d", &n, &k);
// an[1] .... an[n]
for( i = 1; i <= n; i ++ )
scanf("%d", &an[i]);
an[0] = 100000;
// an[0]
for( i = 0; i < k ;i ++ ) { //
for( j = n ; j >= 1 && an[j-1] > an[j] ; j -- ) ;
if( j >= 1 ) {
int nMinLarger = an[j];
int nMinIdx = j;
// an[j] an[j-1]
for( int kk = j; kk <= n; kk ++)
if( nMinLarger > an[kk] && an[kk] > an[j-1]) {
nMinLarger = an[kk];
nMinIdx = kk;
}
//
an[nMinIdx] = an[j-1];
an[j-1] = nMinLarger;
qsort( an + j, n - j + 1 , sizeof(int), MyCompare);
//
}
else { //an 1 2 3
n
for( j = 1; j <= n; j ++ )
139

40.
41.
42.
43.
44.
45.
46.
47.
48. }

an[j] = j;
}
}
for( j = 1; j <= n; j ++ )
printf("%d ", an[j]);
printf("\n");
}

36 qsort
qsort

1 an[1] .... an[n] an[0]


an[0]
23:
for( j = n ; j >= 1 && an[j-1] > an[j] ; j -- ) ;
23 j-1 0 an[0] ....
an[n-1] 23 for j-1 0

2. C++ next_permutation

scanf C++, C++


cin

1
xyz
x z

140

6-5
xyz 012 345
xyz

forward x x
back x x
left x x
right x x
up x x
down x x

6-6

141

24 2 4 6 6 6 24
39 3 9 12 12 1 2
3 3 39

3
12 12
(1, 1)(1,
2)(12, 12)

6-7

(1, 1) -> (2, 1) -> (3, 1)

(2, 1) -> (2, 2) -> (2, 3)


-
(1, 1) -> (2, 2) -> (3, 3)
(1, 12)(12, 1)

0
100

142

= -

= (0.5 * + 0.5 * ) * ( + 10) / 100


= (0.8 * + 0.2 * ) * ( + 10) / 100
= (0.2 * + 0.8 * ) * ( + 10) / 100

N N < 1000

1000

4
n cyclic 1 n ,

142857 :
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
(2 60 )

143


C/C++ int -231231 1
unsigned 0 232
1 0 4294967295int unsigned 10
10 100
double double
64 100 double
100

200

7.1

200

200 0

0 342
0342

22222222222222222222
33333333333333333333

Output Sample:
55555555555555555555

200 C/C++

unsigned an[200] 200 an[0]


an[1]an[2]

10 unsigned an1[201]
unsigned an2[200] an1
an1 201 200 201

144

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.

#include <stdio.h>
#include <string.h>
#define MAX_LEN 200
int an1[MAX_LEN+10];
int an2[MAX_LEN+10];
char szLine1[MAX_LEN+10];
char szLine2[MAX_LEN+10];
int main()
{
scanf("%s", szLine1);
scanf("%s", szLine2);
int i, j;
// memeset an1 sizeof(an1) 0
//sizeof(an1) an1
//memset string.h
memset( an1, 0, sizeof(an1));
memset( an2, 0, sizeof(an2));
// szLine1 an1
//an1[0]
int nLen1 = strlen( szLine1);
j = 0;
for( i = nLen1 - 1;i >= 0 ; i --)
an1[j++] = szLine1[i] - '0';
int nLen2 = strlen(szLine2);
j = 0;
for( i = nLen2 - 1;i >= 0 ; i --)
an2[j++] = szLine2[i] - '0';
for( i = 0;i < MAX_LEN ; i ++ ) {
an1[i] += an2[i];
//
if( an1[i] >= 10 ) {
//
an1[i] -= 10;
an1[i+1] ++;
//
}
}
bool bStartOutput = false;
// 0
for( i = MAX_LEN; i >= 0; i -- ) {
if( bStartOutput)
printf("%d", an1[i]); // 0
else if( an1[i] ) {
printf("%d", an1[i]);
145

45.

bStartOutput = true; // 0 0
}

46.
}
47.
//-------------------------------------------------------------------------------48.
return 0;
49. }

1.

2. 25 unsigned 8
unsigned 8char int Ascii
09 Ascii 8 0 8
7.1
47

bug

7.2

200

200 0

0 342
0342

12345678900
98765432100

1219326311126352690000

unsigned an1[200] unsigned an2[200]


aResult[400] aResult aResult 400
200 400 an1[0], an2[0], aResult[0]

83549
146

835959 45 139 27 1089 72 100


8359 aResult

7-2-1
45 45 20 10 aResult[1]+=20

7-2-2
43 43 12 100 aResult[2]+= 12

7-2-3
48 48 32 1000 aResult[3]+= 32

7-2-4
aResult[0]aResult[0] 5
4 aResult[1]aResult[1] 51 1 5 aResult[2]
aResult 1

7-2-5
i j
i+j i, j 0

1. #include <stdio.h>
2. #include <string.h>
3. #define MAX_LEN 200
4. unsigned an1[MAX_LEN+10];
5. unsigned an2[MAX_LEN+10];
6. unsigned aResult[MAX_LEN * 2 + 10];
7. char szLine1[MAX_LEN+10];
8. char szLine2[MAX_LEN+10];
9. int main()
10. {
11.
gets( szLine1); //gets
12.
gets( szLine2);
147

13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50. }

int i, j;
int nLen1 = strlen( szLine1);
memset( an1, 0, sizeof(an1));
memset( an2, 0, sizeof(an2));
memset( aResult, 0, sizeof(aResult));
j = 0;
for( i = nLen1 - 1;i >= 0 ; i --)
an1[j++] = szLine1[i] - '0';
int nLen2 = strlen(szLine2);
j = 0;
for( i = nLen2 - 1;i >= 0 ; i --)
an2[j++] = szLine2[i] - '0';
for( i = 0;i < nLen2; i ++ )

{ // an1 an2
// an1
for( j = 0; j < nLen1; j ++ ) // an1 an2
aResult[i+j] += an2[i]*an1[j]; // i, j i+j

}
//
for( i = 0; i < MAX_LEN * 2; i ++ ) {
if( aResult[i] >= 10 ) {
aResult[i+1] += aResult[i] / 10;
aResult[i] %= 10;
}
}
//
bool bStartOutput = false;
for( i = MAX_LEN * 2; i >= 0; i -- )
if( bStartOutput)
printf("%d", aResult[i]);
else if( aResult[i] ) {
printf("%d", aResult[i]);
bStartOutput = true;
}
if(! bStartOutput )
printf("0");
return 0;

7.2

148

7.3

1 n 2 1 2
100

3
2405337312963373359009260457742057439230496493930355595797660791082739646
2987192585318701752584429931160870372907079248971095012509790550883793197894
10000000000000000000000000000000000000000
10000000000
5409656775097850895687056798068970934546546575676768678435435345
1

0
1000000000000000000000000000000
5409656775097850895687056798068970934546546575676768678435435345

7546 23 0
23 100 2300 3 646 300 646
230 2 186 20 186 23 8
328

10 100 0

1. #include <stdio.h>
2. #include <string.h>
3. #define MAX_LEN
200
4. char szLine1[MAX_LEN + 10];
5. char szLine2[MAX_LEN + 10];
6. int an1[MAX_LEN + 10];
//, an1[0]
7. int an2[MAX_LEN + 10];
//, an2[0]
8. int aResult[MAX_LEN + 10]; //aResult[0]
9. /* Substract nLen1 p1 nLen2 p2
10. p1

149

11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.

-1 0
p1[0]p2[0] */
int Substract( int * p1, int * p2, int nLen1, int nLen2)
{
int i;
if( nLen1 < nLen2 )
return -1;
// p1 p2 -1
bool bLarger = false;
if( nLen1 == nLen2 ) {
for( i = nLen1-1; i >= 0; i -- ) {
if( p1[i] > p2[i] )
bLarger = true;
else if( p1[i] < p2[i] ) {
if ( ! bLarger )
return -1;
}
}
}
for( i = 0; i < nLen1; i ++ ) { //
p1[i] -= p2[i]; // i>=nLen2 p2[i] 0
if( p1[i] < 0 ) {
p1[i]+=10;
p1[i+1] --;
}
}
for( i = nLen1 -1 ; i >= 0 ; i-- )
if( p1[i] )
return i + 1;
return 0;
}
int main()
{
int t, n;
char szBlank[20];
scanf("%d", &n);
for( t = 0; t < n; t ++ ) {
scanf("%s", szLine1);
scanf("%s", szLine2);
int i, j;
int nLen1 = strlen( szLine1);
memset( an1, 0, sizeof(an1));
memset( an2, 0, sizeof(an2));
memset( aResult, 0, sizeof(aResult));
150

55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.

j = 0;
for( i = nLen1 - 1;i >= 0 ; i --)
an1[j++] = szLine1[i] - '0';
int nLen2 = strlen(szLine2);
j = 0;
for( i = nLen2 - 1;i >= 0 ; i --)
an2[j++] = szLine2[i] - '0';
if( nLen1 < nLen2 ) {
printf("0\n");
continue;
}
nLen1 = Substract( an1, an2, nLen1, nLen2) ;
if( nLen1 < 0 ) {
printf("0\n");
continue;
}
else if( nLen1 == 0) {
printf("1\n");
continue;
}
aResult[0] ++; // 1
// nLen1
int nTimes = nLen1 - nLen2;
if( nTimes < 0) //
goto OutputResult;
else if( nTimes > 0 ) {
// an2 10 an1
for( i = nLen1 -1; i >= 0; i -- ) {
if( i >= nTimes )
an2[i] = an2[i-nTimes];
else
an2[i] = 0;
}
}
nLen2 = nLen1;
for( j = 0 ; j <= nTimes; j ++ ) {
int nTmp;
//
// an2(10 nTimes )
// an2(10 nTimes-1 )......
while( (nTmp = Substract(an1, an2+j, nLen1, nLen2-j)) >= 0) {
nLen1 = nTmp;
aResult[nTimes-j]++; // 1
}
151

99.
}
100.
OutputResult:
101.
//
102.
for( i = 0; i < MAX_LEN; i ++ ) {
103.
if( aResult[i] >= 10 ) {
104.
aResult[i+1] += aResult[i] / 10;
105.
aResult[i] %= 10;
106.
}
107.
}
108.
//
109.
bool bStartOutput = false;
110.
for( i = MAX_LEN ; i >= 0; i -- )
111.
if( bStartOutput)
112.
printf("%d", aResult[i]);
113.
else if( aResult[i] ) {
114.
printf("%d", aResult[i]);
115.
bStartOutput = true;
116.
}
117.
if(! bStartOutput )
118.
printf("0\n");
119.
printf("\n");
120.
}
121.
return 0;
122. }

an1, an2 aResult 0

10000 87
1

7.4

2p-1 P P
2p-1 1998 37
P=3021377 909526
P (1000<P<3100000) , 2p-1 500

P(1000<P<3100000)

1 2p-1 2-11 2p-1 500


50 10 500 0
2p-1 P
152


1279

386
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000104079321946643990819252403273640855
38615262247266704805319112350403608059673360298012
23944173232418484242161395428100779138356624832346
49081399066056773207629241295093892203457731833496
61583550472959420547689811211693677147548478866962
50138443826029173234888531116082853841658502825560
46662248318909188018470682222031405210266984354887
32958028878050869736186900714720710555703168729087

2p-1 2p-1 2, 4, 6, 8 2p-1 2p


C/C++ math.h 10 double
log10(double x) 2p-1
2p
p>0 p

p = a020 +a121+ a222+ an-12n-1 + 2n


ai 1 0

2p 1 21 a0 1 21
22 a1 1 22 24 a2 1 24
i (i 0 nan 1)

ai 1

500 500

4 10000 10000
1 int a 6373384
a[0]=3384, a[1]=637
500
500 4
125
()
1.

#include <stdio.h>
153

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.

#include <memory.h>
#define LEN 125 // 4 125
#include <math.h>
/* Multiply a * b
500 a
*/
void Multiply(int* a, int* b)
{
int i, j;
int nCarry; //
int nTmp;
int c[LEN]; // 500
memset(c, 0, sizeof(int) * LEN);
for (i=0;i<LEN;i++)
{
nCarry=0;
for (j=0;j<LEN-i;j++)
{
nTmp=c[i+j]+a[j]*b[i]+nCarry;
c[i+j]=nTmp%10000;
nCarry=nTmp/10000;
}
}
memcpy( a, c, LEN*sizeof(int));
}
int main()
{
int i;
int p;
int anPow[LEN];
// 2
int aResult[LEN]; // 500
scanf("%d", & p);
printf("%d\n", (int)(p*log10(2))+1);
// 2 2^(2^0)(a^b a b )
// 1
anPow[0]=2;
aResult[0]=1;
for (i=1;i<LEN;i++)
{
anPow[i]=0;
aResult[i]=0;
}
// 2 p
while (p>0) { // p = 0 p
if ( p & 1 ) // p 1
Multiply(aResult, anPow);
154

46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64. }

p>>=1;
Multiply(anPow, anPow);
}
aResult[0]--; //2 p 1
//
for (i=LEN-1;i>=0;i--) {
if (i%25==12)
printf("%02d\n%02d", aResult[i]/100,
aResult[i]%100);
else {
printf("%04d", aResult[i]);
if (i%25==0)
printf("\n");
}
}
return 0;

17j LEN - i - 1 b[i]a[j] c[i+j]i+j


LEN c[i+j] c
18: b[i]a[j] c[i+j] c[i+j-1]

19: c 10000 1 c[i+j] 10000


20:
43 48 ai(i 0 ) 1,

54: 124 4
50 i%25 12 i

55: %02d 2 0
2
58: 0 4
59:

p 2

7.4

155

1. 2 N
N(N<=100) 2 N

2.
2 100

3.
a1, a2, ... an b1, b2, ... bn
N N a1 p1 N a2 p2 N an
pn M = p1 * b1 + p2 * b2 + ... + pn * bn M a1 p1M a2
p2M an pn b1, b2, ... bn 1 <= n <= 10
a1, a2, ... an 50

4.
R ( 0.0 < R < 99.999 ) , R n n
0 < n <= 25

156


8.1

(
) N
n(1) n [2n(2)n N
n {2}{2i+11 i 2i+1 N}
n [2n
(1)(2)

N (1) n [2n

n [2n
n

2
N {22i+11 i 2i+1 N}
[2, N)

N n n
{22i+11 i 2i+1 N}

8.2

23
28 33

157

10 12 2 3

p, e, i d p, e, i
d p, e, i
365, 21252

0000
0 0 0 100
5 20 34 325
4567
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Case 1: the next triple peak occurs in 21252 days.


Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

x x
d 21252
1) (x-p) % 23 = 0
2) (x-e) % 28 = 0
3) (x-i) % 33 = 0
[d+121252]
[d+121252] 1)
2)3) 2)3) 1/23
3)

1)
2)
3)
4)
5)

p, e, i, d
d+1 21252 1) a
a 21252 2) b
b 21252 3) x
x-d

1. #include <stdio.h>
2.
158

3. void main(){
4.
int p,e,i,d,j,no=1;
5.
scanf("%d%d%d%d", &p, &e, &i, &d);
6.
while(p!=-1 && e!=-1 && i!=-1 && d!=-1){
7.
for(j=d+1; j<21252; j++)
8.
if ((j-p)%23 == 0) break;
9.
for( ; j<21252; j=j+23)
10.
if ((j-e)%28 == 0) break;
11.
for( ; j<21252; j=j+23*28)
12.
if ((j-i)%33 == 0) break;
13.
printf("Case %d", no);
14.
printf(": the next triple peak occurs in %d days.\n", j-d);
15.
scanf("%d%d%d%d", &p, &e, &i, &d);
16.
no++;
17.
}
18. }

8.3

12 11 1

A-L

"up'', "down'', "even'',

1
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even

K is the counterfeit coin and it is light.

159


xwx 12
w 2 (xw ) 24
(xw )

"even'' x
w "up'' x
"down'' x
w "up'' x
"down'' x

1)
x
x
x
2)
6
7\0

char left[3][7], right[3][7], result[3][7];

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

#include <stdio.h>
#include <string.h>
char left[3][7], right[3][7], result[3][5];
bool isHeavy( char );
bool isLight( char );
void main() {
int n;
char c;
scanf("%d", &n);
while ( n > 0 ) {
for ( int i = 0; i < 3; i++ )
scanf("%s %s %s", left[i], right[i], result[i]);
for ( c = 'A'; c <= 'L'; c++ ) {
if ( isLight(c) ) {
printf("%c is the counterfeit coin and it is light.\n", c);
break;
}
if ( isHeavy(c) ) {
printf("%c is the counterfeit coin and it is heavy.\n", c);
break;
}
}
n--;
}
}
160

29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.

bool isLight( char x ) { // x


int i;
for ( i = 0; i < 3; i++ ) //
switch( result[i][0] ) {
case 'u': if( strchr(right[i], x) == NULL ) return false;
break;
case 'e': if(strchr(right[i], x) != NULL || strchr(left[i], x) != NULL)
break;
case 'd': if(strchr(left[i], x) == NULL) return false;
break;
}
return true;
}

return false;

bool isHeavy( char x ){ // x


int i;
for ( i = 0; i < 3; i++ ) //
switch( result[i][0] ) {
case 'u': if( strchr(left[i], x) == NULL) return false;
break;
case 'e': if(strchr(right[i], x) != NULL || strchr(left[i], x) != NULL)
break;
case 'd': if(strchr(right[i], x) == NULL) return false;
break;
}
return true;
}

return false;

1
\0 strlen()

8.4

a3 = b3 + c3 + d3 123 = 63 + 83 + 103
N (N100)(a, b, c, d) a3 = b3 + c3 + d3 1<a, b, c, d N

N (N100)

a a
bcd b c
d

24

161

Cube = 6, Triple = (3,4,5)


Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)
:
4 (abcd)
a3 = b3 + c3 + d3
a a
b a b c

1.

2.

(abcd)(1) a6 a 5 bcd
3 1 (5234)(2) 1< b < c < d
(3) (abcd)
bcd a
[2 N] i

[2 N](abcd)

#include <stdio.h>
#include <math.h>

1.
2.
3.
4. void main( )
5. {
6.
int n, a, b, c, d;
7.
long int cube[101];
8.
scanf("%d ", &n );
9.
for ( int i = 1 ; i <= n ; i++ )
10.
cube[i] = i*i*i;
11.
for ( a = 6 ; a <= n ; a++ )
12.
for ( b = 2 ; b < a - 1; b++ ) {
13.
if (cube[a] < cube[b] + cube[b+1] + cube[b+2]) break;
14.
for ( c = b+1 ; c < a ; c++ )
15.
{
16.
if ( cube[a] < cube[b] + cube[c] + cube[c+1]) break;
17.
for ( d = c+1; d < a ; d++ )
18.
if ( cube[a] == cube[b] + cube[c] + cube[d] )
19.
printf( "Cube = %d, Triple = (%d,%d,%d)\n", a, b, c, d);
20.
}
21.
}
22.
23. }

1~N (a, b, c, d)
a3b3c3d3
162

b a3 b3 + (b+1)3 + (b+2)3 c d
c a3 b3 + c3 + (c+1)3 d
a3 b3 + c3 + d3

8.5

6 5
()

3 4 5
8-1 X
8-2 2
35 2 4

1) 2 1

2)
3) 1 2 1
1234 12345
5

8-1

8-2

163


N 5 6
0 10 1

PUZZLE #m m
5 1 0

2
011010
100111
001001
100101
011100
001010
101011
001011
101100
010100

PUZZLE #1
101001
110101
001011
100100
010000
PUZZLE #2
100111
110000
000100
110101
101101

8-3
puzzle[i][j](i, j)1 0
press[i][j](i, j)1
0 0 0 7
30
press 230

press
press press

(1, j)(2, j) press[2][j] 1


(1, j)(2, j) press[2][j] 0
press

164

press
press
press 26 press
26 press

(0

0) (0

1) (0

2) (0

3) (0

(1

0) (1

1) (1

2) 1

6) (0

7)

(1

6) (1

7)

(2

0) (2

1) (2

2) (2

3) (2

4) (2

5) (2

6) (2

7)

(3

0) (3

1) (3

2) (3

3) (3

4) (3

5) (3

6) (3

7)

(4

0) (4

1) (4

2) (4

3) (4

4) (4

5) (4

6) (4

7)

(5

0) (5

1) (5

2) (5

3) (5

4) (5

5) (5

6) (5

7)

3)

(1

4) (0
4) 1

5) (0
5)

8-3

1. press press[1][1]~ press [1][6]

000000
100000
010000
110000
001000

111111
2. press press
press

2 puzzle[6][8] press[6][8]puzzle[6][8] 0 0
7 puzzle[i][j](i, j)1 0
press[6][8] 0 0 7 0press[i][j]
(i, j)1 0
4~17 guess()(1) press puzzle
press 1~4 (2)
press 5 true
false
19~38 enumate() press press[1][1]~ press
[1][6] guess()
165


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.

#include <stdio.h>
int puzzle[6][8], press[6][8];
bool guess( ){
int c, r;
for ( r = 1; r < 5; r++ )
for ( c = 1; c < 7; c++ )
press[r+1][c] =
(puzzle[r][c] + press[r][c] + press[r-1][c] + press[r][c-1] + press[r][c+1]) % 2;
for(c=1; c<7; c++)
if ( (press[5][c-1] + press[5][c] + press[5][c+1] + press[4][c]) % 2 != puzzle[5][c] )
return(false);
return( true );
}
void enumate( )
{
int c;
bool success;
for ( c = 1; c < 7; c++)
press[1][c] = 0;
while( guess() == false ) {
press[1][1]++;
c = 1;
while ( press[1][c] > 1 ) {
press[1][c] = 0;
c++;
press[1][c]++;
}
}
return;
}
void main()
{
int cases, i, r, c;
scanf("%d", &cases);
for ( r = 0; r < 6; r++ )
press[r][0] = press[r][7] = 0;
for ( c = 1; r < 7; r++ )
press[0][c] = 0;
for ( i = 0; i < cases; i++ ) {
for ( r = 1; r < 6; r++ )
for ( c = 1; c < 7; c++ )
scanf("%d", &puzzle[r][c]);
enumate( );
printf("PUZZLE #%d\n", i+1);
166

57.
for ( r = 1; r < 6; r++ ) {
58.
for ( c = 1; c < 7; c++ )
59.
printf("%d ",press[r][c]);
60.
printf("\n");
61.
}
62.
}
63. }
64.

56 68
press[r+1][c]
press[r+1][c] = ( puzzle[r][c] + press[r][c] + press[r-1][c] + press[r][c-1] + press[r][c+1] ) mod 2
press

8.6

8-4
8-5
8-6

8-4

6
8-5

8-6

8-7 8-8

167

3
1

8-7

8-8

8-8
3 3
8-8
(2, 1)(6, 1)
(2, 3)(3, 4)(6, 6)
(2, 1)(2, 3)(2, 5)(2, 7)
(2, 6)
8-8 7 6

RC
1RC5000 N 3N5000
N (1~R)(1~C)

67
14
21
66
42
25
26
27
34
61
62
23
63
64
65
67

7
168

L 3
(X1Y1)(X2Y2)

dx=X2-X1dy=Y2-Y1X0=X1-dxY0=Y1- dyX3=X2 + dxY3=Y2 + dy


(X0Y0)(X1Y1)
(X3Y3) L 3
Xi=X0 + idxYi=Y1 + idy(i3)(XiYi)(XiYi)

(X1Y1)(X2
Y2)(X1Y1)(X2Y2)

struct PLANT {//


int x; //
int y; //
}

(X1Y1)(X2Y2)
(X1Y1)(X2Y2)
(X2Y2)
(XY)(XY)
(X1Y1)(X2Y2)

PLANT plants[5001]
plants /()
plants (XY)(XY) plants
(1)(2) plants plants (3)
plants plants
(XY) Log2N
N

1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
#include <stdlib.h>
int r, c, n;
struct PLANT {
int x, y;
};

169

8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.

PLANT plants[5001];
PLANT plant;
int myCompare( const void *ele1, const void *ele2 );
int searchPath(PLANT secPlant, int dX, int dY);
void main( )
{
int i, j, dX, dY, pX, pY, steps, max = 2;
scanf("%d%d", &r, &c);
scanf("%d", &n);
for ( i = 0; i < n; i++ )
scanf("%d %d", &plants[i].x, &plants[i].y);
qsort(plants, n, sizeof(PLANT), myCompare);
for ( i = 0; i < n - 2; i++ )
for ( j = i + 1; j < n - 1; j++ ) {
dX = plants[j].x - plants[i].x;
dY = plants[j].y - plants[i].y;
pX = plants[i].x - dX;
pY = plants[i].y - dY;
if ( pX <= r && pX >= 1 && pY <= c && pY >= 1 )
continue;
if ( plants[i].x + max * dX > r )
break;
pY = plants[i].y + max * dY;
if ( pY > c || pY < 1)
continue;
steps = searchPath( plants[j], dX, dY );
if ( steps > max ) max = steps;
}
if ( max == 2 ) max = 0;
printf("%d\n", max);
return;
}
int myCompare( const void *ele1, const void *ele2 )
{
PLANT *p1, *p2;
p1 = (PLANT*) ele1;
p2 = (PLANT*) ele2;
if ( p1->x == p2->x ) return( p1->y - p2->y );
return ( p1->x - p2->x );
}
int searchPath( PLANT secPlant, int dX, int dY )
{
PLANT plant;
int steps;
plant.x = secPlant.x + dX;
plant.y = secPlant.y + dY;
steps = 2;
while ( plant.x <= r && plant.x >= 1 && plant.y <= c && plant.y >= 1 ) {
if ( !bsearch(&plant, plants, n, sizeof(PLANT), myCompare) ) {
steps = 0;
break;
}
plant.x += dX;
170

65.
66.
67.
68.
69. }

plant.y += dY;
steps++;
}
return( steps );

C ()
C
C

qsort
bsearch

1. (ai2739)
a b x x <= logab < x + 1 x
x 20

2. (ai2747)
n (0 n 100) a1a2a3 a1 + a2 + a3

0 a1a2a3 n
a1 + a2 2
a2 + a3 3
a1 + a2 + a3 5

3. (ai2813)
N*N
Bob
(i, j) (i-1, j) (i+1, j) (i, j-1) (i, j+1)
Bob

4. (ai1183)

(1) n x 2 n1
2n 1
n 0

arctan( x)

( 0 <= x <= 1)

(1)

PI PI
PI = 4 arctan(1) =4 (1 -

1
1
1
1
1
+
+
+ ...)
3
5
7
9 11
171

(2)


tan(a + b) = [tan(a) + tan(b)] [1 - tan(a) tan(b)]

arctan(p) + arctan(q) = arctan[(p + q) (1- p q)]

(3)
(4)

1
1
q= (p + q) (1- p q) = 1
3
2
1
1 1
1 1
1
arctan( ) + arctan( )=arctan[( + ) (1 - )] = arctan(1)
3
2
2 3
2 3
1 1
arctan(1)
2 3
p =

(4)
arctan(

1
1
1
)=arctan( )+arctan( ) ab c
a
b
c

a1 <= a <= 60000 bc a


b + c

5. (ai2814)
9 3*3 12 3 6
9 8-9 9 8-10
90 9
9 12

8-9

ABDE

ABC

BCEF

ADG

BDEFH

CFI

DEGH

GHI

EFHI

8-10

172


9.1
n ,
n :

1.
2.
3.

int n, m = 1;
for(int i = 2; i <= n; i++) m *= i;
printf(%d %d\n, n, m);

n n n-1 n
1. int factorial(int n){
2. if(n <= 0) return(-1);
3. if(n == 1) return 1;
4. else return n*factorial(n - 1);
5. }

n n n-1

x f(x)
g f(x) = g(f(x-1)) f(0) f(0) g f(x)
xyz x-1 x - x1

9.2

1
2 a a

1 n n 1
a(1 <= a <= 20)
173


n a

4
5
2
19
1

5
1
4181
1
a
n f(n) f(n) = f(n-1)+f(n-2) f(1)=1f(2)=1

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

#include <stdio.h>
int f(int a){
if( a == 1 || a == 2 ) return 1;
return f(a - 1) + f(a - 2);
}
void main()
{
int n;
scanf("%d",&n);
for(int i = 1; i <= n ; i++){
int a;
scanf("%d", &a);
printf("%d\n", f(a));
}
}

9.2 n n>10 f

A) n

B) n

C) 2

D) n!

174

9.3

9-1
1, 2, 3, ...
1 10 (10, 5, 2, 1)
4 (4, 2, 1) 1 1
(1) x y(x1, x2, ... ,1)(y1,
i j xi yj
y2, ... ,1) x = x1y = y1
xi = yj xi + 1 = yj + 1xi + 2 = yj + 2... x y xi
yj

x y 1000

xi

10 4

2
175

common(x, y) x y
x y 1) x y
common(x, y) x y2x y common(x, y) common(x/2, y)
3x y common(x, y) common(x y/2)

1. #include <stdio.h>
2. int common(int x, int y){
3.
if(x == y) return x;
4.
if(x > y) return common(x / 2, y);
5.
return common(x, y / 2);
6. }
7. void main()
8. {
9.
int m, n, result;
10.
scanf("%d%d", &m, &n);
11.
printf("%d\n", common(m, n));
12. }

9.4

2 + 3
+ 2 3
(2 + 3) * 4 * + 2 3 4
+ - * /

176


* + 11.0 12.0 + 24.0 35.0

1357.000000

1
2+
3-4* 5/ 2
+-*/

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.

#include <stdio.h>
#include<math.h>
double exp(){
char a[10];
scanf("%s", a);
switch(a[0]){
case'+': return exp( ) + exp( );
case'-': return exp( ) - exp( );
case'*': return exp( ) * exp( );
case'/': return exp( ) / exp( );
default: return atof(a);
}
}
void main()
{
double ans;
ans = exp();
printf("%f", ans);
}

atof()
9.4

177

9.5

M N
K 511 151

t0 <= t <= 20
M N
1<=MN<=10

M N K

1
73

N M N-1 M
N M
N M-N

f(m, n) m n n n>m
n-m if(n>m) f(m,n) =
f(m,m) n <= m
f(m , n) = f(m , n-1);
f(m , n) = f(m-n , n)
f(m,n) =f(m,n-1)+f(m-n,n)
1.
2.
3.
4.
5.

int f(int m , int n){


if(n == || m == 0) return 1;
if(n > m) return f (m, m);
return f (m , n-1)+f (m-n , n);
}

n=
n
n==1; m n>m return f(m , m)
m==0

1.
2.
3.

#include <stdio.h>
int count(int x, int y){
if(y == 1 || x == 0) return 1;
178

4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

if(x < y) return count(x, x);


return count(x, y - 1) + count(x - y, y);
}
void main()
{
int t, m, n;
scanf("%d", &t);
for(int i = 0; i < t; i++){
scanf("%d%d", &m, &n);
printf("%d\n", count(m, n));
}
}

9.6

W H x
y W H 20 H W

1
.

2
#

3@

(
)

69
....#.
.....#
......
......
......
179

......
......
#@...#
.#..#.
00

45

.#
f(x, y)(x,y)
f(x, y) = 1 + f(x - 1, y) + f(x + 1, y) + f(x, y - 1) + f(x, y + 1)

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.

#include <stdio.h>
int W, H;
char z[21][21];
int f(int x, int y){
if(x < 0 || x >= W || y < 0 || y >= H) //
return 0;
if(z[x][y] == '#')
return 0;
else{
z[x][y] = '#'; //
return 1 + f(x - 1, y) + f(x + 1, y) + f(x, y - 1) + f(x, y + 1);
}
}
void main()
{
int i, j, num;
while(scanf("%d %d", &H, &W) && W != 0 && H != 0){
num = 0;
for(i = 0; i < W; i++)
//
scanf("%s", z[i]);
for(i = 0; i < W; i++)
for(j = 0; j < H; j++)
if(z[i][j] == '@')
printf("%d\n", f (i , j));
}
}
180

scanf

f(x)

9.7

8 8 * 8
8 a a=b1b2...b8
bi i 8 92 92
b b x
y x y

1 n n 1
b(1 <= b <= 92)

n b

2
1
92

15863724
84136275

92 92
181

8*8
92 8

7 8
1 8

1. #include <stdio.h>
2. #include <math.h>
3.
4. int queenPlaces[92][8]; // 92
5. int count = 0;
6. int board[8][8]; //
7. void putQueen(int ithQueen); //
8.
9. void main()
10. {
11.
int n, i, j;
12.
for(i = 0; i < 8; i++){ //
13.
for(j = 0; j < 8; j++)
14.
board[i][j] = -1;
15.
for(j = 0; j < 92; j++)
16.
queenPlaces[j][i] = 0;
17.
}
18.
putQueen(0); // 0 queenPlaces
19.
scanf("%d", &n);
20.
for(i = 0; i < n; i++){
21.
int ith;
22.
scanf("%d", &ith);
23.
for(j = 0; j < 8; j++)
24.
printf("%d", queenPlaces[ith - 1][j]);
25.
printf("\n");
26.
}
27. }
182

28. void putQueen(int ithQueen){


29.
int i, k, r;
30.
if(ithQueen == 8){
31.
count ++;
32.
return;
33.
}
34.
for(i = 0; i < 8; i++){
35.
if(board[i][ithQueen] == -1){
36.
//
37.
board[i][ithQueen] = ithQueen;
38.
// ith i+1
39.
// i ith
40.
for(k = count; k < 92; k++)
41.
queenPlaces[k][ithQueen] = i + 1;
42.
//
43.
for(k = 0; k < 8; k++)
44.
for(r = 0; r < 8; r++)
45.
if(board[k][r] == -1 &&
46.
(k == i || r == ithQueen || abs(k - i) == abs(r - ithQueen)))
47.
board[k][r] = ithQueen;
48.
//
49.
putQueen(ithQueen + 1);
50.
//
51.
for(k = 0; k < 8; k++)
52.
for(r = 0; r < 8; r++)
53.
if(board[k][r] == ithQueen) board[k][r] = -1;
}
54.
55.
}
56. }

45 135

1. #include <stdio.h>
2. int record[92][9], mark[9], count = 0; //record mark
3. bool range[9], line1[17], line2[17]; //45 135
4. void tryToPut(int ); //
5. void main()
183

6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.

{
int i, testtimes, num;
scanf("%d", &testtimes);
for(i = 0; i <=8; i++)
range[i] = true;
for(i = 0; i < 17; i ++)
line1[i] = line2[i] = true;
tryToPut(1);
while(testtimes --){
scanf("%d", &num);
for(i = 1; i <=8; i++)
printf("%d", record[num - 1][i]);
printf("\n");
}
}
void tryToPut(int i){
if(i > 8){ //
for(int k = 1; k < 9; k ++)
record[count][k] = mark[k];
count ++;
}
for(int j=1; j<=8; j++){
if(range[j] && line1 [i + j] && line2[i - j + 9]){ //
//
mark[i] = j;
range[j] = line1[i + j] = line2[i - j + 9] = false;
tryToPut(i + 1);
range[j] = line1[i + j] = line2[i - j + 9] = true;
}
}
}

1. #include <stdio.h>
2. int ans[92][8], n, b, i, j, num, hang[8];
184

3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

void queen(int i){


int j, k;
if(i == 8){ //
for(j = 0; j < 8; j++) ans[num][j] = hang[j] + 1;
num++;
return;
}
for (j=0; j<8; j++){ // i
for(k=0; k<i; k++) // i
if( hang[k] == j || (k - i) == (hang[k] - j) || (i - k) == (hang[k] - j )) break;
if (k == i) { // i i+1
hang[i] = j;
queen(i + 1);
}
}
}
void main( ){
num=0;
queen(0);
scanf(%d, &n);
for(i = 0; i < n; i++){
scanf(%d, &b);
for(j = 0; j < 8; j++) printf(%d, ans[b - 1][j]);
printf(\n);
}
}

8 8

9.8

50

185

64

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

6
5

k M p
L L M p M L N
k N L
L p p
L k
L k
M / L L 1 M / L
M / L L
k M / L L
L f(k, left, L, N)
k N L left left
k t t left
f(k-1, left-t, L, N) k-1
N L left-t k
0 k 0 left==N==0 k N
L left k
left
left==0 left
L N 1 L
L

k
L L

L
L
L

186

L
L

1. #include <stdio.h>
2.
3. #include <stdio.h>
4. #include <stdlib.h>
5. bool concatenate(int, int, int, int); //
6. int compare( const void *arg1, const void *arg2 ) // qsort
7. {
8.
return *(int *)arg2 - *(int *)arg1;
9. }
10. int sticks[100]; //
11. bool used[100]; //
12. void main()
13. {
14.
int n;
15.
scanf("%d", &n);
16.
while(n != 0){
17.
int i, sum = 0, len; //sum
18.
for(i = 0; i < n; i++){ //
19.
used[i] = false;
20.
scanf("%d", &(sticks[i]));
21.
sum += sticks[i];
22.
}
23.
qsort(sticks, n, sizeof(int), compare); //
24.
len = sticks[0]; //len
25.
for(i = len; i <= sum; i++){ //
26.
if (sum % i != 0 ) continue;
//
27.
28.
if ( concatenate(n, n, 0, i) ) {
29.
// n i
30.
printf("%d\n", i);
31.
break;
32.
}
33.
}
34.
scanf("%d", &n);
35.
}
36. }
37.
38.
187

39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.

bool concatenate(int totalSticks, int unusedSticks,int left, int len){


//totalSiticks
//unusedSticks len
//left
//len
int i;
if(unusedSticks == 0 && left == 0) return true;
// 0
// len
if(left == 0) left = len; // 0
for(i=0; i < totalSticks; i++){ //
if(used[i] == true) continue; //
if(sticks[i] > left) continue; //
used[i] = true; //
if(concatenate(totalSticks, unusedSticks - 1, left - sticks[i], len)) return true;
//unusedSticks left
used[i] = false; //
if(sticks[i] == left || left == len) break;
//
//
}
return false;
}

L i L
L i-1 L

L L

9.8

1.

188

1
2
3
4

1
2
3
4
5
6
7
#############################
#
|
#
|
#
|
|
#
#####---#####---#---#####---#
#
#
|
#
#
#
#
#
#---#####---#####---#####---#
#
|
|
#
#
#
#
#
#---#########---#####---#---#
#
#
|
|
|
|
#
#
#############################
9-2
# = Wall
| = No wall
- = No wall

9-2
m *n(m50n50) 0~4

2.
a a = a1 * a2 * a3 * ... * an
1 < a1 <= a2 <= a3 <= ... <= an a = a

3.
Extense n * n
2 .#
Extense ()
Extense A B
(#)

4. 24
4 10 4 4
24

5551 5 * (5 1 / 5) = 24 24
1142 24

5.
Microsoft Windows "explorer"

""

ROOT

189

dir1

file1

file2

file3

dir2

dir3

file1

file1
file2

ROOT 3

6.

w * h

9-3
(1, 3) (4, 4) (2, 3) (3, 4)

7.

1.

2.
190

3.
50 12346
4 1234 6 43 (= 1 + 2 + 34
+ 6) 50 50
1234 6 34 (= 1 + 23 + 4 + 6)
43 1234 6
52 (= 12 + 34 + 6) 50

9-4

1.
2.
3.
15 111
1 11 11 1

8.
*
(n-1) n
()

191

9-5

xi i

n O'

9.

k C

192


10.1

(ai2760)

7
3
8
2
4

8
1

7
5

0
4

4
6

N (1 < N <= 100) N


0 100

5
7
38
810
2744
45265

30

193

D( r, j) r j (rj 1 ) MaxSum(r, j) r
j MaxSum(1, 1)
D(r, j) D(r+1, j) D(r+1, j+1) D(r+1, j)
MaxSum(r, j) MaxSum(r+1, j) + D(r, j) D(r+1, j+1)
MaxSum(r, j) MaxSum(r+1, j+1) + D(r, j) MaxSum(r+1, j)
MaxSum(r+1, j+1)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

#include <stdio.h>
#define MAX_NUM 100
int D[MAX_NUM + 10][MAX_NUM + 10];
int N;
int MaxSum( int r, int j)
{
if( r == N )
return D[r][j];
int nSum1 = MaxSum(r+1, j);
int nSum2 = MaxSum(r+1, j+1);
if( nSum1 > nSum2 )
return nSum1+D[r][j];
return nSum2+D[r][j];

}
main()
{
int m;
scanf("%d", &N);
for( int i = 1; i <= N; i ++ )
for( int j = 1; j <= i; j ++ )
scanf("%d", &D[i][j]);
printf("%d", MaxSum(1, 1));
}
N N=100
MaxSum
MaxSum(r, j) MaxSum(r+1, j)
MaxSum(r, j+1) MaxSum(r+1, j)
MaxSum(r, j)r, j

1
11
121
1331
194

14641
16 8
N 20 +21+222N-1=2N N= 100

MaxSum(r, j)
MaxSum(r, j) MaxSum
MaxSum(r, j) 1 MaxSum
1+2+3+N = N(N+1)/2
MaxSumr, j aMaxSum[N][N]
aMaxSum[r][j] MaxSum(r, j) MaxSum(r, j)
MaxSum aMaxSum[r][j]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.

#include <stdio.h>
#include <memory.h>
#define MAX_NUM 100
int D[MAX_NUM + 10][MAX_NUM + 10];
int N;
int aMaxSum[MAX_NUM + 10][MAX_NUM + 10];
int MaxSum( int r, int j)
{
if( r == N )
return D[r][j];
if( aMaxSum[r+1][j] == -1 ) // MaxSum(r+1, j)
aMaxSum[r+1][j] = MaxSum(r+1, j);
if( aMaxSum[r+1][j+1] == -1) // MaxSum(r+1, j+1)
aMaxSum[r+1][j+1] = MaxSum(r+1, j+1);
if( aMaxSum[r+1][j] > aMaxSum[r+1][j+1] )
return aMaxSum[r+1][j] +D[r][j];
return aMaxSum[r+1][j+1] + D[r][j];
}
main()
{
int m;
scanf("%d", & N);
// aMaxSum -1, MaxSum(r, j)
memset(aMaxSum, -1, sizeof(aMaxSum));
for( int i = 1; i <= N; i ++ )
for( int j = 1; j <= i; j ++ )
scanf("%d", & D[i][j]);
printf("%d", MaxSum(1, 1));
}
195

aMaxSum[N-1]
aMaxSum[1][1]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

#include <stdio.h>
#include <memory.h>
#define MAX_NUM 100
int D[MAX_NUM + 10][MAX_NUM + 10];
int N;
int aMaxSum[MAX_NUM + 10][MAX_NUM + 10];
main()
{
int i, j;
scanf("%d", & N);
for( i = 1; i <= N; i ++ )
for( j = 1; j <= i; j ++ )
scanf("%d", &D[i][j]);
for( j = 1; j <= N; j ++ )
aMaxSum[N][j] = D[N][j];
for( i = N ; i > 1 ; i -- )
for( j = 1; j < i ; j ++ ) {
if( aMaxSum[i][j] > aMaxSum[i][j+1] )
aMaxSum[i-1][j] = aMaxSum[i][j] + D[i-1][j];
else
aMaxSum[i-1][j] = aMaxSum[i][j+1] + D[i-1][j];
}
printf("%d", aMaxSum[1][1]);
}
10.1

196

10.2

n n-1
nm n(m-1)

r=N

(rj)
r j r, j

r1jr+1j+1
(rj)

N(N+1)/2
N(N+1)/2
N
K

K N1, N2,
Nk K array[N1] [N2][Nk]

197

array

10.3

bi b1 < b2 < ... < bS


(a1, a2, ..., aN)(ai1, ai2, ..., aiK) 1 <= i1 < i2 < ... <
iK <= N(1, 7, 3, 5, 9, 4, 8)(1, 7), (3, 4, 8)
4(1, 3, 5, 8).

N (1 <= N <= 1000) N


0 10000

7
1735948

akk=1, 2, 3N

N
N
k
k ak
N MaxLen (k)
ak
MaxLen (1) = 1
MaxLen (k) = Max { MaxLen (i)1<i < k ai < ak k1 } + 1
ak
MaxLen(k) ak
1 ak ak
ak
MaxLen(1) MaxLen(2)
MaxLen(1) MaxLen(2) MaxLen(3)

198

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

#include <stdio.h>
#include <memory.h>
#define MAX_N 1000
int b[MAX_N + 10];
int aMaxLen[MAX_N + 10];
main()
{
int N;
scanf("%d", & N);
for( int i = 1;i <= N;i ++ )
scanf("%d", & b[i]);
aMaxLen[1] = 1;
for( i = 2; i <= N; i ++ ) { // i
int nTmp = 0; // i
for( int j = 1; j < i; j ++ ) { // j
if( b[i] > b[j] ) {
if( nTmp < aMaxLen[j] )
nTmp = aMaxLen[j];
}
}
aMaxLen[i] = nTmp + 1;
}
int nMax = -1;
for( i = 1;i <= N;i ++ )
if( nMax < aMaxLen[i])
nMax = aMaxLen[i];
printf("%d\n", nMax);
}

10.3

10.4 Help Jimmy

"Help Jimmy"

199

10-4

Jimmy 0 1 /
Jimmy 1 /
Jimmy Jimmy MAX

Jimmy

t0 <= t <= 20 NX
YMAXN X Y Jimmy
MAX N
X1[i]X2[i] H[i]H[i]X1[i] X2[i]1
<= N <= 1000-20000 <= X, X1[i], X2[i] <= 200000 < H[i] < Y <= 20000i = 1..N

Jimmy Jimmy
Jimmy

Jimmy

1
3 8 17 20
0 10 8
0 10 13
4 14 3

23

Jimmy

Jimmy

1 (
)

200


Jimmy 0 0
LeftMinTime(k) k RightMinTime(k) k
k
if ( k ) {
if( k h(k) Max)
LeftMinTime(k) = ;
else
LeftMinTime(k) = h(k);
}
else if( k m )
LeftMinTime(k) = h(k)-h(m) +
Min( LeftMinTime(m) + Lx(k)-Lx(m), RightMinTime(m) + Rx(m)-Lx(k));
}
h(i) i Lx(i) i Rx(i) i

h(k)-h(m) k m
Lx(k)-Lx(m) m m Rx(m)-Lx(k) m

RightMinTime(k)
Jimmy 0 0
LeftMinTime(0)

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

()
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#define MAX_N 1000
#define INFINITE 1000000
int t, n, x, y, max;
struct Platform{
int Lx, Rx, h;
};
Platform aPlatform[MAX_N + 10];
int aLeftMinTime[MAX_N + 10];
int aRightMinTime[MAX_N + 10];
int MyCompare( const void * e1, const void * e2 )
{
Platform * p1, * p2;
p1 = (Platform * ) e1;
p2 = (Platform * ) e2;
201

18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.

return p2->h - p1->h;


}
int MinTime( int L, bool bLeft )
{
int y = aPlatform[L].h;
int x;
if( bLeft )
x = aPlatform[L].Lx;
else
x = aPlatform[L].Rx;
for( int i = L + 1;i <= n;i ++ ) {
if( aPlatform[i].Lx <= x && aPlatform[i].Rx >= x)
break;
}
if( i <= n ) {
if( y - aPlatform[i].h > max )
return INFINITE;
}
else {
if( y > max )
return INFINITE;
else
return y;
}
int nLeftTime = y - aPlatform[i].h + x - aPlatform[i].Lx;
int nRightTime = y - aPlatform[i].h + aPlatform[i].Rx - x;
if( aLeftMinTime[i] == -1 )
aLeftMinTime[i] = MinTime(i, true);
if( aRightMinTime[i] == -1 )
aRightMinTime[i] = MinTime(i, false);
nLeftTime += aLeftMinTime[i];
nRightTime += aRightMinTime[i];
if( nLeftTime < nRightTime )
return nLeftTime;
return nRightTime;
}
main()
{
scanf("%d", &t);
for( int i = 0;i < t; i ++ ) {
memset(aLeftMinTime, -1, sizeof(aLeftMinTime));
memset(aRightMinTime, -1, sizeof(aRightMinTime));
scanf("%d%d%d%d", &n, &x, &y, &max);
aPlatform[0].Lx = x;
202

62.
63.
64.
65.
66.
67.
68.
69. }

aPlatform[0].Rx = x;
aPlatform[0].h = y;
for( int j = 1; j <= n; j ++ )
scanf("%d%d%d", & aPlatform[j].Lx, & aPlatform[j].Rx, & aPlatform[j].h);
qsort(aPlatform, n+1, sizeof(Platform), MyCompare);
printf("%d\n", MinTime(0, true));
}

10.4

10.5

Z = < z1, z2, ..., zk > X = < x1, x2, ..., xm >
< i1, i2, ..., ik > j = 1, 2, ... ,k, xij = zj Z = < a, b, f, c > X = < a, b,
c, f, b, c >
X Y X Y
Z Z X Y

200

abcfbc
abfcab
programming
contest
abcd
mnp

4
2
0

s1s2 s1[i] s1 i s2[j]


s2 j 1 0
s1i s1 i
s2j s2 j MaxLen(i, j) s1i s2j

if( i ==0 || j == 0 ) {
MaxLen(i, j) = 0 // 0
}
else if( s1[i] == s2[j] )
MaxLen(i, j) = MaxLen(i-1, j-1 ) + 1;
else {
MaxLen(i, j) = Max( MaxLen(i, j-1), MaxLen(i-1, j));
203

}
MaxLen(i, j) = Max( MaxLen(i, j-1), MaxLen(i-1, j))
MaxLen(i, j) MaxLen(i, j-1) MaxLen(i-1, j) MaxLen(i,
j) MaxLen(i-1, j) s1[i] s1[i] s1i s2j
MaxLen(i, j) MaxLen(i, j-1)
s2[j] s1i s2j MaxLen(i, j)
MaxLen(i, j-1) MaxLen(i-1, j)s1[i] s2[j]
----- s1[i]s2[j]MaxLen(i, j) MaxLen(i, j-1) MaxLen(i-1, j)
MaxLen(i, j) MaxLen(i, j-1) MaxLen(i-1, j)
MaxLen(i, j) = Max( MaxLen(i, j-1), MaxLen(i-1, j))
s1 i s2 j
MaxLen(i, j)
s1 s2

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
char sz1[MAX_LEN];
char sz2[MAX_LEN];
int aMaxLen[MAX_LEN][MAX_LEN];
main()
{
while( scanf("%s%s", sz1+1 ,sz2+1 ) > 0 ) {
int nLength1 = strlen( sz1+1);
int nLength2 = strlen( sz2+1);
int nTmp;
int i, j;
for( i = 0;i <= nLength1; i ++ )
aMaxLen[i][0] = 0;
for( j = 0;j <= nLength2; j ++ )
aMaxLen[0][j] = 0;
for( i = 1;i <= nLength1;i ++ ) {
for( j = 1; j <= nLength2; j ++ ) {
if( sz1[i] == sz2[j] )
aMaxLen[i][j] =
aMaxLen[i-1][j-1] + 1;
else {
int nLen1 = aMaxLen[i][j-1];
int nLen2 = aMaxLen[i-1][j];
if( nLen1 > nLen2 )
aMaxLen[i][j] = nLen1;
else
204

29.
30.
31.
32.
33.
34.
35. }

aMaxLen[i][j] = nLen2;
}
}
}
printf("%d\n", aMaxLen[nLength1][nLength2]);
}

10.6

n n m
m
0 20
m

n mn m
1<=n<=200, 1<=m<=20 m<=n n
2 1
n=m=0

, 'Jury #1', 'Jury #2',

42
12
23
41
62
00

Jury #1
Best jury has value 6 for prosecution and value 4 for defence:
23
205

i V(i)
S(i) f(j, k) j k
f(j, k)
j k f(j, k)-1 f(j, k)
m k f(m, k) (-20m
k 20m)
f(j, k)
f(j, k) f(j-1, x)( -20m x 20m)
f(j-1, x)
f(j, k) ii f(j-1, x)
x+V(i) = k f(j-1, x) f(j-1, x) + S(i)
f(j-1, x) i f(j, k)
f(j, k)
path[j][k] f(j, k) path[j-1][k-V[path[j][k]]
k path[m][k]

f(0, 0) = 0
f(m, k)( -20m k 20m)
f f(j, k)
k
400 0 400
()
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int f[30][1000];
//f[j, k] j k
//" f(j, k)"
int Path[30][1000];
//Path
// f(j, k) Path[j][k]
int P[300]; //
int D[300]; //
int Answer[30]; //
int CompareInt(const void * e1, const void * e2)
{
return * ((int *) e1) - * ((int *) e2);
}
206

18. int main()


19. {
20.
int i, j, k;
21.
int t1, t2;
22.
int n, m;
23.
int nMinP_D; //
24.
int nCaseNo;//
25.
nCaseNo=0;
26.
scanf("%d%d", &n, &m);
27.
while(n+m) {
28.
nCaseNo++;
29.
for(i=1;i<=n;i++)
30.
scanf("%d%d", &P[i], &D[i]);
31.
memset(f, -1, sizeof(f));
32.
memset(Path, 0, sizeof(Path));
33.
nMinP_D=m*20; // 0 m*20
34.
f[0][nMinP_D]=0; // 0 nMinP_D 0
35.
for(j=0;j<m;j++)
{ // j m
36.
for(k=0;k<=nMinP_D*2;k++) //[0, nMinP_D*2]
37.
if(f[j][k]>=0)
{ // f(j, k)
38.
for(i=1;i<=n;i++) // f(j, k)
39.
if(f[j][k]+P[i]+D[i]>f[j+1][k+P[i]-D[i]]) {
40.
t1=j; t2=k;
41.
while(t1>0&&Path[t1][t2]!=i) {
42.
t2-=P[Path[t1][t2]]-D[Path[t1][t2]];
43.
t1--;
44.
}
45.
if(t1==0) {
46.
f[j+1][k+P[i]-D[i]]=f[j][k]+P[i]+D[i];
47.
Path[j+1][k+P[i]-D[i]]=i;
48.
}
49.
}
50.
}
51.
}
52.
i=nMinP_D;
53.
j=0;
54.
while(f[m][i+j]<0&&f[m][i-j]<0)
55.
j++;
56.
if(f[m][i+j]>f[m][i-j])
57.
k=i+j;
58.
else
59.
k=i-j;
60.
printf("Jury #%d\n", nCaseNo);
61.
printf("Best jury has value %d for prosecution and value %d for defence:\n",
207

62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75. }

(k-nMinP_D+f[m][k])/2, (f[m][k]-k+nMinP_D)/2);
for(i=1;i<=m;i++) {
Answer[i]=Path[m-i+1][k];
k-=P[Answer[i]]-D[Answer[i]];
}
qsort(Answer+1, m, sizeof(int), CompareInt);
for(i=1;i<=m;i++)
printf(" %d", Answer[i]);
printf("\n");
printf("\n");
scanf("%d%d", &n, &m);
}
return 0;

39 f(j, k) i
f(j+1, k+P[i]-D[i]) j+1 k+P[i]-D[i]
f(j, k) i
i f(j, k) i
39 f(j+1, k+P[i]-D[i])
f(j, k) i f(j+1, k+P[i]-D[i])

40 44 i f(j, k)
45t1==0 i f(j, k) f(j+1,
k+P[i]-D[i])
46:
47 i Path
53 59 m
k
63 66: f(m, k) Path[m][k] Path[m][k]
f(m, k) Answer
67 Answer

10.6

1.
n

208

2.
KID KID

w s , n1, n2, ... , ns


v1,v2, ..., vsKID

3.
Michael
Michael

1
16
15
14
13

2
17
24
23
12

3
18
25
22
11

4
19
20
21
10

5
6
7
8
9

24-17-16-1 25-24-23-...-3-2-1

4.

5. Pell
Pell a1, a2, a3, ...a1 = 1, a2 = 2, ... , an = 2 * an
1 + an - 2 (n > 2)
k (1 k < 1000000) Pell k 32767

209

6.
2 A = {pi | 1 <= i <= a}B = {qj | 1 <= j <= b}
s (1 <= s <= 10000) pi + qj = s (i, j)

7.
N
K
cm
(1 <= N <= 100001 <= K <= 10000)

8.
N * N (0 < N <= 100)
( 1 * 1)
4 * 4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

9 2
-4 1
-1 8
15

210

5x8+18x5+20x3+100 11-1
first

18

20

100

11-1
4

NULL
first

11.1

NULL
0 NULL

3~10 5~7
9

14 linkHeadlinkTailstudentlinkHeadlinkTail
18 linkHeadlinkTail
NULLstudent
211

20~37
linkHead linkTail
linkTail linkTail

39~48 linkHead

linkHead
student linkHead student student
55~59

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.

#include <stdio.h>
struct Student {//
//
char ID[20];
char name[50];
float score;
//
Student *next;
};
main()
{
Student *linkHead, *linkTail, *student;
float aveScore;
int totalStudents;
linkHead = linkTail = NULL;
//
while (1) {
student = new Student;
scanf("%s%s%f", student->ID, student->name, &student->score);
if (student->score < 0) {
//
delete student;
break;
}
student->next = NULL;
if (linkTail == NULL)
//
linkHead = linkTail = student;
else {
212

33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58. }

//
linkTail->next = student;
linkTail = student;
}
}
//
aveScore = 0.0;
totalStudents = 0;
student = linkHead;
while ( student != NULL ) {
totalStudents++; //
aveScore = aveScore + student->score; //
student = student->next;
}
aveScore = aveScore / totalStudents;
printf("%5.2f\n",aveScore); //
//
while ( linkHead != NULL ) {
student = linkHead;
linkHead = student->next;
delete student;
}
return 0;

1.

2.

NULL 0

3.

link newNode
link link 11-2

newNode->next = link;
link = newNode;
213

link

newNode
1

link

11-2
link newNode v
v
node v 11-3
node newNode
newNode->next = node->next;
node->next = newNode;

newNode

node
11-3

11.2

5x8+18x5+20x3+100 11-4

18

20

11-4
214

100

4~11 12~20
13~17
totalStudentstotalScoreunqualifiedStudents
totalStudents totalScore
unqualifiedStudents
18

24 link
28~32 28 link.head
NULL
35~55
link.head
58~59

62~68

71~75

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
struct Student {//
//
char ID[20];
char name[50];
float score;
//
Student *next;
};
struct StudentList {//
215

12.
//
13.
Student *tail;
14.
int totalStudents;
15.
float totalScore;
16.
int unqualifiedStudents;
17.
//
18.
Student *head;
19. };
20.
21. main()
22. {
23.
StudentList link;
24.
Student *student;
25.
float aveScore;
26.
27.
link.head = NULL;
28.
link.tail = NULL;
29.
link.totalScore = 0;
30.
link.totalStudents = 0;
31.
link.unqualifiedStudents = 0;
32.
33.
//
34.
while (1) {
35.
student = new Student;
36.
scanf("%s%s%f", student->ID, student->name, &student->score);
37.
if (student->score < 0) {
38.
//
39.
delete student;
40.
break;
41.
}
42.
student->next = NULL;
if (link.tail == NULL)
43.
44.
//
45.
link.head = link.tail = student;
46.
else {
47.
//
48.
link.tail->next = student;
49.
link.tail = student;
50.
}
51.
link.totalScore += student->score;
52.
link.totalStudents++;
53.
if (student->score<60) link.unqualifiedStudents++;
54.
}
55.
216

56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77. }
78.

//
aveScore = link.totalScore/link.totalStudents;
printf("%5.2f\n", aveScore); //
//
student = link.head;
printf(", %d \n", link.unqualifiedStudents );
while ( student != NULL ) {
if ( student->score < 60 )
printf("%s
%s\n", student->ID, student->name);
student = student->next;
}
//
while ( link.head != NULL ) {
student = link.head;
link.head = student->next;
delete student;
}
return 0;

12.3

NULL NULL
5x8+18x5+20x3+100
11-5 11-6
first

5
8

18
5

20
3

11-5

217

100
0

5
8

18
5

20
3

100
0

11-5

5~12

37~71
link link
student getPosition(Student *link, Student *student)
student link

student student
student

student student
student
student student

74~80

82~86

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
#include <string.h>
struct Student {//
//
char ID[20];
char name[50];
float score;
//
Student *previous, *next;
};

218

13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.

Student* getPosition(Student *link, Student *student) {


if( strcmp(student->ID, link->ID)<0) {
if (link->previous==NULL)
return (link);
if (strcmp(student->ID, link->previous->ID)<0)
return(getPosition(link->previous, student));
}
if( strcmp(student->ID, link->ID)>0) {
if (link->next==NULL)
return (link);
if (strcmp(student->ID, link->next->ID)>0)
return(getPosition(link->next, student));
}
return(link);
}
main()
{
Student *link, *student, *previous, *next;
link = NULL;
//
while(1) {
student = new Student;
scanf("%s%s%f", student->ID,student->name,&student->score);
if(student->score<0) {
//
delete student;
break;
}
student->next = student->previous = NULL;
if(link == NULL) {
//
link = student;
continue;
}
// student link
link = getPosition(link, student);
if( strcmp(student->ID, link->ID)<0) {
// link
previous = link->previous;
student->next = link;
link->previous = student;
219

57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90. }

if (previous!=NULL) {
student->previous = previous;
previous->next = student;
}
}
if( strcmp(student->ID, link->ID)>0) {
// link
next = link->next;
student->previous = link;
link->next = student;
if (link->next!=NULL) {
student->next = next;
next->previous = student;
}
}
}
//
while (link->previous!=NULL) link = link->previous;
//
while ( link->next!=NULL ) {
printf("%s
%s
%5.2f\n", link->ID, link->name, link->score);
link = link->next;
}
printf("%s
%s
%5.2f\n", link->ID, link->name, link->score);
//
while ( link->previous!=NULL ) {
link = link->previous;
delete (link->next);
}
delete (link);
return 0;

link newNode
node newNodenode node
11-6
newNode->next = node->next;
newNode->previous = node;
node->next ->previous = newNode;
node->next = newNode;

220

newNode

node1

node2
node2

node1

11-6

node
node

54~60

12.4

5x8+18x5+20x3+100
11-7 11-8

link

18

20

11-7

221

100

link

5
8

18
5

20
3

100
0

11-8

N 1 N

1
1 M
1

7
1

11-9

3~6

19~32 19~31
32

35~54

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
struct Monkey {
int ID;
Monkey *next;
};
main()
{
Monkey *link, *monkey, *lastMonkey;
int totalMonkeys, stride, count;
printf(":");
scanf("%d", &totalMonkeys);
222

15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57. }

printf("");
scanf("%d", &stride);
//
link = NULL;
for ( int i=0; i<totalMonkeys; i++) {
monkey = new Monkey;
monkey->ID = i+1;
if (link == NULL)
//
link = lastMonkey = monkey;
else {
//
lastMonkey->next = monkey;
lastMonkey = monkey;
}
}
lastMonkey->next = link; //
//
count = 1;
printf("");
while ( link != NULL ) {
if (link->next == link) {//
printf("%4d\n", link->ID);
delete link;
break;
}
if ( count == stride-1 ) {//link
//
monkey = link->next;
// monkey
link->next = monkey->next;
printf("%4d", monkey->ID);
delete monkey;
count = 0;
}
link = link->next;
count++;
}
return 0;

223


11-10

11-11

11-10 /

11-11

12.5

0
~9


1249630811312610 2006 11
21 11 55 56
2006-11-21 11:55:56 resource created: 1249630811312610

mm_1080_p
1283135310662341 2006 11 21
11 57 57
2006-11-21 11:57:17 mm_1080_p started on resource 1283135310662341

224

mm_1069_p 2006
11 21 12 1 58 1283135310662341

2006-11-21 12:1:58 mm_1069_p finished on resource 1318717414378778

created

started

finished
_

(1)(2)
(3)

resList

resList

225

resList
1

1 1

2 1

m 1

1 2

2 2

m 2

1 3

2 3

m 3

1 n1

2 n2

m nm

4~12 14~19

22~49 computeTimeCost(Task *task)


(1) 1 1 00:00:00
(1) 1 1 00:00:00

22~49 processLog(char log[],


Resource* resList)
created resList

resList

Resource resList

121~139 resList

1.

#include <stdio.h>
226

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.

#include <string.h>
struct Task{
char name[50];
//
char sDate[15]; // xxxx-xx-xx
char sTime[15]; // xx:xx:xx
char eDate[15]; // xxxx-xx-xx
char eTime[15]; // xx:xx:xx
int cost;
//()
Task *next;
};
struct Resource{
char ID[50];
Task *fstTask;
Task *lstTask;
Resource *next;
};
int daysInMon[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void computeTimeCost(Task *task) {//
int sYear, sMon, sDay, sHour, sMin, sSec;
int eYear, eMon, eDay, eHour, eMin, eSec;
int sDays, sSeconds;
int eDays, eSeconds;
int i;
//
sscanf(task->sDate, "%d-%d-%d",&sYear, &sMon, &sDay);
sscanf(task->sTime, "%d:%d:%d",&sHour, &sMin, &sSec);
// sYear 1 1 00:00:00 t1
sDays = sDay-1;
for (i = 1; i < sMon; i++) sDays = sDays + daysInMon[i-1];
sSeconds = ((sDays * 24 + sHour) * 60 + sMin) * 60 +sSec;
//
sscanf(task->eDate, "%d-%d-%d", &eYear, &eMon, &eDay);
sscanf(task->eTime, "%d:%d:%d", &eHour, &eMin, &eSec);
// eYear 1 1 00:00:00 t1
eDays = eDay - 1;
for ( i = 1; i < eMon; i++) eDays = eDays + daysInMon[i-1];
eSeconds = ((eDays * 24 + eHour) * 60 + eMin) * 60 + eSec;
//
227

46.
task->cost = (eYear - sYear) * 365 * 24 * 3600 sSeconds + eSeconds;
47.
48.
return;
49. }
50.
51. Resource *processLog(char log[], Resource* resList) {
52.
char date[30], time[30], taskName[30];
53.
Resource *curRes, *temp;
54.
Task *curTask;
55.
56.
//
57.
if (strstr(log, "created")) {
58.
curRes = new Resource;
59.
curRes->fstTask = curRes->lstTask = NULL;
60.
sscanf( strstr(log, "created") + 9, "%s", curRes->ID);
61.
curRes->next = NULL;
62.
if (resList == NULL)
63.
return(curRes);
64.
temp = resList;
65.
while( temp->next != NULL ) temp = temp->next;
66.
temp->next = curRes;
67.
return(resList);
68.
}
69.
70.
sscanf(log, "%s%s%s", date, time, taskName);
71.
//
72.
curRes = resList;
73.
while (strstr(log, curRes->ID) == NULL ) curRes = curRes->next;
74.
//

75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.

if (strstr(log, "started")) {
curTask = new Task;
strcpy(curTask->name, taskName);
strcpy(curTask->sDate, date);
strcpy(curTask->sTime, time);
curTask->next = NULL;
if (curRes->fstTask == NULL)
curRes->fstTask = curTask;
else
curRes->lstTask->next = curTask;
curRes->lstTask = curTask;
}
//

228

88.
if (strstr(log, "finished")) {
89.
curTask = curRes->fstTask;
90.
while(strcmp(curTask->name, taskName) != 0) curTask = curTask->next;
91.
strcpy(curTask->eDate, date);
92.
strcpy(curTask->eTime, time);
93.
computeTimeCost(curTask);
94.
}
95.
return(resList);
96. }
97.
98. void main()
99. {
100.
FILE *fin, *fout;
101.
char log[80], logFile[30], resultFile[30];
102.
Resource *resList, *curRes;
103.
Task *task;
104.
105.
printf("input log file's name: ");
106.
scanf("%s", logFile);
107.
printf("input the file name for saving results: ");
108.
scanf("%s", resultFile);
109.
110.
resList = NULL;
111.
fin = fopen(logFile, "r");
112.
fgets(log, 80, fin);
113.
while (strlen(log) > 1) {
114.
resList = processLog(log, resList);
115.
fgets(log, 80, fin);
116.
if(feof(fin)) break;
117.
}
118.
fclose(fin);
119.
120.
//
121.
fout = fopen(resultFile,"w");
122.
while (resList != NULL) {
123.
curRes = resList;
124.
resList = curRes->next;
125.
126.
sprintf(log, "Tasks performed on resource %s\n", curRes->ID);
127.
fputs(log, fout);
128.
while (curRes->fstTask != NULL) {
129.
task = curRes->fstTask;
130.
curRes->fstTask = task->next;
131.
sprintf(log, "%s%10s%8d(s)%15s\n", task->sDate, task->sTime,
229

132.
133.
134.
135.
136.
137.
138.
139.
140.
141. }

task->cost, task->name);
fputs(log, fout);
delete task;
}
fputc('\n', fout);
delete curRes;
}
fclose(fout);
return;

1.

[x y]y

2x20 - x17 + 5x9 - 7x7 + 16x5 + 10x4 + 22x2 - 15 2x19 + 3x17 + 15x10 + 7x7 10x5 + 4x4 + 13x2 - 7

-1 17 2 20 5 9 -7 7 10 4 22 2 -15 0 16 5 0 -1 2 19 7 7 3 17
4 4 15 10 -10 5 13 2 -7 0 8 -8

-1 17 2 20 22 2 5 9 -7 7 -15 0 16 5 10 4 0 -1
7 7 -7 0 3 17 4 4 15 10 -10 5 13 2 2 19 9 -7

[2 20] [2 19] [1 17] [ 15 10] [5 9] [6 5] [14 4] [35 2] [-22 0]

2.

230

-
12-1
x9+4x8-8x7+3x5+18x4-4x3+7x2+15

root

184

72

48

19

-87

-43

150

35

12-1

A B A B A B B
A

0 1

3
B A B B
A B A A
B A A

231

12.1

root root
AA
A
4~9

11~27 insertTree(TreeNode *root, int val) root


val root

(1) val root root


(2) val root root
29~34 delTree(TreeNode *root) root

36~51 printTree(TreeNode *root) root


1 1
K K
$ 12-2
12-3 printTree()
8
4
2
root

$
$

8
7
4

$
$

12
12

$
20

20

$
$
12-2

12-3 printTree()

TreeNode root
val insertTree(TreeNode *root,
int val) val root
232

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.

#include <stdio.h>
#include <stdlib.h>
struct TreeNode{////
//
int val;
//
TreeNode *left, *right;
};
TreeNode *insertTree(TreeNode *root, int val) {//
TreeNode *newNode;
if ( root == NULL ) {
newNode = new TreeNode;
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return(newNode);
}
if ( val <= root->val )
root->left = insertTree(root->left, val);
else
root->right = insertTree(root->right, val);
return(root);
}
void delTree(TreeNode *root) {//
if ( root->left != NULL ) delTree(root->left);
if ( root->right != NULL ) delTree(root->right);
delete root;
return;
}
void printTree(TreeNode *root, char offset[]){//
char str[81];
printf("%s%d\n",offset, root->val);
sprintf(str, "%s%s", offset, " ");
if ( root->left != NULL )
printTree(root->left, str);
else
printf("%s$\n",str);
233

45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.

if ( root->right != NULL )
printTree(root->right, str);
else
printf("%s$\n",str);
return;
}
void main()
{
FILE *fin;
TreeNode *root;
int val;
char str[81], inFile[30];
printf("input the data file's name: ");
scanf("%s", inFile);
fin = fopen(inFile, "r");
//
root = NULL;
while( fscanf(fin,"%d", &val)!=EOF ) root = insertTree(root, val);
fclose(fin);
//
sprintf(str, "%s", "");
printTree(root, str);
//
delTree(root);
return;
}

81247220

47122208
root

root

8
2
4

12
12

20
8

20

12-4
(1)
234

(2)(3)
6
24781220
12-4

-()

A valA val
val A A val val
A A val val A
-

-
6 24781220 81247220
- 12-5

root

root

12

12

20

20

12-5 -

12.2

235


root

12

20

12-6
(1)

12-6 84271220
(2)

12-6 27420128
(3)

12-6 2478
1220
(4)

12-6 20128
742
4~9
11~27 insertTree(TreeNode
*root, int val) root val
29~34 delTree(TreeNode *root)
236

36~42 LFRTraverse(TreeNode *root)


root
44~50 FLRTraverse(TreeNode *root) root

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.

#include <stdio.h>
#include <stdlib.h>
struct TreeNode{////
//
int val;
//
TreeNode *left, *right;
};
TreeNode *insertTree(TreeNode *root, int val) {//
TreeNode *newNode;
if (root == NULL) {
newNode = new TreeNode;
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
return(newNode);
}
if (val <= root->val)
root->left = insertTree(root->left, val);
else
root->right = insertTree(root->right, val);
return(root);
}
void delTree(TreeNode *root) {//
if (root->left != NULL) delTree(root->left);
if (root->right != NULL) delTree(root->right);
delete root;
return;
}
void LFRTraverse(TreeNode *root) {
//
if (root->left != NULL) LFRTraverse(root->left);
printf("%d ", root->val);
if (root->right != NULL) LFRTraverse(root->right);
237

41.
return;
42. }
43.
44. void FLRTraverse(TreeNode *root) {
45. //
46.
printf("%d ", root->val);
47.
if (root->left != NULL) FLRTraverse(root->left);
48.
if (root->right != NULL) FLRTraverse(root->right);
49.
return;
50. }
51.
52. void main()
53. {
54.
FILE *fin;
55.
TreeNode *root;
56.
int val;
57.
char inFile[30];
58.
59.
printf("input the data file's name: ");
60.
scanf("%s", inFile);
61.
fin = fopen("data.txt", "r");
62.
//
63.
root = NULL;
64.
while (fscanf(fin,"%d", &val) != EOF) root = insertTree(root, val);
65.
fclose(fin);
66.
//
67.
printf("traversing left sub-tree firstly, then root, and right sub-tree lastly: \n");
68.
LFRTraverse(root); printf("\n");
69.
//
70.
printf("traversing root firstly, then left sub-tree, and right sub-tree lastly: \n");
71.
FLRTraverse(root); printf("\n");
72.
//
73.
delTree(root);
74.
75.
return;
76. }

insertTree(TreeNode *root, int val)

238

(1) -

insertTree(TreeNode *root, int val)


[13 22] 12-7

18
root
8
8
14
21
8

8
15
39

39
4
12
24
50
39

23
32
39
24
12-7
(2) -

12.3
-

N N-1

239


newNodenewNode father
father son1 son2newNode son1 newNode
son1 NULL newNode
newNode

son2 newNode father

father NULL
3

son2 father grandfather


grandfather grandfather

depth newNode
newNode father father
father
2

1 2
C grandfather( A)
A

root

root

newRoot

newRoot

B
C

C
12-8 1

2 2
B B A T
depth(T) depth(R0)-2depth(R2) depth(R0)depth(L1)-2depth(L2) depth(L1)

root

newRoot
B

R0
B

L1

C
L2

L1

R2

L2

12-9 2

240

R2

R0


3 2
C grandfather( A)
root

root

newRoot

A
newRoot

B
A

12-10 3
4 2
B B A T
depth(T) depth(L0)-2depth(L2) depth(L0)depth(R1)-2depth(R2) depth(R1)

root

newRoot

L0

B
A

L2

R1

L0
L2
R2
12-11 4

R2

R1

4~8 10~21 computeDepth(TreeNode


*root) root 23~69 balance(TreeNode *root)
root
71~92 insertBTree(TreeNode *root, int val) root

94~113 save_delTree(TreeNode *root , FILE *fout)

33. #include <stdio.h>


34. #include <stdlib.h>
35.
36. struct TreeNode{
37.
int val;
38.
int depth;
241

39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.

TreeNode *left, *right;


};
void computeDepth(TreeNode *root){
int depth;
if (root->left != NULL)
depth = root->left->depth;
else
depth = 0;
if (root->right != NULL && root->right->depth > depth)
depth = root->right->depth;
root->depth = depth+1;
return;
}
TreeNode *balance(TreeNode *root){
int leftD, rightD;
TreeNode *newRoot;
if (root->left != NULL)
leftD = root->left->depth;
else
leftD = 0;
if (root->right != NULL)
rightD = root->right->depth;
else
rightD = 0;
if (abs(leftD-rightD) < 2) return(root);
if (leftD > rightD )
if (root->left->right != NULL) {
newRoot = root->left->right;
root->left->right = newRoot->left;
newRoot->left = balance(root->left);
root->left = newRoot->right;
newRoot->right = balance(root);
}
else {
newRoot = root->left;
root->left = newRoot->right;
newRoot->right = root;
}
if (leftD < rightD )
242

83.
if (root->right->left != NULL) {
84.
newRoot = root->right->left;
85.
root->right->left = newRoot->right;
86.
newRoot->right = balance(root->right);
87.
root->right = newRoot->left;
88.
newRoot->left = balance(root);
89.
}
90.
else {
91.
newRoot = root->right;
92.
root->right = NULL;
93.
newRoot->left = root;
94.
}
95.
96.
computeDepth(newRoot->left);
97.
computeDepth(newRoot->right);
98.
99.
return(newRoot);
100.
101. }
102.
103. TreeNode *insertBTree(TreeNode *root, int val) {
104.
TreeNode *newNode, *newRoot;
105.
106.
if (root == NULL) {
107.
newNode = new TreeNode;
108.
newNode->val = val;
109.
newNode->depth = 1;
110.
newNode->left = NULL;
111.
newNode->right = NULL;
112.
return(newNode);
113.
}
114.
115.
if (val <= root->val)
116.
root->left = insertBTree(root->left, val);
117.
else
118.
root->right = insertBTree(root->right, val);
119.
120.
newRoot = balance(root);
121.
computeDepth(newRoot);
122.
123.
return(newRoot);
124. }
125.
126. void save_delTree(TreeNode *root , FILE *fout) {
243

127.
char result[20];
128.
129.
sprintf(result, "%d\n", root->val);
130.
fputs(result, fout);
131.
132.
if (root->left != NULL)
133.
saveTree(root->left, fout);
134.
else
135.
fputs(" $\n", fout);
136.
137.
if (root->right != NULL)
138.
saveTree(root->right, fout);
139.
else
140.
fputs("$\n", fout);
141.
142.
delete root;
143.
144.
return;
145. }
146.
147. void main()
148. {
149.
FILE *fin, *fout;
150.
TreeNode *root;
151.
int val;
152.
char inFile[30], outFile[30];
153.
154.
printf("input the data file's name: ");
155.
scanf("%s", inFile);
156.
printf("input the file name for saving results: ");
157.
scanf("%s", outFile);
158.
159.
root = NULL;
160.
fin = fopen(inFile, "r");
while (fscanf(fin,"%d", &val) != EOF)
root = insertBTree(root, val);
161.
162.
fclose(fin);
163.
164.
fout = fopen(outFile,"w");
165.
save_delTree(root, fout);
166.
fclose(fout);
167.
168.
return;
169. }

244


1.

2.

1
1

500~9 6

1 1
50
10 2
2 0~9
6
2 2

10 2
NOT FOUND 2

245

POJ POJ
http://acm.pku.edu.cn/OnlineJudge/POJ

POJ 24 2000

POJ

POJ
POJ

I. POJ
POJ 2229 39,000 1,804,183
2,200 POJ 485
12004 acm 70 361
22005 acm
93 476 3/2003 (80 )/2004
(80 )/2005 (400 )/2006 (500 )1(
- 340 300 200 / 100 )2
( - 370 )3( - 370 )POJ
POJ
Ural State University Problem Set Archive with Online Judge System
http://acm.timus.ru
Universidad de Valladolid Problem Set Archive http://acm.uva.es/problemset
Zhejiang Unversity Online Judge http://acm.zju.edu.cn
POJ

Huazhong University of Science and Technology http://acm.hust.edu.cn/JudgeOnline


Xiamen University http://acm.xmu.edu.cn/JudgeOnline
Hefei University of Technology http://acm.tdzl.net:83/JudgeOnline

POJ

246

POJ
1 POJ
google POJ

1 POJ

II. POJ
POJ 24

II-1

II-2
2229
Accepted( ) Compile Error( ) Run Time Error( ) Time Limit
Exceeded()Memory Limit Exceeded()Wrong Answer()Format Error(
)

247

II-3
POJ

II-4
POJ

II-5
POJ
ID

III POJ
POJ

III-1
C

POJ 24

III-2
POJ
IP
248

III-3
POJ

249

POJ

2.1
2.2
2.3
2.4
2.5
1
2
3
4
5
6
7
8
9
10

2714
2796
2807
2713
2677
2800
2715
2767
2680
2818

skew

2972
2973

2 16

2734
2735
2798
2765

487-3279

2742
2974
2744
2975

All in All

2743
2976
2818
2819
2820
2804
2797
2799

3.1
3.2
1
2
3
4
4.2
4.3
4.4
4.5
1
2
3
4
5
6
7
8

250

2750
1657
2808
2801
1017

5.1
5.2
5.3
5.4
5.5

2733
2712
2964
2965
2966

2723
2967

2746
2950
2745
1833

1835
2764
2785
2952

2981
2980
2737
2706

2 N

2809
2738
2793
2951

2977
2692
2810
2811
2812

2739
2747
2813
2814
1183

2753

1
2

6.1
6.2
6.3
6.4
1
2
3
4
7.1
7.2
7.3
7.4
1
2
3
4
8.2
8.3
8.4
8.5
8.6
1
2
3
4
5

9.2

251

9.3
9.4
9.5
9.6
9.7
9.8
1
2
3
4
5
6
7
8
9
10.3
10.4
10.5
10.6
1
2
3
4
5
6
7
8

2756
2694
1664
2816
2754
2817

24
""

2815
2749
2790
2787
2775
2802
2803
1191
1321

Help Jimmy

2757
1661
2806
2979

Pell

2809
2795
1088
2733
2786
2792
2774
2766

252

You might also like