22智能 代码作业集合
3-2
#include <stdio.h>
int main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Line 1 - c 的值是 %d\n", c );
c = a - b;
printf("Line 2 - c 的值是 %d\n", c );
c = a * b;
printf("Line 3 - c 的值是 %d\n", c );
c = a / b;
printf("Line 4 - c 的值是 %d\n", c );
c = a % b;
printf("Line 5 - c 的值是 %d\n", c );
c = a++; // 赋值后再加 1 ,c 为 21,a 为 22
printf("Line 6 - c 的值是 %d\n", c );
c = a--; // 赋值后再减 1 ,c 为 22 ,a 为 21
printf("Line 7 - c 的值是 %d\n", c );
}
#include <stdio.h>
int main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
printf("Line 1 - 条件为真\n" );
}
if ( a || b )
{
printf("Line 2 - 条件为真\n" );
}
/* 改变 a 和 b 的值 */
a = 0;
b = 10;
if ( a && b )
{
printf("Line 3 - 条件为真\n" );
}
else
{
printf("Line 3 - 条件为假\n" );
}
if ( !(a && b) )
{
printf("Line 4 - 条件为真\n" );
}
}
4-1 判断、循环
4-2 斐波那契数列 递归和迭代都要写
4-3 数组的插入与删除
6-1 链表的插入与删除
7-1 二分查找
7-2 选择排序
8-1 递归 两个都要写
8-2 汉诺塔
8-2
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
typedef struct {
ListNode *top;
int size;
} LinkedListStack;
LinkedListStack *newLinkedListStack() {
LinkedListStack *s = (LinkedListStack *)malloc(sizeof(LinkedListStack));
if (s == NULL) {
fprintf(stderr, "内存分配失败\n");
exit(1);
}
s->top = NULL;
s->size = 0;
return s;
}
void delLinkedListStack(LinkedListStack *s) {
while (s->top) {
ListNode *n = s->top->next;
free(s->top);
s->top = n;
}
free(s);
}
int size(LinkedListStack *s) {
return s->size;
}
bool isEmpty(LinkedListStack *s) {
return size(s) == 0;
}
void push(LinkedListStack *s, int num) {
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
if (node == NULL) {
fprintf(stderr, "内存分配失败\n");
exit(1);
}
node->next = s->top;
node->val = num;
s->top = node;
s->size++;
}
int peek(LinkedListStack *s) {
if (isEmpty(s)) {
printf("栈为空\n");
return INT_MAX;
}
return s->top->val;
}
int pop(LinkedListStack *s) {
if (isEmpty(s)) {
printf("栈为空\n");
return INT_MAX;
}
int val = s->top->val;
ListNode *tmp = s->top;
s->top = s->top->next;
free(tmp);
s->size--;
return val;
}
int main() {
LinkedListStack *stack = newLinkedListStack();
push(stack, 1);
push(stack, 2);
push(stack, 3);
printf("栈的长度: %d\n", size(stack));
printf("栈顶元素: %d\n", peek(stack));
printf("出栈元素: %d\n", pop(stack));
printf("出栈元素: %d\n", pop(stack));
printf("出栈元素: %d\n", pop(stack));
printf("栈的长度: %d\n", size(stack));
delLinkedListStack(stack);
return 0;
}
9-1 基于链表实现的栈
9-2 基于链表实现队列
11-2 基于链表实现双向队列
12-1 散列表
12-2 哈希冲突
13-1 广度优先搜索
13-2 图
14-1 深度优先搜索
14-2 dijkstra算法
15-1 背包问题
原文地址:https://blog.csdn.net/Michelle8023/article/details/144372304
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!