PTA - C语言接口题集

6-1 计算两个复数之积(结构体函数)

本题要求实现一个计算复数之积的简单函数。

函数接口定义:

struct complex multiply(struct complex x, struct complex y);

其中struct complex是复数结构体,其定义如下:

struct complex{
    int real;
    int imag;
};

裁判测试程序样例:

#include <stdio.h>

struct complex{
    int real;
    int imag;
};
struct complex multiply(struct complex x, struct complex y);
int main()
{
    struct complex product, x, y;
    scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
    product = multiply(x, y);
    printf("(%d+%di) * (%d+%di) = %d + %di\n", 
            x.real, x.imag, y.real, y.imag, product.real, product.imag);
    
    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例:
3 4 5 6
输出样例:
(3+4i) * (5+6i) = -9 + 38i

struct complex multiply(struct complex x, struct complex y){
    struct complex ans;
    ans.real = x.real*y.real-x.imag*y.imag;
    ans.imag = x.real*y.imag+y.real*x.imag;

    return ans;
}

6-2 字符定位(返回字符的地址,指针)

本题要求定义一个函数,在字符串中查找字符,并定位在最后一次找到的位置。

函数接口定义:

char * match(char *s, char ch);

其中s是字符串首地址,ch是要查找的字符。要求函数在字符串s中查找字符ch,如果找到,返回最后一次找到的该字符在字符串中的位置(地址);否则,返回空指针NULL。

裁判测试程序样例:

#include <stdio.h> 

char *match(char *s, char ch); 
int main(void )
{
    char ch, str[80], *p = NULL;
    scanf("%s", str);
    getchar();                 /* 跳过输入字符串和输入字符之间的分隔符 */
    ch = getchar();          /* 输入一个字符 */
    p = match(str, ch);     /* 调用函数match() */
    if( p != NULL ){        /* 找到字符ch */
        printf("%s\n", p);
    }else{ 
        printf("Not Found.\n");
    }

    return 0;
}

/* 请在这里填写答案 */
输入样例1:
program
r
输出样例1:
ram
输入样例2:
program
x
输出样例2:
Not Found.

char * match(char *s, char ch){
    char* ans=NULL;
    for(int i=0;s[i]!='\0';i++){
        if(s[i]==ch) {
            ans=&s[i];
        }
    }
    return ans;
}

6-3 求结构体平均成绩(变量名(数组名)用.;指针(带有*)用->)

本题要求实现一个函数,可统计结构体中成绩项的平均成绩。

结构体由两项组成:学号和成绩。

函数接口定义:

double avg(RECORD x[],int n);

其中 n 是结构体数组的元素个数。

裁判测试程序样例:

#include <stdio.h>
#define N 20

typedef struct
{
    char no[10];
    double score;
}RECORD;
double avg(RECORD x[],int n);
int main()
{
    RECORD a[N];
    int n,i;
    double av;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%s%lf",a[i].no,&a[i].score);
    av=avg(a,n);
    printf("%f",av);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
5
1001 1
1002 2
1003 3
1004 4
1005 5
输出样例:
3.000000

double avg(RECORD x[],int n){
    double ans=0;
    for(int i=0;i<n;i++){
        ans+=x[i].score;
    }
    ans/=n;
    return ans;
}

6-4 删除字符串中数字字符

删除一个字符串中的所有数字字符。

函数接口定义:

void delnum(char *s);

其中 s 是用户传入的参数。 函数的功能是删除指针 s 所指的字符串中的所有数字字符。

裁判测试程序样例:

#include "stdio.h"

void delnum(char *s);
int main ()
{ 
	char item[80];
	gets(item);
	delnum(item);
	printf("%s\n",item);
 
	return 0;
}

/* 请在这里填写答案 */
输入样例:
a0bc+d496df
输出样例:
abc+ddf

void delnum(char *s){
    int i=0;
    while(s[i]!='\0'){
        if(s[i]>='0' && s[i]<='9')
        // if(isdigit(s[i]))
        {
            int j=i;
            while(s[j]!='\0'){
                s[j]=s[j+1];
                j++;
            }
        }
        else i++;
    }
}

6-5 使用函数找出数组中的最大值

本题要求实现一个找出整型数组中最大值的函数。

函数接口定义:

int FindArrayMax( int a[], int n );

其中a是用户传入的数组,n是数组a中元素的个数。函数返回数组a中的最大值。

裁判测试程序样例:

#include <stdio.h>
#define MAXN 10

int FindArrayMax( int a[], int n );
int main()
{
    int i, n;
    int a[MAXN];
    scanf("%d", &n);
    for( i=0; i<n; i++ ){
        scanf("%d", &a[i]);
    }
    printf("%d\n", FindArrayMax(a, n));
   
    return 0;
}

/* 请在这里填写答案 */
输入样例:
4
20 78 99 -14
输出样例:
99

int FindArrayMax( int a[], int n ){
    int ans=a[0];
    for(int i=1;i<n;i++){
        if(a[i] > ans) ans = a[i];
    }
    return ans;
}

6-6 在数组中查找指定元素

本题要求实现一个在数组中查找指定元素的简单函数。

函数接口定义:

int search( int list[], int n, int x );

其中list[]是用户传入的数组;n(≥0)是list[]中元素的个数;x是待查找的元素。如果找到

则函数search返回相应元素的最小下标(下标从0开始),否则返回−1。

裁判测试程序样例:

#include <stdio.h>
#define MAXN 10

int search( int list[], int n, int x );
int main()
{
    int i, index, n, x;
    int a[MAXN];
    scanf("%d", &n);
    for( i = 0; i < n; i++ )
        scanf("%d", &a[i]);
    scanf("%d", &x);
    index = search( a, n, x );
    if( index != -1 )
        printf("index = %d\n", index);
    else
        printf("Not found\n");
            
    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例1:
5
1 2 2 5 4
2
输出样例1:
index = 1
输入样例2:
5
1 2 2 5 4
0
输出样例2:
Not found

int search( int list[], int n, int x ){
    for(int i=0;i<n;i++){
        if(list[i]==x) return i;
    }
    return -1;
}

6-7 按等级统计学生成绩

本题要求实现一个根据学生成绩设置其等级,并统计不及格人数的简单函数。

函数接口定义:

int set_grade( struct student *p, int n );

其中p是指向学生信息的结构体数组的指针,该结构体的定义为:

struct student{
    int num;
    char name[20];
    int score;
    char grade;
};

n是数组元素个数。学号num、姓名name和成绩score均是已经存储好的。set_grade函数需要根据学生的成绩score设置其等级grade。等级设置:85-100为A,70-84为B,60-69为C,0-59为D。同时,set_grade还需要返回不及格的人数。

裁判测试程序样例:

#include <stdio.h>
#define MAXN 10

struct student{
    int num;
    char name[20];
    int score;
    char grade;
};
int set_grade( struct student *p, int n );
int main()
{  
	struct student stu[MAXN], *ptr;
	int n, i, count;
	ptr = stu;
	scanf("%d\n", &n);
	for(i = 0; i < n; i++){
		scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
	} 
	count = set_grade(ptr, n);
	printf("The count for failed (<60): %d\n", count);
	printf("The grades:\n"); 
	for(i = 0; i < n; i++)
		printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
	
	return 0;
}

/* 你的代码将被嵌在这里 */
输入样例:
10
31001 annie 85
31002 bonny 75
31003 carol 70
31004 dan 84
31005 susan 90
31006 paul 69
31007 pam 60
31008 apple 50
31009 nancy 100
31010 bob 78
输出样例:
The count for failed (<60): 1
The grades:
31001 annie A
31002 bonny B
31003 carol B
31004 dan B
31005 susan A
31006 paul C
31007 pam C
31008 apple D
31009 nancy A
31010 bob B

int set_grade( struct student *p, int n ){
    int ans=0;
    for(int i=0;i<n;i++){
        if(p[i].score <60) ans++;
        
        if(p[i].score>=85 && p[i].score<=100) p[i].grade='A';
        else if(p[i].score>=70) p[i].grade='B';
        else if(p[i].score>=60) p[i].grade='C';
        else p[i].grade='D';
    }
    return ans;
}

6-8 学生成绩比高低

学生结构体定义如下:

struct Student{
    int sid;
    int C;
    int English;
};

其中sid是学号,C是C语言课程成绩,English是英语课程成绩。学生的成绩按照这样的规则比较:
先比较两门课的总成绩,总成绩高的为优;
若总成绩相同,再比较C语言成绩,C语言成绩高的为优;
若C语言成绩也相同,则说明两名学生成绩相等。

函数接口定义:

int compareScore(const struct Student *s1, const struct Student *s2);

其中s1s2是传入的参数,分别指向两名学生的结构体变量。函数返回值为int型,
s1所指学生成绩优于s2所指学生,返回1
s2所指学生成绩优于s1所指学生,返回-1
若两学生成绩相等,返回0
裁判测试程序样例:

#include <stdio.h>

struct Student{
    int sid;
    int C;
    int English;
};
int compareScore(const struct Student *s1, const struct Student *s2);
int main(){
    struct Student zs, ls;
    scanf("%d%d%d", &zs.sid, &zs.C, &zs.English);
    scanf("%d%d%d", &ls.sid, &ls.C, &ls.English);
    int r;
    r = compareScore(&zs, &ls);
    if(r < 0) printf("Less\n");
    else if(r > 0) printf("Greater\n");
    else printf("Equal\n");
    
    return 0;
}

/* 你所编写的函数代码将被嵌在这里 */
输入样例1:
1 95 90
2 90 91
输出样例1:
Greater
输入样例2:
1 90 95
2 95 90
输出样例2:
Less

int compareScore(const struct Student *s1, const struct Student *s2) {
    int sum1 = s1->C + s1->English;
    int sum2 = s2->C + s2->English;

    if (sum1 != sum2) {
        return sum1 > sum2 ? 1 : -1;
    } else if (s1->C != s2->C) {
        return s1->C > s2->C ? 1 : -1;
    } else {
        return 0;
    }
}

6-11 mystrcpy

编写一个函数 char *mystrcpy(char *s1, const char *s2)

它的功能是把字符串s2中的所有字符复制到字符数组s1中。函数返回指向数组s1第一个元素的指针。

函数接口定义:
函数接口:

char *mystrcpy(char *s1, const char *s2);

把字符串s2中的所有字符复制到字符数组s1中。函数返回指向数组s1第一个元素的指针。

裁判测试程序样例:

#include <stdio.h>

char *mystrcpy(char *s1, const char *s2);
int main()
{
    char a[30], b[30], c[30];
    gets(c);
    mystrcpy(a, mystrcpy(b, c));
    puts(a);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
abcd
输出样例:
abcd

char *mystrcpy(char *s1, const char *s2){
    char* ans=s1;
    int i=0;
    for(i=0;s2[i]!='\0';i++){
        s1[i]=s2[i];
    }
    s1[i]='\0';
    return ans;
}

6-12 mystrcat

编写一个函数 char* mystrcat(char *s1, const char *s2)

函数功能是把字符串s2的所有元素连接到字符串s1之后。函数返回指向字符串s1第一个元素的指针。

函数接口定义:
函数接口:

 char* mystrcat(char *s1, const char *s2);

把字符串s2的所有元素连接到字符串s1之后。函数返回指向字符串s1第一个元素的指针。

裁判测试程序样例:

#include <stdio.h>

char *mystrcat(char *s1, const char *s2);
int main()
{
    char a[100] = "abcd", b[60], c[30];
    scanf("%s", b);
    scanf("%s", c);
    mystrcat(a, mystrcat(b, c));
    puts(a);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
fgh
ijk
输出样例:
abcdfghijk

char* mystrcat(char *s1, const char *s2){
    char* ans=s1;
    int start=0;
    while(s1[start]!='\0') start++;
    int i=0;
    for(i=0;s2[i]!='\0';i++){
        s1[start+i]=s2[i];
    }
    s1[start+i]='\0';
    
    return ans;
}

6-13 mystrcmp

写一个函数 int mystrcmp(const char *string1, const char *string2)

函数的功能是比较2个字符串的大小。返回值表示两个字符串比较的结果。

函数接口定义:
函数接口:

 int mystrcmp(const char *string1, const char *string2);

函数的功能是比较2个字符串的大小。返回值表示两个字符串比较的结果。
图
字符串比较的规则是对两个字符串自左至右逐个字符相比较(按ASCII码值大小比较),直到出现不同的字符或遇到‘\0’为止。如全部字符相同,则认为相等;若出现不同的字符,则以第一个不相同的字符的比较结果为准。
例如:

“A”< “B”,“a”>“A”,“computer”>“compare”,“these”>“that”, “36+54”<“99”, “CHINA”>“CANADA”, “DOG”<“cat”,“abc”<“ax”,“abcde”>“abcd”。

裁判测试程序样例:

#include <stdio.h>

int mystrcmp(const char *s1, const char *s2);
int main()
{
    char s1[30], s2[30];
    scanf("%s", s1);
    scanf("%s", s2);
    if (mystrcmp(s1, s2) > 0)
        printf("%s > %s\n", s1, s2);
    if (mystrcmp(s1, s2) == 0)
        printf("%s == %s\n", s1, s2);
    if (mystrcmp(s1, s2) < 0)
        printf("%s < %s\n", s1, s2);
        
    return 0;
}

/* 请在这里填写答案 */
输入样例:
abcd
ax
输出样例:
abcd < ax

int mystrcmp(const char *string1, const char *string2){
    while(*string1 && *string2){
        if(*string1 > *string2) return 1;
        else if(*string1 < *string2) return -1;
        string1++;string2++;
    }
    if(*string1) return 1;
    if(*string2) return -1;
    
    return 0;
}

6-14 求正整数的因子和

本题要求实现一个函数,可统计1个正整数的因子中,不包括自己的其它所有因子之和。例如,4的因子和是1+2=3,6的因子和是1+2+3=6,7的因子和是1,8的因子和是1+2+4=7,9的因子和是1+3=4。我们约定1的因子和是0。

函数接口定义:
函数接口如下:

int  factors_sum( int x);

其中’ x’是用户传入的参数。 x 的值不超过int的范围。函数须返回 x 的因子和。

裁判测试程序样例:

#include <stdio.h>

int factors_sum(int x);
int main(void)
{
    int x;
    scanf("%d", &x);
    while (x != -1){
        printf("%d\n", factors_sum(x));
        scanf("%d", &x);
    }
    return 0;
}

/* 请在这里填写答案 */
输入样例:
3
9
7
8
10
-1
输出样例:
1
4
1
7
8

int  factors_sum( int x)
{
    int ans=0;
    for(int i=1;i<x;i++){
        if(x%i==0) ans+=i;
    }
    return ans;
}

6-15 字符串翻转

本题要求实现一个函数,把一个字符串前后翻转。

函数接口定义:
函数接口如下:

void reverse(char *start, char *end);

参数中的start指针和end指针指向同一个字符串,start指针小于等于end指针。

裁判测试程序样例:

#include <stdio.h>

void reverse(char *start, char *end);
int main(void)
{
    char s[] = "abcdefghijklmnopqrstuvwxyz";
    printf("%s\n", s);
    reverse(s + 1, s + 3);
    reverse(s + 6, s + 10);
    reverse(s + 13, s + 21);
    printf("%s\n", s);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:

输出样例:
abcdefghijklmnopqrstuvwxyz
adcbefkjihglmvutsrqponwxyz

void reverse(char *start, char *end){
    while(*start!=*end){
        char tmp=*start;
        *start=*end;
        *end=tmp;
        start++;end--;
    }
}

6-17 判断完数

例如:本题要求实现一个函数,判断一个自然数是否是完数。如果一个自然数除自身之外的因子和等于它自己,则称该数为完数。例如 6 = 1 + 2 + 3;则6是完数。

函数接口定义:
在这里描述函数接口。:

int isPerfect (int x);

在这里解释接口参数。其中 x 是用户传入的参数。 x 的值不超过int的范围且 x大于0;如果 x是完数,函数返回1,否则返回0。

裁判测试程序样例:

#include <stdio.h>

int isPerfect(int x);
int main()
{
    for (int i = 1; i <= 10000; i++){
        if (isPerfect(i))
            printf("%d\n", i);
    }
    return 0;
}

/* 请在这里填写答案 */
输入样例:
本程序无需输入
输出样例:
6
28
496
8128

int isPerfect (int x){
    if(x==1) return 0;
    
    int sum=0;
    for(int i=1;i<=x/2;i++){
        if(x%i==0) sum+=i;
    }

    return sum==x;
}

6-18 mystrlen

写一个函数,返回一个字符串的长度。
在C语言中,字符串的长度定义为除’\0’之外的字符元素的个数。

函数接口定义:

int mystrlen ( const char *s );

其中 s 是用户传入的字符串。函数返回 字符串 s 的长度。

裁判测试程序样例:

#include <stdio.h>

int mystrlen(const char *s);
int main(void)
{
    char s[30];
    gets(s);
    printf("%d", mystrlen(s));
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
Hello world!
输出样例:
12

int mystrlen ( const char *s ){
    int ans=0;
    for(int i=0;s[i]!='\0';i++) 
        ans++;

    return ans;
}

6-19 mystrcpy

编写一个函数 char *mystrcpy(char *s1, const char *s2)

它的功能是把字符串s2中的所有字符复制到字符数组s1中。函数返回指向数组s1第一个元素的指针。

函数接口定义:

char *mystrcpy(char *s1, const char *s2)

其中 s1s2 都是用户传入的参数。 函数把字符串s2 中的所有字符复制到字符数组s1中。函数返回指向数组s1第一个元素的指针。

裁判测试程序样例:

#include <stdio.h>

char *mystrcpy(char *s1, const char *s2);
int main(void)
{
    char str1[30], str2[30], str3[30];
    gets(str3);
    mystrcpy(str1, mystrcpy(str2, str3));
    printf("str1: %s\n", str1);
    printf("str2: %s\n", str2);
    printf("str3: %s\n", str3);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
Hello world!
输出样例:
str1: Hello world!
str2: Hello world!
str3: Hello world!

char *mystrcpy(char *s1, const char *s2)
{
    char* ans=s1;
    int i=0;
    for(i=0;s2[i]!='\0';i++){
        s1[i]=s2[i];
    }
    s1[i]='\0';

    return ans;
}

6-20 mystrcat

编写一个函数 char* mystrcat(char *s1, const char *s2)

函数功能是把字符串s2的所有元素连接到字符串s1之后。

函数接口定义:

char* mystrcat(char *s1, const char *s2)

裁判测试程序样例:

#include <stdio.h>

char* mystrcat(char *s1, const char *s2);
int main(void)
{
    char str1[100] = "Winter ", str2[60] = "is ", str3[30] = "comming.";
    mystrcat(str1, mystrcat(str2, str3));
    printf("%s", str1);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
无输入
输出样例:
Winter is comming.

char* mystrcat(char *s1, const char *s2)
{
    char* ans=s1;
    int start=0;
    while(s1[start]!='\0') start++;
    int i=0;
    for(i=0;s2[i]!='\0';i++){
        s1[start+i]=s2[i];
    }
    s1[start+i]='\0';

    return ans;
}

6-21 mystrupr

编写一个函数 void mystrup(char *s);
函数功能是把字符串s中的所有小写字母变成大写字母,其它字符不变。

函数接口定义:

void mystrupr(char *s)

其中 s 是一个字符串。

裁判测试程序样例:

#include <stdio.h>

void mystrupr(char *s);
int main(void)
{
    char s[] = "ab+cdEFhijk@cqust";
    mystrupr(s);
    printf("%s", s);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:

输出样例:
AB+CDEFHIJK@CQUST

void mystrupr(char *s){
    for(int i=0;s[i]!='\0';i++){
        if(s[i]>='a' && s[i]<='z') 
            s[i]-=32;
    }
}

6-22 数字字符转整数

编写函数 int myatoi(const char *s);
返回字符串s中所有数字字符按从左到右的顺序组合成的整数。

函数接口定义:

 int myatoi(const char *s);

其中 s 是用户传入的字符串。 字符串s中可能含有数字字符。把s中的数字字符按从左到右的顺序组合成一个整数,返回这个整数的值。
例如,字符串 "ab3A70@9"中,含有数字字符组合成整数3709。如果字符串s中没有数字字符,则返回0。数据保证数字字符组合不超过整型表示范围。

裁判测试程序样例:

#include <stdio.h>

int myatoi(const char *s);
int main(void)
{
    char s1[30] = "ab6@0gap49$";
    char s2[30] = "5rt1";
    int x = myatoi(s1);
    int y = myatoi(s2);
    int z = x + y;
    printf("%d + %d = %d\n", x, y, z);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:

输出样例:
6049 + 51 = 6100

 int myatoi(const char *s){
     int ans=0;
     int flag=1;
     for(int i=0;s[i]!='\0';i++){
         if(s[i]>='1' && s[i]<='9' && flag==1) {
             ans+=s[i]-'0';
             flag=0;
         }
         else if(s[i]>='0' && s[i]<='9' && !flag)
         {
             ans=ans*10+s[i]-'0';
         }
     }
     return ans;
 }

6-23 统计英文字母的个数

编写函数
int countAlpha(const char *s);
返回字符串s中英文字母的个数。

函数接口定义:
在这里描述函数接口。例如:

int countAlpha(const char *s);

其中 s 是用户传入的字符串。 函数返回字符串 s 中英文字母的个数。如果字符串 s 中没有英文字母,则返回0。

裁判测试程序样例:

#include <stdio.h>

int countAlpha(const char *s);
int main(void)
{
    char s[100];
    gets(s);
    printf("%d", countAlpha(s));
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
963AfBy@ t7
输出样例:
5

int countAlpha(const char *s){
    int ans=0;
    for(int i=0;s[i]!='\0';i++){
        if(s[i]>='A' && s[i]<='Z') ans++;
        if(s[i]>='a' && s[i]<='z') ans++;
    }

    return ans;
}

6-24 求字符串中英文字母和数字字符的个数(传址调用,对指针解引用)

本题要求实现一个函数,可统计任一字符串中英文字母和数字字符出现的次数。例如字符串"Ab32+72=A"中,英文字母出现了3次,数字字符出现了4次。

函数接口定义:
在这里描述函数接口。例如:

int countString(char *str, int *pDigitCount);

在这里解释接口参数。例如:其中 strpDigitCount 都是用户传入的参数。 字符指针 str 指向一个以’\0’结束的字符串; 整型指针pDigitCount指向一个用来统计数字字符出现次数的整数 。函数须返回 str 中 英文字母出现的次数。

裁判测试程序样例:

#include <stdio.h>

int countString(char *str, int *pDigitCount);
int main(void)
{
    char buf[30];
    int alphaCount, digitCount;
    gets(buf);
    alphaCount = countString(buf, &digitCount);
    printf("alphaCount = %d\n", alphaCount);
    printf("digitCount = %d\n", digitCount);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例:
Ab32+72=A
输出样例:
alphaCount = 3
digitCount = 4

int countString(char *str, int *pDigitCount){
    int ans=0;
    *pDigitCount=0;
    for(int i=0;str[i]!='\0';i++){
        if(str[i]>='A' && str[i]<='Z') ans++;
        if(str[i]>='a' && str[i]<='z') ans++;

        if(str[i]>='0' && str[i]<='9') (*pDigitCount)++;
    }

    return ans;
}

6-27 回文

回文是前后两个方向拼写完全相同的字符串。回文的例子如“radar”、“able was i ere i saw elba"和"ABCBA”,“ABBA”。很显然,空字符串是回文,任何一个只有1个字符的字符串是回文。编写一个函数 testPalindrome, 判断一个字符串是否是回文。

函数接口定义:
在这里描述函数接口。例如:

int testPalindrome ( char *start, char *end);

其中 startend 都是用户传入的参数。 字符指针 start 指向字符串第一个字符,字符指针end 指向字符串最后一个字符('\0’前的一个字符)。

裁判测试程序样例:

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

int isPalindrome(char *str);
int testPalindrome(char *start, char *end);
int main(void)
{
    char s[30];
    gets(s);
    if (isPalindrome(s))
        printf("%s is a palindrome.\n", s);
    else
        printf("%s is not a palindrome.\n", s);
    return 0;
}

//isPalindrome函数用来调用testPalindrome
//该函数为用户提供了更友好的接口
//我们可以称isPalindrome函数是testPalindrome函数的包装函数
int isPalindrome(char str)
{
return testPalindrome(str, str + strlen(str) - 1);
}
/
请在这里填写答案 */
输入样例1:
radar
输出样例1:
radar is a palindrome.
输入样例2:
ABBA
输出样例2:
ABBA is a palindrome.
输入样例3:
ABCA
输出样例3:
ABCA is not a palindrome.

int testPalindrome ( char *start, char *end){
    while(start<end){
        if(*start!=*end) 
            return 0;
        start++;end--;
    }
    return 1;
}

6-28 递归顺序输出字符串

编写一个递归函数 displayString,顺序输出一个字符串的所有元素。

函数接口定义:

void displayString(char *s)

其中 s 是用户传入的参数。 s 指向一个以‘\0’结束的字符串第一个字符。

裁判测试程序样例:

#include <stdio.h>

void displayString(char *s);
int main(void)
{
    char s[30];
    gets(s);
    displayString(s);
    return 0;
}

/* 请在这里填写答案 */
输入样例:
Hello world
输出样例:
Hello world

// 递归顺序输出
void displayString(char *s){
    if(*s=='\0') return;// 递归出口

    printf("%c",*s);
    displayString(s+1);
}

6-29 逆序输出字符串

编写一个递归函数 displayStringReverse,逆序输出一个字符串的所有元素。

函数接口定义:

void displayStringReverse(char *s);

其中 s 是用户传入的参数。 s 指向一个以‘\0’结束的字符串第一个字符。

裁判测试程序样例:

#include <stdio.h>

void displayStringReverse(char *s);
int main(void)
{
    char s[30];
    gets(s);
    displayStringReverse(s);
    return 0;
}

/* 请在这里填写答案 */
输入样例:
Hello world!
输出样例:
!dlrow olleH

// 递归逆序输出
void displayStringReverse(char *s){
    if(*s=='\0') return;// 递归出口

    displayStringReverse(s+1);
    printf("%c",*s);
    
}

6-30 四舍五入(实现round函数)

本题要求实现一个函数myRound,返回其浮点型参数四舍五入之后得到的整数。例如myRound(6.49)应该返回6,而myRound(6.51)应该返回7。

函数接口定义:

int myRound (double x);

其中 x 是用户传入的参数。 x 的值不超过double的范围。函数须返回 x 四舍五入后得到的整型数。

裁判测试程序样例:

#include <stdio.h>

int myRound(double x);
int main(void)
{
    double x;
    int y;
    scanf("%lf", &x);
    y = myRound(x);
    printf("x = %f, y = %d\n", x, y);
    
    return 0;
}

/* 请在这里填写答案 */
输入样例1:
6.49
输出样例1:
x = 6.490000, y = 6
输入样例2:
6.51
输出样例2:
x = 6.510000, y = 7

int myRound (double x){
    int ans=(int)x;
    x*=10;
    int res=(int)(x-(int)x/10*10);
    
    return res>=5?ans+1:ans;
}

6-31 Sum of the nodes of a Singly Linked List with dummy header

In this problem you will implement a function for a singly-linked list for geting the sum of the nodes.

Format of functions:

ElementType getSum(List L);

The parameter L is a pointer to the dummy header. The function return the sum of the nodes of a Singly Linked List with dummy header.

where List is defined as the following:

typedef int ElementType; 
struct Node 
{ 
        ElementType data; 
        struct Node* next; 
}; 
typedef struct Node* PtrToNode; 
typedef PtrToNode List;

A singly linked list is a data structure in which an element has two parts one is the value and other is the link to the next element. So to find the sum of all elements of the singly linked list, we have to navigate to each node of the linked list and add the element’s value to a sum variable.

For example
Suppose we have a linked list: Dummy Header -> 2 -> 27 -> 32 -> 1 -> 5
sum = 2 + 27 + 32 + 1 + 5 = 67.

Sample program of judge:

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

typedef int ElementType;
struct Node
{
    ElementType data;
    struct Node* next;
};
typedef struct Node* PtrToNode;
typedef PtrToNode List;
ElementType getSum(List L);
int main(void)
{
    List L = malloc(sizeof(struct Node));
    L->next = NULL;
    ElementType x;
    scanf("%d", &x);
    while (x != -1)
    {
        PtrToNode t = malloc(sizeof (struct Node));
        t->data = x;
        t->next = L->next;
        L->next = t;
        scanf("%d", &x);
    }
    printf("%d", getSum(L));

    return 0;
}

/* Your functions will be put here */

Sample Input:
2 27 32 1 5 -1
Sample Output:
67

ElementType getSum(List L){
    int sum=0;
    for(List p=L->next;p!=NULL;p=p->next){
        sum+=p->data;
    }
    return sum;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值