demo模板
demo模板
demo模板
下面more是分隔符
算法复杂度
时间复杂度——大O标记法
- 用常数1取代运行中的所有加法常数。
- 在修改后的运行次数函数中,只保留最高阶项。
- 如果最高阶项存在,且不是1,则除去其常系数,得到的结果就是大O阶。
1 2 3 4 5 6 7 8 9 10
|
void func(){ int i, n = 100, sum = 0; for (i = 0; i <= n; i++) { sum += i; } printf("%d \n", sum); }
|
1 2 3 4 5 6
| #时间复杂度 demo: (1) x=x+1; 时间复杂度为O(1),称为常量阶 (2) for( i=1,i<=n,i++ ) x=x+1; 时间复杂度为O(n),称为线性阶 (3) for (int i = 0; i <=n; i++) for (int j = 0; j <= n; j++) x = x + 1; 时间复杂度为O(n^2),成为平方阶
|
简单的链表
demo,创建链表, 输出链表, 删除链表
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
| #include<stdio.h> #include<malloc.h> void Chinese(){ system("chcp 65001"); }
struct book { int num; int price; struct book *next; };
struct book *creat(int n) { struct book *head, *p1, *p2; head = p2 = (struct book *) malloc(sizeof(struct book)); for (int i = 0; i < n; i++) { p1 = (struct book *) malloc(sizeof(struct book)); printf("请输入图书名字、价格,以空格分隔: \n"); scanf("%d%d", &p1->num, &p1->price);
p2->next = p1; p2 = p1; } p1->next = NULL;
return head; }
void printBook(struct book *head) { struct book *p = head->next;
do { printf("书名:%d, 价格:%d \r\n", p->num, p->price); p = p->next; } while (p != NULL);
printf("************************ \n"); }
void deleteBook(struct book *head1, int num) { struct book *p = head1->next, *q = head1; while (p->next != NULL) { if (p->num == num) { q->next = p->next; break; } q = q->next; p = p->next; } }
int main() { Chinese();
struct book *head; head = creat(3);
printBook(head);
deleteBook(head, 111); printBook(head);
return 0; }
|
底部
没有了