天梯初级练习题目 6-41 ~ 6-50 含全部测试点

6-41 十进制转换二进制 分数 15

本题要求实现一个函数,将非负整数n转换为二进制后输出。

函数接口定义:

void dectobin( int n );

函数dectobin应在一行中打印出二进制的n。建议用递归实现。

裁判测试程序样例:

#include <stdio.h>

void dectobin( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    dectobin(n);
    
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

10

输出样例:

1010

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

void dectobin(int n)
{
    int st[100010], idx = -1;
    do st[++ idx] = n % 2; while(n /= 2);
    while(idx != -1) printf("%d", st[idx --]);
}

6-42 递归求阶乘和 分数 15

本题要求实现一个计算非负整数阶乘的简单函数,并利用该函数求 1!+2!+3!+...+n! 的值。

函数接口定义:

double fact( int n ); double factsum( int n );

函数fact应返回n的阶乘,建议用递归实现。函数factsum应返回 1!+2!+...+n! 的值。题目保证输入输出在双精度范围内。

裁判测试程序样例:

#include <stdio.h>

double fact( int n );
double factsum( int n );

int main()
{
    int n;

    scanf("%d",&n);
    printf("fact(%d) = %.0f\n", n, fact(n));
    printf("sum = %.0f\n", factsum(n));
        
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

10

输出样例1:

fact(10) = 3628800
sum = 4037913

输入样例2:

0

输出样例2:

fact(0) = 1
sum = 0

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

double fact(int n)
{
    if(!n) return 1;
    return n * fact(n - 1);
}

double factsum(int n)
{
    if(!n) return 0;
    return factsum(n - 1) + fact(n);
}

6-43 递归计算P函数 分数 15

本题要求实现下列函数P(n,x)的计算,其函数定义如下:

函数接口定义:

double P( int n, double x );

其中n是用户传入的非负整数,x是双精度浮点数。函数P返回P(n,x)函数的相应值。题目保证输入输出都在双精度范围内。

裁判测试程序样例:

#include <stdio.h>

double P( int n, double x );

int main()
{
    int n;
    double x;
    
    scanf("%d %lf", &n, &x);
    printf("%.2f\n", P(n,x));
    
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

10 1.7

输出样例:

3.05

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

double P(int n, double x)
{
    if(!n) return 1;
    else if(n == 1) return x;
    return ((2 * n - 1) * P(n - 1, x) - (n - 1) * P(n - 2, x)) / n;
}

6-44 查找星期 分数 15

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

序号星期
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;
}

/* 你的代码将被嵌在这里 */

输入样例1:

Tuesday

输出样例1:

2

输入样例2:

today

输出样例2:

wrong input!

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

char* st[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int getindex(char* s)
{
    for(int i = 0; i < 7; ++ i)
        if(!strcmp(s, st[i])) return i;
    return -1;
}

6-45 输出月份英文名 分数 15

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

函数接口定义:

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;
}

/* 你的代码将被嵌在这里 */

输入样例1:

5

输出样例1:

May

输入样例2:

15

输出样例2:

wrong input!

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

char* st[13] = {" ", "January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"};

char* getmonth(int n)
{
    if(n < 1 || n > 12) return NULL;
    return st[n];
}

6-46 指定位置输出字符串 分数 20

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

函数接口定义:

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

函数match应打印s中从ch1ch2之间的所有字符,并且返回ch1的地址。

裁判测试程序样例:

#include <stdio.h>

#define MAXS 10

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

char* match(char* s, char c, char op)
{
    int d1 = strlen(s), d2 = 0;
    for(int i = 0; s[i]; ++ i)
        if(s[i] == c)
        {
            d1=  i;
            break;
        }
    
    for(int i = d1; s[i]; ++ i)
    {
        putchar(s[i]);
        if(s[i] == op) break;
    } putchar('\n');
    return s + d1;
}

6-47 查找子串 分数 20

本题要求实现一个字符串查找的简单函数。

函数接口定义:

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;
}

/* 你的代码将被嵌在这里 */

输入样例1:

The C Programming Language
ram

输出样例1:

10

输入样例2:

The C Programming Language
bored

输出样例2:

-1

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

//KMP模板
char ne[100010] = {};
char* search(char* s, char* p)
{
	ne[0] = -1;
	for (int i = 1, j = -1; p[i]; ++i)
	{
		while (j >= 0 && p[i] != p[j + 1]) j = ne[j];
		if (p[i] == p[j + 1]) ++j;
		ne[i] = j;
	}

	for (int i = 0, j = -1; s[i]; ++i)
	{
		while (j != -1 && s[i] != p[j + 1]) j = ne[j];
		if (s[i] == p[j + 1]) ++j;
		if (j == strlen(p) - 1) return s + i - j;
	}
	return NULL;
}

6-48 计算最长的字符串长度 分数 15

本题要求实现一个函数,用于计算有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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

int max_len(char* s[], int n)
{
    int ans = 0;
    for(int i = 0; i < n; ++ i)
        ans = ans > strlen(s[i]) ? ans : strlen(s[i]);
    return ans;
}

6-49 建立学生信息链表 分数 20

本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。

函数接口定义:

void input();

该函数利用scanf从输入中获取学生的信息,并将其组织成单向链表。链表节点结构定义如下:

struct stud_node {
    int              num;      /*学号*/
    char             name[20]; /*姓名*/
    int              score;    /*成绩*/
    struct stud_node *next;    /*指向下个结点的指针*/
};

单向链表的头尾指针保存在全局变量headtail中。

输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

裁判测试程序样例:

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

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};
struct stud_node *head, *tail;

void input();

int main()
{
    struct stud_node *p;
    
    head = tail = NULL;
    input();
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0

输出样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

typedef struct stud_node st;

void input()
{
    int e, sc;
    char sn[20];
    while(scanf("%d", &e), e)
    {
        scanf("%s %d", sn, &sc);
        
        st* node = malloc(sizeof(st));
        node->num = e, node->score=  sc;
        strcpy(node->name, sn);
        if(!head) head = tail = node;
        else
        {
            tail->next = node;
            tail = node;
        }
    }
}

6-50 学生成绩链表处理 分数 20

本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

函数接口定义:

struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:

struct stud_node {
    int              num;      /*学号*/
    char             name[20]; /*姓名*/
    int              score;    /*成绩*/
    struct stud_node *next;    /*指向下个结点的指针*/
};

输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。

裁判测试程序样例:

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

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};

struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;

    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80

输出样例:

2 wang 80
4 zhao 85

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include <string.h>
typedef struct stud_node sd;
sd* createlist()
{
    int e;
    sd* tail = NULL, *head = NULL;
    while(scanf("%d", &e), e)
    {
        char st[20]; int sc;
        scanf("%s %d", st, &sc);
        
        sd* node = malloc(sizeof(sd));
        node->num = e, node->score = sc;
        strcpy(node->name, st);
        node->next = NULL;
        
        if(!head) head = tail = node;
        else
        {
            tail->next = node;
            tail = node;
        }
    }
    return head;
}

sd* deletelist(sd* hd, int n)
{
    sd* tt = malloc(sizeof(sd));
    tt->next = hd;
    for(sd* pp = tt, *node = hd; node;)
        if(node->score < n)
        {
            sd* temp = node;
            pp->next = node->next;
            node = node->next;
            free(temp);
        }
        else 
        {
            pp = pp->next;
            node = node->next;
        }
    return tt->next;
}

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值