程设

排序

快速排序的两个分区方法:$Lomuto$ 单指针,$Hoare$ 双指针

归并排序在多核情境更快 —— 均分


Pointers

&:get address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int i = 10;
printf("%d\n", i);
printf("%p\n", &i);

/* %X, %x, %lu
%p 64bit
%x 32bit(不完整)
*/

/*
printf("%p\n", &(i + 1)); 报错
左值:可以出现在赋值号左侧的值
a = 6 √
(a + 1) = 6 ×
a # 2 = a[2] ???
*/

1
2
3
4
5
6
7
8
int a[3] = {0, 1, 2};
printf("%p\n", &a[0]);
printf("%p\n", &a[1]);
printf("%p\n", &a[2]);
printf("%p\n", &a); //equal to a[0]
/*

*/

1
2
3
4
5
6
7
8
9
10
int i = 10;
int *p = &i;
printf("%p\n", p);
printf("%d\n", *p); //访问变量,*p可作左值9

int *x, *y; //with each one

/*
access: r & w
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vp  oid f(int *a){

}

void g(int a[]){

}

//equal

int main(){
int a[10];
printf("%p\n", a);
printf("%p\n", &a);
//equal

int *p = a;
printf("%d\n", p[0]);
printf("%d\n", *a);
printf("%d\n", *(p + 5));
printf("%d\n", p - a); // 1?
}

string.h下的函数:

  • strlen(char *)
  • strcmp
  • strcpy

while(*p++) 可以在字符串末结束


1
2
3
struct{
int a;
}str;

这样是可以的

1
2
3
4
5
6
struct node{
int len;
char content[];
};

node *str = (node*)malloc(sizeof(int) + 32);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef struct{
...
}node;

node t;


typedef struct node{//声明
...
node *next;
}node;

typedef char line[30]
line aa;p //char aa[30]

typedef int *inp
inp p;

1
2
3
4
5
void f(int x){
...;
}

void (*p)() = f;

1
2
3
4
5
6
7
#ifdef
//#ifundef
#else
#endif

用处:编译时 "-D xxx" 宏定义
(不需要修改源码)
1
2
#if (表达式)
#elif (表达式)
1
2
3
4
5
6
7
8
9
多段注释:

#if 0
sss
sss
#endif

恢复:
#if 1
1
2
#error text...
让编译器出错

1
2
3
4
5
6
if(){
int i;//第一个
if(){
int i;//第二个,此if内优先访问此i
}
}

1
2
3
4
5
6
7
void f(void){
//没有参数
}

void g(){
//任意参数
}

1
--save-temps:保存编译过程

Note

1.Declaring the pointer of a function within another function’s prototype declaration:

1
int func( int x, int (*func2)(int, double) )

However, another way int func2(int, double) is acceptable too, for the function type can be automatically converted to function pointer type


py笔记

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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112

######

print(0b100) # 二进制整数
print(0o100) # 八进制整数
print(100) # 十进制整数
print(0x100) # 十六进制整数

######

print(type(100)) # <class 'int'>
print(type(123.45)) # <class 'float'>
print(type('hello, world')) # <class 'str'>
print(type(True)) # <class 'bool'>

######

print(int('100', base=16)) # str->十六进制int
print(bool(1)) # str->bool,True
print(chr(65)) # int->str,输出'd'
print(ord('A')) # str->int,100

######

print(11//3)
print(2 ** 4)

######

a = None
print(a := 10)
print(1 <= a <= 2)

b = 11

print('%d 你好 %d' % (a, b))
print(f'{a:d} 你好 {b:.1f}')

######

if 1 <= a <= 100:
print('你好')
elif a and (not b):
print('你好1')
else:
if b:
print('wow')
else:
print('now')

match a:
case 1 | 3: print('1')
case 2: print('2')

######

'''

import time

# 0 ~ 99

for _ in range (100): # empty, only repeat
time.sleep(1) # 1s

for i in range (0, 100, 2): # even number
time.sleep(0.01)

print(sum(range(1, 3, 0.1)))

'''

######

while a <= 15:
a += 1
if a > 20:
break
elif a > 25:
continue

######

import random

a = random.randrange(1, 101)

######

items1 = [1, 1.1, '1', True]
items2 = list(range(1, 11))
items3 = list('hello') # h e l l o
print(items1 + items2 + items3 * 2)
print(2 not in items1) # False
print(items1[0], items1[-1]) # forward subscript 0 ~ 4 is equal to reversed subscript -5 ~ -1
print(items1[0:5:2]) # range [0,5), span is 2, namely 0, 2, 4, they can also be negative
print(items1[:], items1[::]) # acquiescently 0, ending, 1
# such options can also be used to modify lists

# lists can be compared, like ==, !=, <= ..., the rule is comparing each elements one by one(parallel[adv])


# type 1 to traverse each one:
for i in range(len(items1)):
print(items1[i], end = ' ')
# type 2:
for item in items1:
print(item, end = " ")


######