20年833
1. 给一个带头结点单链表,删除所有值为k的节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| struct ListNode{ int val; struct ListNode *next; }; ListNode * removeElements(ListNode *head,int k){ if(head==NULL) return NULL; ListNode *p=head; ListNode *q; while(p->next!=NULL) { if(p->next->val==k){ q=p->next; p->next=p->next->next; free(q); } else p=p->next; } return head; }
|
2. 建立二叉排序树
给定一个数组{10,18,9,2,20,5,6,15,19,25},设计一个程序根据本数组建立一颗二叉排序树,输入数据时以-1作为结束。
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
| typedef struct BTNode{ int key; struct BTNode *lchild; struct BTNode *rchild; }BTNode,*BiTree;
int BSTInsert(BiTree &bt,int key){ if(bt==NULL){ bt=(BTNode*)malloc(sizeof(BTNode)); bt->lchild=bt->rchild=NULL; bt->key=key; return 1; }else{ if(key<bt->key){ BSTInsert(bt->lchild,key); }else if(key>bt->key){ BSTInsert(bt->rchild,key); }else if(key==bt->key) return 0; } } void solve(){ int x; BiTree tree=NULL; while(cin>>x&&x!=-1){ BSTInsert(tree,x); } }
|
3. 快速排序
给定数组{46,79,56,52,38,40,80,31,95,24}。要求:
- 写出快速排序的思想
- 实现快速排序算法,从键盘输入该数组,对其进行排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int qsort(int arr[],int left,int right){ if(left>=right) return 0; int num=arr[left]; int i=left; int j=right; while(i<j){ while(i<j&&arr[j]>=num)j--; while(i<j&&arr[i]<=num)i++; swap(arr,i,j); } swap(arr,left,j); qsort(arr,left,j-1); qsort(arr,j+1,right); }
|
4. 合法出入栈字符序列
输入一串字符串,如IOIOOOII,长度最长为50,其中I代表入栈操作,O代表出栈操作。试设计一个程序,判断输入的字符串序列是否合法的出入栈操作序列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
bool isLegal(char str[],int n){ int cnt=0; for(int i=0;i<n;i++){ if(str[i]=='O'){ cnt--; if(cnt<0) return 0; }else cnt++; } return 1; }
|
5. 统计二叉树叶子结点
给一个二叉树写一个函数统计叶子结点个数,函数声明void counterleaf(bitree *t,int &count)
1 2 3 4 5 6 7 8 9 10
| void counterleaf(bitree *t,int &count){ if(t->lchild==NULL && t->rchild==NULL){ count++; return; } if(t->lchild!=NULL) counterleaf(t->lchild,count); if(t->rchild!=NULL) counterleaf(t->rchild,count); }
|
6. 最小乘船数问题
进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。一条独木舟上乘客的总重量不能超过独木舟的最大承载量,并且每条船最多只能坐两个乘客。我们要尽量减少这次活动中的花销,所以要找出可以安置所有旅客的最少的独木舟条数。现在请写一个程序,读入独木舟的最大承载量、旅客数目和每位旅客的重量。根据给出的规则,计算要安置所有旅客必须的最少的独木舟条数,并输出结果。 第一行输入最大船载重量和乘客数 。第二行输入乘客的重量。输出为所需要的最少独木舟的条数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| int MinBoat(int people[],int n,int limit){ Sort(people,n); int i=1; int j=n; int ans=0; while(i<=j){ if(people[i]+people[j]<=limit) { i++;j--; } else j--; ans++; } return ans; }
|
7. 最长公共子序列
小王打枪,给定一个目标序列,如 ccca,子弹的序列为 acbc。打枪的规则如下:按照子弹序列的顺序 射击;子弹打中对应的目标得 1 分,否则无分;允许放空枪。假定:
(1)都是神枪手,只要射击就一定能打中;
(2)子弹打中目标,目标就销毁;
(3)共 26 种目标用 26 个小写字母表示。
输入第 1 行是子弹列,第 2 行是目标列,输出为 1 个数字,表示最高分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| int n,m; char a[Maxn]; char b[Maxn]; int dp[Maxn][Maxn]; void fun(){ for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ dp[i][j]=0; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); } } }
|
公共汽车问题
假设某条街上每一公里就有一个公共汽车站,并且乘车费如下表:
公里数 1 2 3 4 5 6 7 8 9 10
费用 12 21 31 40 49 58 69 79 90 101
而任意一辆汽车行驶不能超过 10 公里。某人想行驶 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
|
int Mincost(int cost[],int n,int Dis) { int dp[Maxn]; dp[0]=0; for(int i=1;i<=Dis;i++){ int Min=inf; for(int j=1;j<=min(i,n);j++) Min=min(Min,dp[i-j]+cost[j]); dp[i]=Min; } return dp[Mis]; } void solve(){ int n; int Dis; int cost[Maxn]; cin>>n; for(int i=1;i<=n;i++) cin>>cost[i]; cin>>Dis; cout<<Mincost(cost,n,Dis)<<endl; }
|