C 2015年真题【保】
1、编写一个完整的程序,使之能完成以下功能:从键盘中输入若干个整数,用链表储存这些输入的数,并要求存储的顺序与输入的顺序相反。
分析:链表建立【头插法】
代码:
#include <stdio.h>
#include <stdlib.h> //定义单链表
typedef struct slist{
int data;
struct slist *next;
}; void main()
{
struct slist *head,*temp; //head为头结点
head = (struct slist *)malloc(sizeof(struct slist));
head ->next = NULL;
int i,s; printf("输入元素:\n");
scanf("%d",&s);
while(s != 9999)
{
temp = (struct slist *)malloc(sizeof(struct slist));
temp ->data = s;
//头插法,建立逆序链表
temp ->next = head ->next;
head ->next = temp;
scanf("%d",&s);
} printf("输出链表:\n");
temp = head ->next;
while(temp != NULL)
{
printf("%d\t",temp ->data);
temp = temp ->next;
}
}
2、编写一个函数,把整数序列分成两个部分,使得左边部分都不大于右边部分,不需要排序。 ( 考察的是快速排序的部分)
函数代码:
//链表划分
int partition(int arr[],int low,int high)
{
int pos=low;
int i=low, j=high;
int temp; while(i!=j)
{
while(arr[j]>=arr[pos] && i<j)j--;
while(arr[i]<=arr[pos] && i<j)i++;
if(i<j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
} temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp; return i;
}完整代码:
#include <stdio.h>
#include <stdlib.h>
const int m = 7; //链表划分
int partition(int arr[],int low,int high)
{
int pos=low;
int i=low, j=high;
int temp; while(i!=j)
{
while(arr[j]>=arr[pos] && i<j)j--;
while(arr[i]<=arr[pos] && i<j)i++;
if(i<j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
} temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp; return i;
} //快速排序递归式
void quicksort(int a[],int low,int high)
{
if(low < high)
{
int pivotpos = partition(a,low,high);
quicksort(a,low,pivotpos - 1);
quicksort(a,pivotpos+1,high);
}
} void main()
{
int a[] = {1,4,7,8,5,2,3};
int low = 0, high = m - 1,i;
quicksort(a,low,high); printf("排序后:\n");
for(i = 0; i < m; i++)
{
printf("%d\t",a[i]);
}
}
![]()
3、有两个整数数组A和B,它们分别有m、n个整数。并且都是按非递减序列,现将B数组插入A数组中,使得A数组中各元素不大于B数组中各元素,非递减序列。
实例:将B[2,3,6,9] 数组插入A[1,4,7] 数组,顺序排序
思路一:将B插入A后,再顺序排序
代码:
#include <stdio.h>
#include <stdlib.h> void main()
{
int n = 3,m = 4;
int a[7] = {1,4,7},b[] = {2,3,6,9},i = 0,j;
j = n;
//将B插入到A后
while(i < m)
{
a[j++] = b[i++];
}
//将A进行排序【冒泡】
for(i = 0;i < m+n-1;i++)
{
for(j =0;j < m+n-1-i;j++)
{
int tmp;
if(a[j] > a[j+1])
{
tmp = a[j+1];
a[j+1] = a[j];
a[j] = tmp;
}
}
}
printf("将B插入A中,顺序排序:\n");
for(i = 0;i < m+n;i++)
{
printf("%d\t",a[i]);
}
}
思路二:使用附加数组C
代码:
#include <stdio.h>
#include <stdlib.h>
const int n = 3;
const int m = 4;
void main()
{
int a[] = {1,4,7},b[] = {2,3,6,9},c[7] = {0},i = 0,j = 0,k = 0;
do
{
if(a[i] > b[j])
{
c[k++] = b[j++];
}
else
c[k++] = a[i++];
if(i >= n)
c[k++] = a[j++];
if(j >= m)
c[k++] = b[i++];
}while((i >= n && j >= m) == 0); printf("将B插入A中,顺序排序:\n");
for(i = 0;i < m+n;i++)
{
printf("%d\t",c[i]);
}
}
4、两个递增有序整数数列链表La和Lb,将他们合并后,变成一个新的链表,要求该链表递减排序。(结点node由整型data和节点指针next构成)
代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct slist
{
int data;
struct slist *next;
};
//合并【头插法逆序】
void unionslist(struct slist *la,struct slist *lb)
{
struct slist *p=la->next;
struct slist *q=lb->next;
struct slist *temp;
la->next=0; while(p&&q)
{
if(p->data <= q->data)
{
temp = p->next;
p->next = la->next;
la->next = p;
p=temp;
}
else
{
temp = q->next;
q->next = la->next;
la->next = q;
q=temp;
}
}
//若La比Lb短,La遍历完,Lb依序插入
if(q) //若q不为空
p=q;
while(p)
{
temp = p->next;
p->next = la->next;
la->next = p;
p=temp;
}
}
//打印输出
void input(struct slist *head)
{
struct slist *p = head ->next;//p为工作指针
while(p != NULL)
{
printf("%d\t",p ->data);
p = p ->next;
}
}
int main()
{
//构建链表La和Lb
struct slist *La,*Lb,*p;// La为La链表的头结点,Lb为Lb链表的头结点,p为尾指针
La = (struct slist*)malloc(sizeof(struct slist));
Lb = (struct slist*)malloc(sizeof(struct slist));
La ->next = NULL;
Lb ->next = NULL;
int a[] = {1,4,7,8},b[] = {2,3,5,6,9},i,j;
p = La;
for(i = 0; i < 4; i++)
{
//尾插法顺序
struct slist *node = (struct slist*)malloc(sizeof(struct slist));
node ->data = a[i];
node ->next = p ->next;
p ->next = node;
p = node;
}
p = Lb;
for(i = 0; i < 5; i++)
{
//尾插法顺序
struct slist *node = (struct slist*)malloc(sizeof(struct slist));
node ->data = b[i];
node ->next = p ->next;
p ->next = node;
p = node;
}
//输出
printf("La:");
input(La);
printf("\nLb:");
input(Lb);
//合并
printf("\n合并后:");
unionslist(La,Lb);
input(La);
return 0;
}
5、编写一个函数,删除链表中的最小值。(结点node由整型data和节点指针next构成)
代码:
#include <stdio.h>
#include <stdlib.h> typedef struct slist
{
int data;
struct slist *next;
};
//删除最小值
void delmin(struct slist *la)
{
struct slist *p = la ->next,*pre = la; //p为工作指针,pre为p的前驱,min记录最小值,premin是min的前驱
struct slist *min = p,*minpre = la;
while(p)
{
if(p ->data < min ->data)
{
min = p;
minpre = pre;
}
pre = p;
p = p ->next;
}
minpre ->next = min ->next;
free(min);
}
//打印输出
void input(struct slist *head)
{
struct slist *p = head ->next;//p为工作指针
while(p != NULL)
{
printf("%d\t",p ->data);
p = p ->next;
}
}
int main()
{
//构建链表La
struct slist *La,*p;// La为La链表的头结点,p为尾指针
La = (struct slist*)malloc(sizeof(struct slist));
La ->next = NULL;
int a[] = {3,6,9,8,1,5,2},i;
p = La;
for(i = 0; i < 7; i++)
{
//尾插法顺序
struct slist *node = (struct slist*)malloc(sizeof(struct slist));
node ->data = a[i];
node ->next = p ->next;
p ->next = node;
p = node;
}
//输出
printf("La:");
input(La);
//删除最小值
delmin(La);
//删除最小值后输出
printf("\n删除最小值后La:");
input(La); return 0;
}
6、编写函数判断小括号是否匹配。
分析:由于限定了只有小括号,故不需要栈来实现
代码:
#include <stdio.h>
#include <stdlib.h> int ismarry(const char *str)
{
int top=-1;
char c; while(*str)
{
if(*str == '(')++top;
if(*str == ')')
{
if(top == -1)return -1; //若此时指针指向“)”,top = -1,这说明前面的已匹配成功
top--;
}
str++;
}
if(top == -1)return 0;
return -1;
} int main()
{
char str[] = "((())))";
if(ismarry(str) == 0)
printf("匹配成功!");
else
printf("匹配不成功!");
return 0;
}
7、【】对多个字符串进行字典排序
对一个字符串进行字典排序:
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void Sort(char parr[],int n)
{
int i,j;
char *str1, *str2;
//冒泡排序
for(i=0; i<n-1; i++)
{
for(j=i+1; j<=n-1; j++)
{
str1=parr[i];
str2=parr[j];
while(((*str1) && (*str2) && (*str1==*str2)) == 1)
{
str1++;
str2++;
}
if(*str1-*str2>0)
{
char *temp = parr[i];
parr[i] = parr[j];
parr[j] = temp;
}
}
}
}
int main()
{
char ch[] = "helloworld!";
printf("排序前:%s",ch); //字符串输出
Sort(ch,strlen(ch));
printf("\n排序后:%s",ch); //字符串输出
return 0;
}对多个字符串按字典顺序排序
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //对数组内前n个字符串进行字典排序
int main()
{
char *str[] = {"hello","world","shao","hang"}; //指针数组
int i,j,n;
char *temp; scanf("%d",&n);
for(i =0; i < n-1; i++)
{
for(j = i+1; j <n; j++)
{
//strcmp(str1,str2),比较函数,若str1 > str2,则返回正数
if(strcmp(str[i],str[j]) > 0)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
}
}
for(i = 0;i < n;i++)
{
printf("%s\t",str[i]);
}
return 0;
}
8、【不懂】编写一个函数,使之能完成以下功能:利用递归方法找出一个数组中的最大值和最小值
要求递归调用函数的格式如下:MinMaxValue(arr,n,&max,&min),其中arr是给定的数组,n是数组的个数,max、min分别是最大值和最小值
代码:
void MinMaxValue(int arr[], int n, int *max, int *min)
{
if(n==1)
{
*max = arr[0];
*min = arr[0];
}
else
{
int _max=arr[0], _min=arr[0];
MinMaxValue(arr+1, n-1, max, min);
if(*max<_max)*max=_max;
if(*min>_min)*min=_min;
}
}
9、有两字符数组s和t,求t在s中出现第一次的开始位置,如果没有则输出“No”,有则输出开始位置
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int start(char s[],char t[])
{
int s_length = strlen(s);
int i;
char *str1, *str2;
for(i=0; i<s_length; i++)
{
str1 = s+i;
str2 = t;
while(*str1 && *str2 && *str1==*str2)str1++,str2++; //str1与str2相同,
if(*str1-*str2 == *str1) //str2为空,判断str2在sre1中
{
printf("%d",i);
return 0;
} }
printf("NO!\n");
return -1;
}
int main()
{
char a[] = "helloworld",b[] = "o";
start(a,b);
return 0;
}
C 2015年真题【保】的更多相关文章
- C 2015年真题
1.写出程序输出结果 void main() { char p[10]="abc"; char q[]="xyz"; int i,j; i=0; while(* ...
- 2015.12.29~2015.12.30真题回顾!-- HTML5学堂
2015.12.29~2015.12.30真题回顾!-- HTML5学堂 吃饭,能够解决饥饿,提供身体运作机能.练习就像吃饭,强壮自己,提升编程技能,寻求编程技巧的最佳捷径!吃饭不能停,练习同样不能停 ...
- 2015.12.21~2015.12.24真题回顾!-- HTML5学堂
2015.12.21~2015.12.24真题回顾!-- HTML5学堂 山不在高,有仙则名!水不在深,有龙则灵!千里冰封,非一日之寒!IT之路,须厚积薄发!一日一小练,功成不是梦!小小技巧,尽在HT ...
- 2016.1.4~2016.1.7真题回顾!-- HTML5学堂
2016.1.4~2016.1.7真题回顾!-- HTML5学堂 2015悄然而逝,崭新的2016随即而行!生活需要新鲜感,学习JavaScript的过程需要有成就感!成就感又是来自于每一天的不断练习 ...
- 蓝桥杯Java真题解析
上个月参加蓝桥杯省赛拿了个省一,自从比赛完之后就一直没怎么写代码了,还有一个多月就要国赛了,从现在开始准备下国赛,但是我也不想学什么算法,而且我还在准备考研,所以就打算只做下历年的真题,争取国赛拿个国 ...
- Noip前的大抱佛脚----Noip真题复习
Noip前的大抱佛脚----Noip真题复习 Tags: Noip前的大抱佛脚 Noip2010 题目不难,但是三个半小时的话要写四道题还是需要码力,不过按照现在的实力应该不出意外可以AK的. 机器翻 ...
- NOIP2010~2017部分真题总结
NOIP2010~2017部分真题总结 2010 (吐槽)md这个时候的联赛还只有4题吗? 引水入城 只要发现对于有合法解的地图,每个蓄水厂贡献一段区间这个结论就很好做了 那么\(O(n^3)\)对每 ...
- NOIP真题汇总
想想在NOIP前总得做做真题吧,于是长达一个月的刷题开始了 涉及2008-2016年大部分题目 NOIP [2008] 4/4 1.传纸条:清真的三维DP 2.笨小猴:字符串模拟 3.火柴棒等式:打表 ...
- 再也不用担心问RecycleView了——面试真题详解
关于RecycleView,之前我写过一篇比较基础的文章,主要说的是缓存和优化等问题.但是有读者反映问题不够实际和深入.于是,我又去淘了一些关于RecycleView的面试真题,大家一起看看吧,这次的 ...
随机推荐
- MySQL5.7彻底取消主从复制
由于手误在master节点执行了stop slave;->change master to XXX;->start slave;的动作,后面虽然使用stop slave停止了主从复制,但是 ...
- 1078 Hashing (25 分)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- CentOS使用epel安装不同版本php-fpm
针对CentOS使用epel安装 yum -y install epel-release安装好后可以通过如下命令查看yum info epel-releaseyum repolist查看php版本ph ...
- Quartz Tutorial 11 - Miscellaneous Features of Quartz
文章目录 Plug-Ins Quartz提供了一个接口(org.quartz.spi.SchedulerPlugin) 用于插入附加的功能. 与Quartz一同发布的,提供了各种实用功能的插件可以在o ...
- 探究 Go 语言 defer 语句的三种机制
Golang 的 1.13 版本 与 1.14 版本对 defer 进行了两次优化,使得 defer 的性能开销在大部分场景下都得到大幅降低,其中到底经历了什么原理? 这是因为这两个版本对 defer ...
- Mac开发环境部署
1. 安装 Xcode command line tools xcode-select --install 2. 安装 Homebrew 安装 Homebrew 之前,必须先安装 Xcode Comm ...
- Golang/Python/PHP带你彻底学会gRPC
目录 一.gRPC是什么? 二.Protocol Buffers是什么? 三.需求:开发健身房服务 四.最佳实践 Golang 1. 安装protoc工具 2. 安装protoc-gen-go 3. ...
- 从0开发3D引擎(十一):使用领域驱动设计,从最小3D程序中提炼引擎(第二部分)
目录 上一篇博文 本文流程 回顾上文 解释基本的操作 开始实现 准备 建立代码的文件夹结构,约定模块文件的命名规则 模块文件的命名原则 一级和二级文件夹 api_layer的文件夹 applicati ...
- Flutter01-学习准备
1. 简介: Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面. Flutter可以与现有的代码一起工作.在全世界,Flutter正在被越来越多的开发者和 ...
- <select>标签,不要在select标签中写value属性!!!
<select> select标签,一个选择框标签,在开发中很多时候会用到这个标签,例如选择生日19**年,或者在segmentfault中编辑文章时选择'原创','转载',还是'翻译'等 ...






