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的面试真题,大家一起看看吧,这次的 ...
随机推荐
- Beautiful Soup的用法(五):select的使用
原文地址:http://www.bugingcode.com/blog/beautiful_soup_select.html select 的功能跟find和find_all 一样用来选取特定的标签, ...
- 吴裕雄--天生自然 python开发学习笔记:下载python离线安装whl文件链接
https://www.lfd.uci.edu/~gohlke/pythonlibs/
- webpack里面__dirname意思
- 常用JS代码片段
1.隐藏部分数字,如手机号码,身份证号码 1 2 3 function (str,start,length,mask_char){ return str.replace(str.substr(star ...
- ES常见问题整理
1.集群状态red.yellow处理方法 1.red表示主分片数据不完整,通常时由于某个索引的主分片为分片unassigned,找出这个分片未分配的原因,解决即可: curl -XGET http:/ ...
- scrapy爬虫框架教程(二)-- 爬取豆瓣电影
前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大家讲解一个完整爬虫的流程. 工具和环境 语言:python 2 ...
- 【原创】面试官问我G1回收器怎么知道你是什么时候的垃圾?
这是why技术的第36篇原创文章 上面的图片是我上周末在家拍的.以后的文章里面我的第一张配图都用自己随手拍下的照片吧.分享生活,分享技术,哈哈. 阳台上的花开了,成都的春天快来了,疫情也应该快要过去了 ...
- 一文看懂js中元素的客户区大小(clientWidth,clientHeight)
元素的客户区 元素的客户区大小,指的是元素内容及其内边距所占据的空间大小. 相关属性如下: 1. clientWidth:元素内容区宽度+元素左右内边距 2. clientHeight:元素内容区高度 ...
- lnmp 一键安装包(nginx) 运行laravel项目显示该网页无法正常运行
vi /usr/local/nginx/conf/fastcgi.conf 注释掉 PHP_ADMIN_VALUE #fastcgi_param PHP_ADMIN_VALUE "open_ ...
- 东南大学RM装甲板识别算法详解
rm中,装甲板的识别在比赛中可谓是最基础的算法.而在各个开源框架中,该算法也可以说最为成熟.出于学习目的,之后将对比多个高校或网络代码(),尝试学习各个rm装甲板识别算法的优点和流程. 这次先是东南大 ...