![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
严版教材答案
小白算法习题记录本
一个刚学算法的超级无敌小白痴。博客里所有的文章都是用来记录我做题时候敲下的代码的,全是水货,无贡献,经不起深敲,主页也没有浏览的必要,蟹蟹!
展开
-
3.24√ 试编写如下定义的递归函数的递归算法,并根据算法画出求g(5,2)时栈的变化过程
#include<stdio.h>int g(int m, int n){ if(m>=0 && n>=0) { if(m > 0) return (g(m-1, 2*n)+n); else return 0; }}void main(...原创 2019-09-20 16:05:13 · 3914 阅读 · 0 评论 -
汉诺塔问题
【汉诺塔问题】设有3个命名为A、B、C的塔座,在塔座A上插有n个直径大小各不相同、依小到大编号为1,2…,n的圆盘。现要求将A轴上的n个圆盘移至塔座C上并仍按相同顺序叠排。严版教材解析:当n = 1时,只要将编号为1的圆盘从塔座A直接移至塔座C;当n > 1时,需利用B作为辅助塔座,先将A上n-1个圆盘移动到B上,再将A上剩余的n号圆盘移动到C上,最后将B上的n-1个圆盘移动到C上...原创 2019-09-25 17:04:41 · 685 阅读 · 0 评论 -
行编辑程序
#include<stdio.h>#define END '~'void Line_Edit(){//行编辑程序:'#'为退格符,表示前一个字符无效;'@'为退行符,表示改行字符无效 char stack[100]; int top = -1; printf("请用户输入数据:\n"); char ch = getchar(); while(c...原创 2019-09-25 16:20:29 · 243 阅读 · 0 评论 -
数制转换
#include<stdio.h>void num_trans(int a, int k){//将十进制的数字a转成k进制 int stack[100]; int top = -1; while(a > 0) { stack[++top] = a % k; a = a / k ; printf("a=%...原创 2019-09-25 15:52:26 · 100 阅读 · 0 评论 -
线性表:两个一元多项式相乘
热泪盈眶。。。#include<stdio.h>#include <stdlib.h>typedef int ElemType;typedef struct LNode{ float coef; //系数 int expn; //指数 struct LNode *next;}LNode, *LinkList;typedef L...原创 2019-09-23 17:57:11 · 1684 阅读 · 0 评论 -
线性表:两个一元多项式相加的算法
网上搜了一下大家的算法,发现以我的脑袋瓜子好像看不太懂。。。于是自己尝试着搞了一下,发现其实并不难嘻嘻嘻#include<stdio.h>#include <stdlib.h>typedef int ElemType;typedef struct LNode{ float coef; //系数 int expn; //指数 stru...原创 2019-09-23 16:40:00 · 3303 阅读 · 0 评论 -
2.16√ 无头结点单链表插入元素
因为是无头结点单链表L,L指向第一个结点,所以在插入元素时要考虑插入位置是否在表头,如果是,则要将新结点的next指针指向第一个结点,同时将L重新指向新结点。重点在于❗❗【指针作为参数时,如何改变它所指向的值】❗❗在C语言中, 一般而言,在子函数内,要想改变传进来的参数(全局变量)的值,必须要用到指针。 比如 int a = 0,子函数应该这样写:xxxx(int *a),表示传进来的参...原创 2019-09-13 00:31:56 · 1704 阅读 · 0 评论 -
3.26√求解平方根A的迭代函数定义如下,试写出相应的递归算法,并消除递归
#include<stdio.h>//递归算法float sqrt1(float A, float p, float e){ if(p*p-A < e && p*p-A > -e) return p; else return (sqrt1(A, (p+A/p)/2, e));}//消除递归(迭代...原创 2019-09-20 17:09:58 · 845 阅读 · 1 评论 -
3.25√ 试写出求递归函数F(n)的递归函数,并消除递归
#include<stdio.h>//递归算法int F(int n){ if(n == 0) return n+1; else return n*F(n/2);}//消除递归int F2(int n){ if(n>=0) { int outcome = 1; in...原创 2019-09-20 16:19:34 · 1850 阅读 · 0 评论 -
7.14√编写算法,由依次输入的顶点数目、弧的数目、各顶点信息和各条弧的信息建立有向图的邻接表
代码在dev c++中编译通过了,但是在其它编译器好像不行?不知道啥原因#include <stdio.h>#include <stdlib.h>#define maxsize 100typedef int VexType;typedef struct ArcNode{ struct ArcNode *nextarc; int adjvex; ...原创 2019-09-17 15:43:50 · 5634 阅读 · 0 评论 -
3.17√ 识别次读入的一个以@为结束符的字符序列是否为形如”序列1&序列2“模式的字符序列 ,序列12均没有&,且序列2为序列1的逆序列
#include <stdio.h>#include <stdlib.h>#define maxsize 100#define false -1#define true 1typedef int status;status judge(){//判断依次读入的一个以@为结束符的字符序列是否为形如”序列1&序列2“模式的字符序列//序列12均没有&a...原创 2019-09-17 18:38:29 · 6161 阅读 · 0 评论 -
3.18√ 试写一个判别表达式中开、闭括号是否配对出现的算法
很简单的一道题,使用 getchar() 时要注意回车键#include<stdio.h>#define true 1#define false 0#define maxsize 100typedef int status;status match(char exp[], int n){//判别表达式中开闭括号是否配对出现,假设表达式已//存储在数组exp[]中,并...原创 2019-09-18 15:32:42 · 3727 阅读 · 0 评论 -
3.19√ 假设一个算术表达式中可以包含三种括号:圆括号、方括号、花括号。编写判别给定表达式中所含括号是否正确配对出现的算法
#include<stdio.h>#define true 1#define false 0#define maxsize 100typedef int status;status match(char exp[], int n){//判别表达式中开闭括号是否配对出现,假设表达式已//存储在数组exp[]中,并且表达式中的圆、方、花括号可以任意次序嵌套,n为数组长度...原创 2019-09-18 15:40:35 · 19516 阅读 · 5 评论 -
3.16√ 火车调度站的入口处有n节硬席或软席车厢(分别以H和S表示)等待调度,试编写算法,输出对这n节车厢进行调度的操作
车厢型号是一个一个输进去的,当准备进站车厢为软席车厢时,操作为直接输出,接着输入下一个车厢型号,全部型号输入完毕后,才对栈中的硬座车厢进行输出操作,所以输出显示的序号是不连续的,但顺序是正确的#include <stdio.h>#include <stdlib.h>#define maxsize 100#define error -1#define ok 1t...原创 2019-09-17 17:22:41 · 4228 阅读 · 0 评论 -
3.20√ 地图染色体
dev c++编译通过#include<stdio.h>#define true 1#define false 0#define maxsize 10typedef int status;void change(int g[][maxsize],int m, int n, int i, int j, int newcolor){//mn分别为g数组的行数和列数,ij为...原创 2019-09-18 16:29:17 · 176 阅读 · 0 评论 -
3.22√ 对以波兰式表示的表达式求值
将就一下。。只做到了字符型向整型数值的转换,所以输出结果只有整型。。。。太烦了。。。。。#include <stdio.h>#define maxsize 100typedef char Elemtype;int compare_dominant(char a, char b){//比较运算符的优先级 if(a == '+' || a == '-' &a...原创 2019-09-19 18:03:29 · 170 阅读 · 0 评论 -
2.29√ 已知ABC为三个递增有序的线性表,现要求对A表进行如下操作:删去那些即在B表中出现,又在C表中出现的元素(题中没有特别指明同一表中的元素值各不相同)
#include<stdio.h>#include<malloc.h>#include <stdlib.h>#define maxsize 10typedef int ElemType;typedef int status;typedef struct{ ElemType *data; int length;}SqList;...原创 2019-09-15 15:40:00 · 5326 阅读 · 2 评论