栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言程序设计函数题后15道

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言程序设计函数题后15道

浙大版《C语言程序设计(第3版)》题目集

习题10-2 递归求阶乘和习题10-3 递归实现指数函数习题10-4 递归求简单交错幂级数的部分和习题10-5 递归计算Ackermenn函数习题10-6 递归求Fabonacci数列习题10-7 十进制转换二进制习题10-8 递归实现顺序输出整数习题11-1 输出月份英文名习题11-2 查找星期习题11-3 计算最长的字符串长度习题11-4 字符串的连接习题11-5 指定位置输出字符串习题11-6 查找子串习题11-7 奇数值结点链表习题11-8 单链表结点删除[百度网盘链接 ](https://pan.baidu.com/s/1VZn8FZ8D7ht6dvI_57xaiw?pwd=ffff )

习题10-2 递归求阶乘和
改题求n的阶乘前面好像写过
double fact( int n ){	//递归求n的阶乘
	if(n){
		//TODO
		return n * fact(n-1);
	}
	return 1;
}
double factsum( int n ){
	double sum = 0;
	for(int i=1;i<=n;i++){
		//TODO
		sum += fact(i);
	}
	return sum;
	
}
习题10-3 递归实现指数函数
double calc_pow( double x, int n ){
	if(n){
		return x*calc_pow(x,n-1);		
		//大于0时返回x*(递归以后的值)
		//如: n=2 return x * cal(x,1) 
		//----return x * cal(x,0) 之后就跳出来了
		//最终返回的就是x*x*1
	}
	return  1;
}
习题10-4 递归求简单交错幂级数的部分和
double fn( double x, int n ){
	double sum = 1;		//每一位的阶乘
	if(n == 0){			// n = 0 ,返回0
		return 0;
	}
	else if(n%2 == 0){
		for(int i=1;i<=n;i++){	//阶乘的写法 ,记住
			sum *= x;
		}
		//当n为偶数时,减号
		return -sum+fn(x,n-1);
	}
	else if(n%2 == 1){	
		for(int i=1;i<=n;i++){	//x^n
			sum *= x;
		}
		//奇数时,加号
		return sum+fn(x,n-1);
	}
}
习题10-5 递归计算Ackermenn函数
	可以看出该if语句写的不是很好,下面输出会报警告
	可能会无效输出,不用担心,可看题中要求:
	其中m和n是用户传入的非负整数
	如果是自己写代码,一定要保证代码健壮性。

int Ack( int m, int n ){
	if(m == 0){
		return n+1;
	}
	else if(n==0 && m>0){
		return Ack(m-1,1);
	}
	else if(n>0 && m>0){
		return Ack(m-1,Ack(m,n-1));
	}
}
习题10-6 递归求Fabonacci数列
	要求:函数f应返回第n个Fabonacci数。
		题目保证输入输出在长整型范围内。
		建议用递归实现
此题没什么难度,就是按照给的公式写出就行
int f( int n ){
	if(n >= 2){
		return f(n-2) + f(n-1);
	}
	else if(n == 1){
		return 1;
	}
	else
		return 0;
}
习题10-7 十进制转换二进制
void dectobin( int n ){
	if(n>0){
		if(n/2>0)	//只有当n/2>0 时,说明还能往下除
			dectobin(n/2);
		printf("%d",n%2);
	}
	else	//当n = 0时,返回0
		printf("0");
}
习题10-8 递归实现顺序输出整数
	还是用递归比较简单
 prinft 不能在if前,因为从大到小排序,所以等到高位输出后才能低位输出。
 
void printdigits( int n ){
	//被10整除后,如果>0说明n>10,还可以往下除
	if(n/10 > 0)	
		 printdigits(n/10);
	printf("%dn",n%10);	

}
习题11-1 输出月份英文名
char *getmonth( int n ){

	if(n>0 && n<13){
		switch (n) {
		case 1:	return "January";
		case 2:	return "February";
		case 3:	return "March";
		case 4:	return "April";
		case 5:	return "May";
		case 6: return "June";
		case 7:	return "July";
		case 8:	return "August";
		case 9:		return "September";
		case 10:	return "October";
		case 11:	return "November";
		case 12:	return "December";
		}
	}
	return NULL;
}
习题11-2 查找星期
int getindex( char *s ){
	char index[7][20] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	for(int i=0;i<7;i++){
		if(strcmp(s,index[i]) == 0){
			return i;
		}
	}
	return -1;
}
习题11-3 计算最长的字符串长度
// strlen()函数 计算字符串长度
int max_len( char *s[], int n ){
	 int length = strlen(s[0]);
	for(int i=1;i length)
			length = strlen(s[i]);
	}
	return length;
}
习题11-4 字符串的连接
就是strcat()连接两个函数,没啥的好像。
char *str_cat( char *s, char *t ){
	return strcat(s,t);
}
习题11-5 指定位置输出字符串

有两种方案 但是都是通过不了的,呃呃呃难受不理解
不理解什么意思(超长s,取整个字符串)----有知道的可以回答一下,谢谢了

我的代码

char *match( char *s, char ch1, char ch2 ){
	int add=0,j=0,flag=0;
	
	for(int i=0;s[i]!='';i++){
		if( s[i] == ch1 ){		// 找到第一个字符第一次出现匹配成功
			flag = 1;
			add = i;
		}
		if(flag == 1){	//在ch1-ch2之间输出
			j++;
			printf("%c",s[i]);
		}
		if( s[i] == ch2 ){	//轮到ch2时,标志位置零停止输出。
			break;
		}
	}
	printf("n");
	if(flag == 0){
		s[add] = '';
	}
	return &s[add];
}
习题11-6 查找子串
char *search( char *s, char *t ){
	int temp;
	for(int i=0,j=0; s[i]!=''; i++){
		//首字符相同时,看后缀是否相同
		if(s[i] == t[j]){
			temp = i;
			while(t[j] != ''){
//				printf("%c-%c-%d-%d-%dn",s[i],t[j],temp,i,j);
				if(s[i] != t[j]){
					i = temp+1;
					j = 0;
					break;
				}
				j++;
				i++;
			}
			// 后缀匹配相同,返回首地址
			if(t[j] == ''){
				return &s[temp];
			}
		}
	}
	return NULL;
}
习题11-7 奇数值结点链表
struct ListNode *readlist(){
	struct ListNode *head,*p,*end;
	int data;
	head = (struct ListNode*)malloc(sizeof(struct ListNode));		//空表首尾相同
	end = head;
	scanf("%d",&data);
	if(data == -1){
		head = NULL;
	}
	head->data = data;
	
	scanf("%d",&data);
	while(data != -1){
		p = (struct ListNode*)malloc(sizeof(struct ListNode));
		p->data = data;
		p->next = NULL;
		end->next = p;
		end = p;
		scanf("%d",&data);
	}
	return head;
}

//首先建立两个带头结点的链表,偶数和奇数链表然后将*L中符合各自的值连入各自的表中,和尾插法差不多
struct ListNode *getodd( struct ListNode **L ){
	struct ListNode *odd,*even,*s = *L,*o_head,*e_head;
	even = (struct ListNode*)malloc(sizeof(struct ListNode));
	odd = (struct ListNode*)malloc(sizeof(struct ListNode));
	o_head = odd;
	e_head = even;

	while(s != NULL){
		if(s->data%2 == 1){		//奇数
			odd->next = s;
			odd = s;

		}
		else if(s->data%2 == 0){	//偶数
			even->next = s;
			even = s;
		}
		s = s->next;
	}	
	even->next = NULL;
	odd->next = NULL;
	*L = e_head->next;
	return o_head->next;
}
习题11-8 单链表结点删除
不带头结点插入
struct ListNode *readlist(){		
	struct ListNode *head,*end,*s;	// head 头节点 end 尾节点 s 待插入节点
	int data;
	head = (struct ListNode*)malloc(sizeof(struct ListNode));	//首先创建头节点并插入值
	scanf("%d",&data);
	if(data == -1){
		return NULL;
	}
	head->data = data;
	end = head;
	scanf("%d",&data);
	while(data != -1){
		s = (struct ListNode*)malloc(sizeof(struct ListNode));
		s->data = data;
		end->next = s;
		end = s;
		scanf("%d",&data);
	}
	end->next = NULL;
	return head;
}
首先找到第一个不是m的头节点,标记
 p第一个不为m的首部,end 尾结点,和尾插法一样
struct ListNode *deletem( struct ListNode *L, int m ){
	struct ListNode *p,*end;
	while(L->data == m){		//找到第一个节点
			L = L->next;
		if(L == NULL)	// 查完表后 发现都是m的节点,并且到最后null时,退出
			return NULL;
	}
	p = L;	
	end = p->next;
	while(end != NULL){
		while(end->data==m){
			end = end ->next;
			if(end == NULL)
				break;
		}
		if(end == NULL){
			break;
		}
		p->next = end;
		p = end;
		end = end->next;
	}
	p->next =NULL;
	return L;
}

我比较菜,写的代码都有点长 ,谅解
最后祝愿都能顺利的写完函数题。
链表很重要!!!!

百度网盘链接

提取码ffff

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780243.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号