851_2021
1. 链表题
- 什么是线性结构,写出线性结构的特点
- 写出删除链表头结点的算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Node *DeleteHead(Node *pHead){ if(PHead==NULL) return NULL; Node *pTemp = pHead->next; free(pHead); if(pTemp==NULL)return NULL; pHead=pTemp; return pHead; }
|
2. 如图所示,阴影部分为边界像素,像素点着色算法的原理如下
我们先选取s点,将其置为区域中的已知颜色,再按右上左下的方式将其邻接点着色

- 栈是什么?有什么特点?
- 请描述该算法的过程
- 画图展示该算法的运行过程
3. 综合快速排序,回答下面问题:
- 叙述冒泡排序的过程
- 设计实例,叙述快速排序的过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
|
4. 结合计算机算法设计与分析
- 什么是动态规划?动态规划和分治法的区别是什么?
- 举例说明,动态规划求解最优化问题的过程?
5. Fibonacci函数表达式为:
f(1)=f(2)=1;
f(n)=f(n-1)+f(n-2),(n>=3,且n为N+)
- 用递归思想写出代码,关键部分加上注释
- 分析算法的时间复杂度和空间复杂度
1 2 3 4 5 6 7 8 9 10
| int Fib(int n) { if(n==1||n==2) return 1; else return Fib(n-1)+Fib(n-2); }
|
6. 哈夫曼树编码自底向上实现,若定义叶子结点所在层为第一层,其父为第二层,以此类推,处在第n层的结点扫描n-1次,复杂度为o(n^2)
- 设计能表示二叉树的链表数据结构
- 基于上述两种数据结构设计一个复杂度为O(n)的哈夫曼树新编码算法,可实现从树根想叶子结点编码,写出思想
- 编写代码实现上溢问题的算法
- 分析时间复杂度为什么为O(n)
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| typedef struct _htNode { char symbol; struct _htNode *left,*right; }htNode; typedef struct _htNode { htNode *root; }htTree;
struct character{ char ch; int time; }; bool cmp(character a,character b){ return a.time<b.time; } int n; character arr[30];
void initial_work(){ cin>>n; for(int i=0;i<n;i++){ cin>>arr[i].ch>>arr[i].time; } sort(arr,arr+n,cmp); }
struct htNode{ char ch; int value; struct htNode *left,*right; }; typedef struct _htNode { htNode *root; }htTree;
htNode queone[100]; int q1h,q2h; htNode quetwo[100]; int q1t,q2t;
int Compare(int x,int y,int z){ if(x<=y&&x<=z) return 1; if(y<=x&&y<=z) return 2; if(z<=x&&z<=y) return 3; } htTree create(){ htNode tep; for(int i=0;i<n;i++){ tep.ch=arr[i].ch; tep.value=arr[i].time; tep.left=NULL; tep.right=NULL; queone[q1t++]=tep; } while(q1t-q1h+q2t-q2h>1) { int x1=inf; int x2=inf; int x3=inf; if(q1t-q1h>=2) x1=queone[q1h].value+queone[q1h+1].value; if(q2t-q2h>=2) x2=quetwo[q2h].value+quetwo[q2h+1].value; if(q1t-q1h>=1&&q2t-q2h>=1) x3=queone[q1h].value+quetwo[q2h].value; int Result=Compare(x1,x2,x3); if(Result==1) { tep.value=x1; tep.left=&queone[q1h]; q1h++; tep.right=&queone[q1h]; q1h++; quetwo[q2t++]=tep; }else if(Result==2) { tep.value=x2; tep.left=&quetwo[q2h]; q2h++; tep.right=&quetwo[q2h]; q2h++; quetwo[q2t++]=tep; }else { tep.value=x3; tep.left=&queone[q1h]; q1h++; tep.right=&quetwo[q2h]; q2h++; quetwo[q2t++]=tep; } } htTree tree; tree.root=&quetwo[q2h]; return tree; } int ans; void dfs(htNode *node,int step) { if(node->left==NULL&&node->right==NULL) { ans+=(node->value)*step; return ; } if(node->left!=NULL) { dfs(node->left,step+1); } if(node->right!=NULL) { dfs(node->right,step+1); } }
|
7. 一个由n个点构成的图,用n*n的矩阵来表示,矩阵由0或1组成,i行j列为0表示i点和j点之间无边,否则i和j之间右边,请你设计算法判断任意两点是否连通。
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
| #include<iostream> using namespace std; int n; bool Graph[101][101]; bool vis[101]; int ans=0; void dfs(int now,int Target){ if(now==Target){ ans=1; return; } for(int i=1;i<=n;i++){ if(Graph[now][i]&&!vis[i]) { vis[i]=1; dfs(i,Target); vis[i]=0; } } } int main(){ cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>Graph[i][j]; int x,y; cin>>x>>y; vis[x]=1; dfs(x,y); if(ans==0)cout<<"不连通"<<endl; else cout<<"连通"<<endl; return 0; }
|