补充第十题

This commit is contained in:
Luthics 2023-12-02 14:12:30 +08:00
parent ab2e91de78
commit da0755858e
6 changed files with 314 additions and 1 deletions

8
10/data.in Normal file
View File

@ -0,0 +1,8 @@
4
1 2
5 2
3 4
666 9
2
1 3
2 4

163
10/main.c Normal file
View File

@ -0,0 +1,163 @@
// 1.问题描述
// 设计一个一元稀疏多项式简单计算器。
// 2.基本要求
// 一元稀疏多项式简单计算器的基本功能是:
// (1)输入并建立多项式;
// (2)输出多项式,输出形式为整数序列:
// n,c1,e1,c2,e2,…,cn,en,
// 其中n是多项式的项数ci,ei分别是第i项的系数和指数序列按指数降序排列
// (3)多项式a和b相加建立多项式a+b
// (4)多项式a和b相减建立多项式a-b
// (5)计算多项式在x处的值即给定x值计算多项式值。
// 3.实现提示
// 用带表头结点的单链表存储多项式,多项式的项数存放在头结点中。
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int c, e;
struct Node *next;
} Node;
typedef struct List {
Node head;
int len;
} List;
List *init_list() {
List *l = (List *)malloc(sizeof(List));
l->head.next = NULL;
l->len = 0;
return l;
}
// 降序插入
void insert(List *l, int c, int e) {
Node *p = &(l->head);
while (p->next && p->next->e > e) {
p = p->next;
}
if (p->next && p->next->e == e) {
p->next->c += c;
if (p->next->c == 0) {
Node *q = p->next;
p->next = q->next;
free(q);
l->len--;
}
} else {
Node *q = (Node *)malloc(sizeof(Node));
q->c = c, q->e = e;
q->next = p->next;
p->next = q;
l->len++;
}
}
// 只输出
void output(List *l) {
Node *p = l->head.next;
printf("%d,", l->len);
while (p) {
printf("%d,%d", p->c, p->e);
p = p->next;
if (p)
printf(",");
}
printf("\n");
}
// 只输出
void plus(List *l1, List *l2) {
Node *p1 = l1->head.next, *p2 = l2->head.next;
while (p1 && p2) {
if (p1->e > p2->e) {
printf("%d,%d,", p1->c, p1->e);
p1 = p1->next;
} else if (p1->e < p2->e) {
printf("%d,%d,", p2->c, p2->e);
p2 = p2->next;
} else {
printf("%d,%d,", p1->c + p2->c, p1->e);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) {
printf("%d,%d,", p1->c, p1->e);
p1 = p1->next;
}
while (p2) {
printf("%d,%d,", p2->c, p2->e);
p2 = p2->next;
}
printf("\n");
}
// 只输出
void minus(List *l1, List *l2) {
Node *p1 = l1->head.next, *p2 = l2->head.next;
while (p1 && p2) {
if (p1->e > p2->e) {
printf("%d,%d,", p1->c, p1->e);
p1 = p1->next;
} else if (p1->e < p2->e) {
printf("%d,%d,", -p2->c, p2->e);
p2 = p2->next;
} else {
printf("%d,%d,", p1->c - p2->c, p1->e);
p1 = p1->next;
p2 = p2->next;
}
}
while (p1) {
printf("%d,%d,", p1->c, p1->e);
p1 = p1->next;
}
while (p2) {
printf("%d,%d,", -p2->c, p2->e);
p2 = p2->next;
}
printf("\n");
}
int calc(List *l, int x) {
int sum = 0;
Node *p = l->head.next;
while (p) {
sum += p->c * pow(x, p->e);
p = p->next;
}
return sum;
}
int main() {
// freopen("data.in", "r", stdin);
List *l = init_list();
List *l2 = init_list();
int n1, n2, c, e;
scanf("%d", &n1);
for (int i = 0; i < n1; i++) {
scanf("%d%d", &c, &e);
insert(l, c, e);
}
scanf("%d", &n2);
for (int i = 0; i < n2; i++) {
scanf("%d%d", &c, &e);
insert(l2, c, e);
}
output(l);
output(l2);
plus(l, l2);
minus(l, l2);
while (1) {
printf("请输入x的值");
scanf("%d", &c);
if (c == -1)
break;
printf("%d\n", calc(l, c));
}
return 0;
}

72
13/bfs.c Normal file
View File

@ -0,0 +1,72 @@
// // Ref: P1605 迷宫
// 1、问题描述
// 迷宫实验是取自心理学的一个古典实验。在该实验中,把一只老鼠从一个无顶大盒子的门放入,在盒中设置了许多墙,对行进方向形成了多处阻挡。盒子仅有一个出口,在出口处放置一块奶酪,吸引老鼠在迷宫中寻找道路以到达出口。对同一只老鼠重复进行上述实验,一直到老鼠从入口到出口,而不走错一步。老鼠经多次试验终于得到它学习走迷宫的路线。
// 2、设计功能要求
// 迷宫由m行n列的二维数组设置0表示无障碍1表示有障碍。设入口为11出口为mn每次只能从一个无障碍单元移到周围四个方向上任一无障碍单元。编程实现对任意设定的迷宫求出一条从入口到出口的通路或得出没有通路的结论。
// 算法输入:代表迷宫入口的坐标
// 算法输出:穿过迷宫的结果。 算法要点:创建迷宫,试探法查找路。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int m, n;
int maze[105][105];
int vis[105][105];
typedef struct {
int x, y;
} Node;
Node *q[10005];
int head, tail;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void push(Node *node) { q[tail++] = node; }
Node *front() { return q[head]; }
void pop() { head++; }
int empty() { return head == tail; }
int main() {
freopen("data.in", "r", stdin);
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &maze[i][j]);
}
}
Node *start = (Node *)malloc(sizeof(Node));
start->x = 1;
start->y = 1;
push(start);
vis[1][1] = 1;
while (!empty()) {
Node *cur = front();
pop();
for (int i = 0; i < 4; i++) {
int nx = cur->x + dir[i][0];
int ny = cur->y + dir[i][1];
if (nx < 1 || nx > m || ny < 1 || ny > n) {
continue;
}
if (maze[nx][ny] == 0 && !vis[nx][ny]) {
Node *next = (Node *)malloc(sizeof(Node));
next->x = nx;
next->y = ny;
push(next);
vis[nx][ny] = 1;
}
}
}
if (vis[m][n]) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}

8
13/data.in Normal file
View File

@ -0,0 +1,8 @@
7 6
0 0 1 1 0 0
1 0 0 0 0 0
1 0 0 0 0 1
1 0 0 0 0 1
1 1 0 0 0 0
1 1 1 1 0 0

63
13/dfs.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int m, n;
int maze[105][105];
// 存储当前路径
char *path[10005];
int path_len = 0;
void dfs(int x, int y) {
if (x == m && y == n) {
for (int i = 0; i < path_len; i++) {
printf("%s", path[i]);
if (i != path_len - 1) {
printf("->");
}
}
printf("\n");
return;
}
maze[x][y] = 1;
if (x + 1 <= m && maze[x + 1][y] == 0) {
path[path_len] = (char *)malloc(sizeof(char) * 105);
sprintf(path[path_len++], "(%d, %d)", x + 1, y);
dfs(x + 1, y);
path_len--;
}
if (y + 1 <= n && maze[x][y + 1] == 0) {
path[path_len] = (char *)malloc(sizeof(char) * 105);
sprintf(path[path_len++], "(%d, %d)", x, y + 1);
dfs(x, y + 1);
path_len--;
}
if (x - 1 >= 1 && maze[x - 1][y] == 0) {
path[path_len] = (char *)malloc(sizeof(char) * 105);
sprintf(path[path_len++], "(%d, %d)", x - 1, y);
dfs(x - 1, y);
path_len--;
}
if (y - 1 >= 1 && maze[x][y - 1] == 0) {
path[path_len] = (char *)malloc(sizeof(char) * 105);
sprintf(path[path_len++], "(%d, %d)", x, y - 1);
dfs(x, y - 1);
path_len--;
}
}
int main() {
freopen("data.in", "r", stdin);
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &maze[i][j]);
}
}
path[path_len] = (char *)malloc(sizeof(char) * 105);
strcpy(path[path_len++], "(1, 1)");
dfs(1, 1);
return 0;
}

View File

@ -1 +0,0 @@
// Ref: P1605 迷宫