forked from CodePanda66/CSPostgraduate-408
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS_0_0_love.cpp
87 lines (73 loc) · 1.7 KB
/
DS_0_0_love.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
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
//
// Created by kim on 2020/6/17.
// Copyright (c) Kim Yang All rights reserved.
//
#include <stdio.h>
/**实现模块**/
//下面四种函数的时间复杂度值得分析一二
//逐步递增型爱你
void LoveYou0(int n) {
int i = 1;
while (i <= n) {
printf("I love you %d \n", i);
i++;
}
printf("I love you more than %d\n", n);
}
//嵌套循环型爱你
void loveYou1(int n) {
int i = 1;
while (i <= n) {
i++;
printf("I love you %d\n", i);
for (int j = 1; j < n; j++) {
printf("I love you too\n");
}
}
printf("I love you more than %d\n", n);
}
//指数递增型爱你
void loveYou2(int n) {
int i = 1;
while (i <= n) {
printf("I love you %d\n", i);
i = i * 2;
}
printf("I love you more than %d\n ", n);
}
//搜索数字型爱你
void loveYou3(int flag[], int n) {
printf("I Am kim\n");
for (int i = 0; i < n; i++) {
//我觉这里应该是数组长度
if (flag[i] == n) {
printf("I love you %d\n", n);
break;//找到之后就跳出循环
}
}
}
//递归型爱你
void loveYou4(int n) {
int a, b, c;
if (n > 1) {
loveYou4(n - 1);
}
printf("I love you %d\n", n);
}//递归调用会带来多余的内存开销
/**实现模块**/
/**测试模块**/
//测试函数
void testModule() {
LoveYou0(30);
loveYou1(30);
loveYou2(30);
int array[5] = {2, 10, 100, 1000, 10000};
//声明一个数组并初始化
loveYou3(array, 10);
loveYou4(4);
}
/**测试模块**/
int main() {
testModule();
return 0;
}