目录

PTA 浙大版《C语言程序设计(第4版)》题目集 参考答案(函数题)

本文是函数题答案,编程题答案链接
PTA 浙大版《C语言程序设计(第4版)》题目集 参考答案(编程题)

本答案配套详解教程专栏

😀😀 欢 迎 订 阅😀😀

PTA浙大版《C语言程序设计(第4版)》题目集 详解教程

练习 5-1 求m到n之和 (10 分)

int sum( int m, int n ){
    int sum;
    sum=(m+n)*(n-m+1)/2;
    return sum;
}

练习5-2 找两个数中最大者 (10 分)

int max( int a, int b ){
    return (a>b)? a:b;
}

练习5-3 字符金字塔 (15 分)

void CharPyramid( int n, char ch ){
    int i,j;
    for(i=1;i<=n;i++){
        for(j=0;j<n-i;j++)printf(" ");
        for(j=0;j<i;j++)printf("%c ", ch);
        printf("\n");
    }
}

习题5-1 符号函数 (10 分)

int sign(int x){  
    int y;
    if(x > 0)  y = 1;
    else if(x == 0) y = 0;
    else y = -1;
    return y;
}

习题5-2 使用函数求奇数和 (15 分)

int even( int n ){
    int even;
    if(n%2==0)even=1;
    else even=0;
    return even;
}
int OddSum( int List[], int N ){
    int OddSum=0;
    for(int i=0;i<N;i++)
        if(!even(List[i]))
            OddSum+=List[i];
    return OddSum;
}

习题5-3 使用函数计算两点间的距离 (10 分)

double dist (double x1, double y1 , double x2 , double y2){
	double distant;
	distant=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
	return distant;
}

习题5-4 使用函数求素数和 (20 分)

int prime( int p ){
    int prime;
    if(p<=1)prime=0;
    else if(p==2)prime=1;
    else{
        int i;
        prime=1;
        for(i=2; i<=sqrt(p);i++)
            if(p % i ==0){
                prime=0;
                break;
            }
    }
    return prime;
}
int PrimeSum( int m, int n ){
	int Sum = 0;
	for (int i = m; i <= n; i++)if (prime(i))Sum = Sum + i;
	return Sum;
}

习题5-5 使用函数统计指定数字的个数 (15 分)

int CountDigit( int number, int digit ){
	if(number<0)
	    number =-number;
    if(number==0){
        if(digit==0) return 1;
        else return 0;
    }
	int r,count=0;
	while(number){
        r=number%10;
	    if(r==digit)
	        count++;
	    number/=10;
	}
	return count;
}

习题5-6 使用函数输出水仙花数 (20 分)

int power(int a,int b){
    int pow=1,i;
    for(i=0;i<b;i++)
        pow*=a;
    return pow;
}
int narcissistic( int number ){
    int a[5],i=0,N,sum=0,k;
    k=number;
    while(number){
        a[i]=number%10;
        number/=10;
        i++;
    }
    N=i;
    while(i>0){
        sum+=power(a[i-1],N);
        i--;
    }
    if(sum==k)return 1;
    else return 0;
}
void PrintN(int m,int n){
    int i;
    for(i=m+1;i<n;i++)
        if(narcissistic(i))
            printf("%d\n",i);
}

习题5-7 使用函数求余弦函数的近似值 (15 分)

double funcos(double e,double x){
	int flag=-1;
	double item=1,i=2,funcos=1;
	double denominator=1,numerator=1;
	while(item>e){
		denominator*=(i-1)*i;
		numerator*=x*x;
		item=numerator/denominator;
		funcos+=flag*item;
		i=i+2;
		flag=-flag;
	}
	return funcos;
}

习题5-8 空心的数字金字塔 (15 分)

void hollowPyramid ( int n ){
	int i,j;
	for(i=1;i<n;i++){
		for(j=n;j>i;j--)
            printf(" ");
		printf("%d",i);
		if(i!=1){
		    for(j=1;j<=1+2*(i-2);j++)
                printf(" ");
		    printf("%d",i);
        }
		printf("\n");
	}
	for(i=1;i<2*n;i++)printf("%d",n);
}

习题6-1 分类统计字符个数 (15 分)

void StringCount( char s[] ){
    int i,letter=0,blank=0,digit=0,other=0;
    for(i=0;s[i]!='\0';i++){
        if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')letter++;
        else if(s[i]==' '||s[i]=='\n')blank++;
        else if(s[i]>='0'&&s[i]<='9')digit++;
        else other++;
    }
    printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit,other);
}

习题6-2 使用函数求特殊a串数列和 (20 分)

int fn( int a, int n ){
	int i,fn=a;
	for(i=1;i<n;i++)
        fn=fn*10+a;
	return fn;
}
int SumA( int a, int n ){
	int i,sum=0;
	for(i=1;i<=n;i++)
        sum+=fn(a,i);
    return sum;
}

习题6-3 使用函数输出指定范围内的完数 (20 分)

int factorsum( int number ){
    int i,factorsum=0;
    for(i=1;i<number;i++)
        if(number%i==0)
            factorsum+=i;
    return factorsum;
}
void PrintPN(int m,int n){
    int i,j,flag=0;
    for(i=m;i<=n;i++){
        if(i==factorsum(i)){
            printf("%d = 1",i);
            for(j=2;j<i;j++)
                if(i%j==0)printf(" + %d",j);
            printf("\n");
            flag=1;
        }
    }
    if(flag==0)printf("No perfect number");
}

习题6-4 使用函数输出指定范围内的Fibonacci数 (20 分)

int fib( int n ){
    int A1=1,A2=1,An,i;
    if(n<3)An=1;
    else{
        for(i=3;i<=n;i++){
            An=A1+A2;
            A1=A2;
            A2=An;
        }
    }
    return An;
}
void PrintFN( int m, int n ){
    int i=0,f=0,count=0;
    while(f<=n){
        i++;
        f=fib(i);
        if(f>=m&&f<=n){
            if(count==0){
                printf("%d",f);
                count++;
            }else{
                printf(" %d",f);
                count++;  
            }
        }
    }
    if(count==0)printf("No Fibonacci number");
}

习题6-5 使用函数验证哥德巴赫猜想 (20 分)

int prime( int p ){
    int prime,i;
    if(p==1)prime=0;
    else if(p==2)prime=1;
    else{
        for(i=2;i<=sqrt(p);i++)
            if(p%i==0){
                prime=0;
                break;
            }
    }
    return prime;
}
void Goldbach( int n ){
    int p,q;
    for(p=2;p<n;p++){
        if(prime(p)){
            q=n-p;
            if(prime(q)){
                printf("%d=%d+%d",n,p,q);
                break;
            }
        }
    }
}

习题6-6 使用函数输出一个整数的逆序数 (20 分)

int reverse( int number ){
    int i, j, flag = 0, reverse = 0;
    if(number<0){
        flag = 1;
        number = -number;
    }
    while(number>0){
        i = number%10;
        reverse = reverse*10+i;
        number = number/10;
    }
    if(flag)reverse=-reverse;
    return reverse;
}

练习8-2 计算两数的和与差 (10 分)

void sum_diff( float op1, float op2, float *psum, float *pdiff ){
    *psum = op1+op2;
    *pdiff = op1-op2;
}

练习8-8 移动字母 (10 分)

void Shift( char s[] ){
    char a[3];
    int i,n;
    for(i=0;i<3;i++)a[i]=s[i];
    n=strlen(s);
    for(i=3;i<n;i++)s[i-3]=s[i];
    s[n-3]=a[0];
    s[n-2]=a[1];
    s[n-1]=a[2];
}

习题8-1 拆分实数的整数与小数部分 (15 分)

void splitfloat( float x, int *intpart, float *fracpart ){
    *intpart=(int)x;
    *fracpart=x-*intpart;
}

习题8-2 在数组中查找指定元素 (15 分)

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

习题8-3 数组循环右移 (20 分)

void ArrayShift( int a[], int n, int m ){
    m=m%n;
    int i,b[n];
    for(i=0;i<=m-1;i++)b[i]=a[n-m+i]; 
    for(i=m;i<=n-1;i++)b[i]=a[i-m];
    for(i=0;i<n;i++)a[i]=b[i];
}

习题8-4 报数 (20 分)

void CountOff(int n,int m,int out[]){
    int count=0,cnt=1,i;
    for (i=0;i<n;i++)out[i]=0;
    while (cnt<=n){
        for (i=0;i<n;i++){
            if (!out[i])count++;
            if (count==m){
                out[i]=cnt;
                cnt++;
                count = 0;
            }
        }
    }
}

习题8-5 使用函数实现字符串部分复制 (20 分)

void strmcpy( char *t, int m, char *s ){
    int i, j;
    for (i=0; t[i]!='\0'; i++);
    if (m>i) s[0] = '\0';
    else {
        i = 0; j = m-1;
        while (t[j]!='\0')
            s[i++] = t[j++];
        s[i] = '\0';
    }
}

习题8-6 删除字符 (20 分)

void delchar( char *str, char c ){
    int i=0,j=0;
    while(str[i]!='\0'){
        if(str[i]!=c){
            str[j]=str[i];
            j++;
        }
        i++;
    }
    str[j]='\0';
}

习题8-8 判断回文字符串 (20 分)

bool palindrome( char *s ){
    int len = strlen(s);
    for(int i = 0;i <=len/2;i++)
        if(s[i] != s[len-i-1]) return false;
    return true;
}

习题8-9 分类统计各类字符个数 (15 分)

void StringCount( char *s ){
    int digit=0,A_letter=0,a_letter=0,blank=0,other=0;
    int i=0;
    for(i=0;s[i]!='\0';i++){
        if(s[i]>='A'&&s[i]<='Z')
            A_letter++;
        else if(s[i]>='a'&&s[i]<='z')
            a_letter++;
        else if(s[i]>='0'&&s[i]<='9')
            digit++;
        else if(s[i]==' ')
            blank++;
        else other++;
    }
    printf("%d %d %d %d %d",A_letter,a_letter,blank,digit,other);
}

习题9-2 计算两个复数之积 (15 分)

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

习题9-6 按等级统计学生成绩 (20 分)

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

练习10-1 使用递归函数计算1到n之和 (10 分)

int sum( int n ){
    int Sum=0;
    if(n<=0)return 0;
    if(n==1)return 1;
    else return Sum=n+sum(n-1);
}

习题10-1 判断满足条件的三位数 (15 分)

int search(int n){
    int i,j,cnt = 0,a,b,c;
    for(i = 101;i<n;i++){
        a=i%10;
        b=i/10%10;
        c=i/100;
        if(sqrt(i)==(int)sqrt(i))
            if(a==b||b==c||a==c)
                cnt++;
    }
    return cnt;
}

习题10-2 递归求阶乘和 (15 分)

double fact( int n ){
    if(n==1||n==0)return 1;
    else return n*fact(n-1);
}
double factsum( int n ){
    int sum=0;
    for(int i=1;i<=n;i++)
        sum+=fact(i);
    return sum;
}

习题10-3 递归实现指数函数 (15 分)

double calc_pow( double x, int n ){
    if(n==0)return 1;
    else return x*calc_pow(x,n-1);
}

习题10-4 递归求简单交错幂级数的部分和 (15 分)

double fn(double x, int n){
    if (n == 1)return x;
    else return fn(x,n-1)*(-x)+x;
}

习题10-5 递归计算Ackermenn函数 (15 分)

int Ack( int m, int n ){
    if(m==0)return n+1;
    else if(m>0&&n==0)return Ack(m-1,1);
    else if(m>0&&n>0)return Ack(m-1,Ack(m,n-1));
}

习题10-6 递归求Fabonacci数列 (10 分)

int f( int n ){
    if(n==0)return 0;
    else if(n==1)return 1;
    else return f(n-1)+f(n-2);
}

习题10-7 十进制转换二进制 (15 分)

void dectobin(int n){
    int re=0;
    if(n==0)printf("0");
    else if(n==1)printf("1");
    else{
        dectobin(n/2);
        re=n%2;
        printf("%d",re);
    }
}

习题10-8 递归实现顺序输出整数 (15 分)

void printdigits(int n){
    if (n >= 10)
        printdigits(n / 10);
    printf("%d\n", n % 10);
}

习题10-11 有序表的增删改查操作 (20 分)

int insert(int a[ ], int value) {
    int i, index, j;
    index = query(a, value);
    if(index != -1)return -1;
    else{
        for (i = 0; i < Count; i++)
            if(value < a[i])
                break;
        for(j = Count -1; j >= i; j--)
            a[j+1] = a[j];
        a[i] = value;
        Count++;
    }    
    return 0;
}


int del(int a[ ], int value) {
    int i, index; 
    index = query(a, value);
    if(index == -1)return -1;
    else{
        for(i = index; i < Count - 1; i++)
            a[i] = a[i+1];
        Count --;
    }    
    return 0;
}

int modify(int a[ ], int value1, int value2){
    int index;
    index = query(a, value1);
    if(index == -1 || query(a, value2) != -1)
        return -1;
    else{
        del(a, value1);
        insert(a, value2);
    }
    return 0;
}

int query(int a[ ], int value) {
    int mid, left = 0, right = Count - 1;
    while (left <= right){
        mid = (left + right) / 2;
        if (value == a[mid])return mid;
        else if (value < a[mid])right = mid - 1;
        else left = mid + 1;
    }    
    return -1;
}

练习11-4 字符定位(最后一次找到的字符) (15 分)

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

习题11-1 输出月份英文名 (15 分)

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

习题11-2 查找星期 (15 分)

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

习题11-3 计算最长的字符串长度 (15 分)

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

习题11-4 字符串的连接 (15 分)

char *str_cat( char *s, char *t ){
    strcpy(s+strlen(s) , t);
    return s;
}

习题11-5 指定位置输出字符串 (20 分)

char *match( char *s, char ch1, char ch2 ){
    char *p;
    int i=0;
    p=NULL;
    while(s[i]!=ch1&&s[i]!='\0')i++;
    p=&s[i];
    while(s[i]!=ch2&&s[i]!='\0'){
        printf("%c",s[i]);
        i++;
    }
    if(s[i]==ch2)printf("%c",s[i]);
    printf("\n");
    return p;
}

习题11-6 查找子串 (20 分)

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

习题11-7 奇数值结点链表 (20 分)

struct ListNode *readlist() {
	struct ListNode *head = NULL;
	struct ListNode *last = head;
	struct ListNode *p;
	int number;
	do {
		scanf("%d", &number);
		if (number != -1) {
			p = (struct ListNode *)malloc(sizeof(struct ListNode));
			p->data = number;
			p->next = NULL;
			if (last) {
				last->next = p;
                last = last->next;
			} else {
				head = p;
				last = p;
			}
		}
	} while (number != -1);
	return head;
}
struct ListNode *getodd( struct ListNode **L ){
	struct ListNode *p,*head1,*last1,*head2,*last2;
    p=*L;
    head1=NULL;
    last1=NULL;
    head2=NULL;
    last2=NULL;
    while(p!=NULL){
        if(p->data%2==0){
            if (last2) {
				last2->next = p;
				last2 = last2->next;
			} else {
				head2 = p;
				last2 = p;
				*L=p;
			}
        }else{
            if (last1) {
				last1->next = p;
				last1 = last1->next;
			} else {
				head1 = p;
				last1 = p;
			}
        }
        p=p->next;
    }
    if(last1)last1->next=NULL;
    if(last2)last2->next=NULL;
    else *L=NULL;
    return head1;
}

习题11-8 单链表结点删除 (20 分)

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

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐