[PTA] 第十一章 题解




前言


因为所写的平台目前还未升级,只支持C90 标准,所以代码相对来说,比较详细,同时也便于读者小白更好地理解代码思路,帮助学弟学妹们快速入门C语言。

更多章节可以点击 → PTA丨 《C语言程序设计》 相关题解



习题11-1 输出月份英文名


1)题目描述


本题要求实现函数,可以返回一个给定月份的英文名称。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

函数接口定义:
char *getmonth( int n );
函数getmonth应返回存储了n对应的月份英文名称的字符串头指针。如果传入的参数n不是一个代表月份的数字,则返回空指针NULL。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

裁判测试程序样例:

#include <stdio.h>

char *getmonth( int n );

int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}

/* 请将你的代码嵌在这里与上述代码一起提交 */

输入样例:

5

输出样例:

May


2) 代码示例

#include <stdio.h>
char *getmonth( int n );
int main()
{
    int n;
    char *s;

    scanf("%d", &n);
    s = getmonth(n);
    if ( s==NULL ) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}
char *getmonth( int n )
{
    char *a[12] = { "January", "February", "March", "April", "May", "June", "July","August","September","October","November","December" };
    if(n>=1&&n<=12)
        return a[n-1];
    else
        return NULL;
}



习题11-2 查找星期


1)题目描述


本题要求实现函数,可以根据下表查找到星期,返回对应的序号。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

序号星期
0Sunday
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday

函数接口定义:
int getindex( char *s );

函数getindex应返回字符串s序号。如果传入的参数s不是一个代表星期的字符串,则返回-1。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

裁判测试程序样例:

#include <stdio.h>
#include <string.h>

#define MAXS 80

int getindex( char *s );

int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);

    return 0;
}

/* 请将你的代码嵌在这里与上述代码一起提交 */

输入样例:

Tuesday

输出样例:

2


2) 代码示例

#include <stdio.h>
#include <string.h>
#define MAXS 80
int getindex( char *s );
int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if ( n==-1 ) printf("wrong input!\n");
    else printf("%d\n", n);

    return 0;
}
int getindex( char *s )
{
    char *a[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int i;
	for (i = 0; i <= 6; i++)
	{
		if (strcmp(s,a[i]) == 0)
			break;
	}
	if (i == 7)
		i = -1;
	return i;
}



习题11-3 计算最长的字符串长度


1)题目描述


本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

函数接口定义:
int max_len( char *s[], int n );

其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

裁判测试程序样例:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 10
#define MAXS 20

int max_len( char *s[], int n );

int main()
{
    int i, n;
    char *string[MAXN] = {NULL};

    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        string[i] = (char *)malloc(sizeof(char)*MAXS);
        scanf("%s", string[i]);
    }
    printf("%d\n", max_len(string, n));

    return 0;
}

/* 将你的代码嵌在这里与上述代码一起提交 */

输入样例:
4
blue
yellow
red
green
输出样例:
6


2) 代码示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10
#define MAXS 20

int max_len( char *s[], int n );

int main()
{
    int i, n;
    char *string[MAXN] = {NULL};

    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        string[i] = (char *)malloc(sizeof(char)*MAXS);
        scanf("%s", string[i]);
    }
    printf("%d\n", max_len(string, n));

    return 0;
}
int max_len( char *s[], int n )
{
int max=0,k;
for(k=1;k<n;k++)
		if(strlen(s[k])>max)
		    max = strlen(s[k]);
return max;
}




习题11-4 字符串的连接


1)题目描述


本题要求实现一个函数,将两个字符串连接起来。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

函数接口定义:
char *str_cat( char *s, char *t );

函数str_cat应将字符串t复制到字符串s的末端,并且返回字符串s的首地址。
裁判测试程序样例:

#include <stdio.h>
#include <string.h>

#define MAXS 100

char *str_cat( char *s, char *t );

int main()
{
    char *p;
    char str1[MAXS+MAXS] = {'\0'}, str2[MAXS] = {'\0'};

    scanf("%s%s", str1, str2);
    p = str_cat(str1, str2);
    printf("%s\n%s\n", p, str1);

    return 0;
}

/* 将你的代码被嵌在这里,与上述代码一起提交 */

输入样例:

abc
def

输出样例:

abcdef
abcdef


2) 代码示例

#include <stdio.h>
#include <string.h>
#define MAXS 100
char *str_cat( char *s, char *t );

int main()
{
    char *p;
    char str1[MAXS+MAXS] = {'\0'}, str2[MAXS] = {'\0'};

    scanf("%s%s", str1, str2);
    p = str_cat(str1, str2);
    printf("%s\n%s\n", p, str1);

    return 0;
}
char *str_cat( char *s, char *t )
{
	int a,i;
	a = strlen(s);
	for (i = 0; *(t + i) != '\0'; i++)
	{
		*(s + i + a) = *(t + i);
	}
 
	return s;
}



习题11-5 指定位置输出字符串


1)题目描述


本题要求实现一个函数,对给定的一个字符串和两个字符,打印出给定字符串中从与第一个字符匹配的位置开始到与第二个字符匹配的位置之间的所有字符。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

函数接口定义:
char *match( char *s, char ch1, char ch2 );

函数match应打印s中从ch1到ch2之间的所有字符,并且返回ch1的地址。
裁判测试程序样例:

#include <stdio.h>

#define MAXS 100

char *match( char *s, char ch1, char ch2 );

int main()
{
    char str[MAXS], ch_start, ch_end, *p;

    scanf("%s\n", str);
    scanf("%c %c", &ch_start, &ch_end);
    p = match(str, ch_start, ch_end);
    printf("%s\n", p);

    return 0;
}

/*将你的代码被嵌在这里,与上述代码一起提交 */

输入样例1:

program
r g

输出样例1:

rog
rogram

输入样例2:

program
z o

输出样例2:

(空行)
(空行)

输入样例3:

program
g z

输出样例3:

gram
gram


2) 代码示例

#include <stdio.h>
#define MAXS 100
char *match( char *s, char ch1, char ch2 );
int main()
{
    char str[MAXS], ch_start, ch_end, *p;

    scanf("%s\n", str);
    scanf("%c %c", &ch_start, &ch_end);
    p = match(str, ch_start, ch_end);
    printf("%s\n", p);

    return 0;
}
char *match( char *s, char ch1, char ch2 )
{
    char *p;
	int i,j;
	for(i=0;i<strlen(s);i++)
		if(s[i]==ch1)break;
	p=&s[i];
	for(j=i;j<strlen(s);j++)
	{
		printf("%c",s[j]);
		if(s[j]==ch2)break;
	}
	printf("\n");
	return p;
}



习题11-6 查找子串


1)题目描述


本题要求实现一个字符串查找的简单函数。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

函数接口定义:
char *search( char *s, char *t );

函数search在字符串s中查找子串t,返回子串t在s中的首地址。若未找到,则返回NULL。

裁判测试程序样例:

#include <stdio.h>
#define MAXS 30

char *search(char *s, char *t);
void ReadString( char s[] ); /* 裁判提供,细节不表 */

int main()
{
    char s[MAXS], t[MAXS], *pos;

    ReadString(s);
    ReadString(t);
    pos = search(s, t);
    if ( pos != NULL )
        printf("%d\n", pos - s);
    else
        printf("-1\n");

    return 0;
}
void ReadString( char s[] )
{
    gets(s);
}
  /* 将你的代码嵌在这里,与上述代码一起提交 */

输入样例1:

The C Programming Language
ram

输出样例1:

10


2) 代码示例

#include <stdio.h>
#include<string.h>
#define MAXS 30
char *search(char *s, char *t);
void ReadString( char s[] ); /* 裁判提供,细节不表 */
int main()
{
    char s[MAXS], t[MAXS], *pos;

    ReadString(s);
    ReadString(t);
    pos = search(s, t);
    if ( pos != NULL )
        printf("%d\n", pos - s);
    else
        printf("-1\n");

    return 0;
}
void ReadString( char s[] )
{
    gets(s);
}
char *search(char *s, char *t)
{
    int i,j,k;
    int x=strlen(s)-strlen(t);
    for (i=0; i<=x; i++)
    {
        for (j=0,k=i; j<strlen(t); j++,k++)
        {
            if (s[k]!=t[j])
                break;
        }
        if (j==strlen(t))
            break;
    }
    if (i<=x) return s+i;
    else return NULL;   
}



习题11-7 奇数值结点链表


1)题目描述


本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。
链表结点定义如下:‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
struct ListNode { int data; ListNode *next; };

函数接口定义:

struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数getodd将单链表L中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L的指针)。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    struct ListNode *L, *Odd;
    L = readlist();
    Odd = getodd(&L);
    printlist(Odd);
    printlist(L);

    return 0;
}

/* 将你的代码嵌在这里,与上述代码一起提交 */

输入样例:

1 2 2 3 4 5 6 7 -1

输出样例:

1 3 5 7
2 2 4 6


2) 代码示例

#include <stdio.h>
#include <stdlib.h>
#define N 20
struct ListNode 
{
    int data;
    struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *getodd( struct ListNode **L );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}
int main()
{
    struct ListNode *L, *Odd;
    L = readlist();
    Odd = getodd(&L);
    printlist(Odd);
    printlist(L);
 
    return 0;
}
struct ListNode *readlist(){
	int n,flag=0;
	struct ListNode *head,*tail;
	scanf("%d",&n);
	while(n!=-1){
		struct ListNode *temp=(struct ListNode*)malloc(sizeof(struct ListNode));
		temp->next=NULL;
		if(!flag++)
			head=tail=temp;
		else
			tail->next=temp;
		tail=temp;
		temp->data=n;
		scanf("%d",&n);	
	}
	return head;
}
struct ListNode *getodd( struct ListNode **L ){
	struct ListNode *head=NULL,*temp=*L,*pre=NULL,*odd=NULL;
	while(temp){
		if(temp->data%2){
			if(odd)
				odd->next=temp;
			else
				head=temp;
				
			if(pre){
				pre->next=temp->next;
				temp->next=NULL;
				odd=temp;
				temp=pre->next;
			}
			else{
				odd=temp;
				temp=temp->next;
				*L=temp;
				odd->next=NULL;
			}
			continue;
		}
		pre=temp;
		temp=temp->next;
	}
	return head;
}




习题11-8 单链表结点删除


1)题目描述


本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。

链表结点定义如下:‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
struct ListNode { int data; ListNode *next; };

函数接口定义:
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );

函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。

函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

/* 将你的代码嵌在这里,与上述代码一起提交 */

输入样例:

10 11 10 12 10 -1
10

输出样例:
11 12


2) 代码示例

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

struct ListNode *readlist()
{
	int num;
	struct ListNode *head,*p;
	head=(struct ListNode*)malloc(sizeof(struct ListNode));
	p=head;
	while(scanf("%d",&num )!=NULL&&num!=-1)
	{
		p->next =(struct ListNode*)malloc(sizeof(struct ListNode));
		p=p->next ;
		p->data =num;
		p->next=NULL;
	}
	head=head->next ;
	
	return (head);
}
struct ListNode *deletem( struct ListNode *L, int m )
{
	struct ListNode *p,*q;
	if(L==NULL)
	{
		return NULL;
	}
	p=L;q=p->next ;
	while(q)
	{
		if( q->data ==m )
		{
			p->next =q->next ;
			free(q);
			q=p->next ;
		}
		else 
		{
			p=p->next ;
			q=p->next ;
		}
	}
	if(L->data ==m )
	{
		L=L->next ;
	}
	
	return L;
}



后序

更多章节可以点击 → PTA丨 《C语言程序设计》 相关题解

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

湫喃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值