浙大版《C语言程序设计(第4版)》题目集(函数题)已完结
1.练习5-1 求m到n之和
int sum(int m, int n){ int summary; for(summary=0;m<=n;m++) { summary+=m; } return(summary);}
2.练习5-2找两个数中最大者
int max(int a,int b){ int bigger; if(a>=b) bigger=a; else bigger=b; return(bigger);}
3.练习5-3 字符金字塔
void CharPyramid( int n, char ch){int i, j;for(i=1; i<=n; i++){for(j=1; j<=(n-i); j++){printf(" ");}for(j=1; j<=i; j++){printf("%c ", ch);}printf("\n");}}
4.习题5-1 符号函数
int sign( int x ){ if(x>0) return 1; else if(x==0) return 0; else return -1;}
5.习题5-2 使用函数求奇数和
int even( int n ){ if(n%2==0) return 1; else return 0;}int OddSum( int List[], int N ){ int sum=0; int i; for(i=0;i<N;i++) { if (even(List[i])==0) sum=sum+List[i]; } return sum;}
6.习题5-3 使用函数计算两点间的距离
double sqrt(double x);double dist( double x1, double y1, double x2, double y2 ){ double distance; double power; power=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); distance=sqrt(power); return distance;}
7.习题5-4 使用函数求素数和
int prime( int p )// judge if it is a prime{ int i; if(p>1) { for(i=2;i<p;i++) { if(p%i==0) break; } if(i<p) return 0; else return 1; } else return 0;}int PrimeSum( int m, int n ){ int sum=0; int p; for(p=m;p<=n;p++) { if( prime(p)!= 0 ) sum=sum+p; } return sum;}
8.习题5-5 使用函数统计指定数字的个数
int CountDigit( int number, int digit ){int count=0;if(number==0&&digit==0) count=1;if(number<0){number=-number;}while(number>0){if(number%10==digit) count++;number/=10;}return count; }
9.习题5-6 使用函数输出水仙花数
int narcissistic( int number ){int res = 0, t;if (number <= 999){int g = 0,s = 0,b = 0;g = number % 10;s = number % 100 / 10;b = number / 100;t = g * g * g + s * s * s + b * b * b;if (t == number){res = 1;}}else if (number <= 9999){int g = 0,s = 0,b = 0,q = 0;g = number % 10;s = number / 10 % 10;b = number / 100 % 10;q = number / 1000;t = g * g * g * g + s * s * s * s + b * b * b * b + q * q * q * q;if (t == number){res = 1;}}return res;}void PrintN( int m, int n ){for (int i = m + 1; i < n;i++){if (narcissistic(i)){printf("%d\n", i);}}}
10.习题5-7 使用函数求余弦函数的近似值
double funcos( double e, double x ){int flag = 1,n = 0;double fact = 1,temp = 0.0,res = 0.0;while (1){if (n == 0){fact = 1;}else {fact *= n * (n - 1);}temp = flag *pow(x,n) / fact;res += temp;if (fabs(temp) < e){break;} n += 2;flag = -flag;}return res;}
11.习题5-8 空心的数字金字塔
void hollowPyramid(int n){int i, row=1; //row记录行数for( ; row<=n; row++ ){for( i=1; i<=n-row; i++ ){printf(" ");} //输出数字前空格,共n-row个if(row==1)printf("1"); //第一行只有1个数字,单独输出else if(row>1&&row<n){printf("%d", row); //输出中间某行第一个数字for(i=1; i<=2*row-3; i++){printf(" ");} //输出两数字之间空格,共2row-3项printf("%d", row); //输出中间某行第二个数字}else if(row==n){for(i=1; i<=2*row-1; i++){printf("%d", row);} //输出第n行数字}printf("\n");}}
12.习题6-1 分类统计字符个数
void StringCount( char s[] ){ int letter=0,blank=0,digit=0,other=0; for(int 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);}
13.习题6-2 使用函数求特殊a串数列和
int fn( int a, int n ){ int b=a; for(int i=1;i<n;i++) a=10*a+b; return a;}int SumA( int a, int n ){ int s=0; for(int i=1;i<=n;i++) s=s+fn(a,i); return s;}
14.习题6-3 使用函数输出指定范围内的完数
int factorsum( int number ){ int s=1,a=number/2; if(number==1) return 0; for(int i=2;i<=a;i++) { if(number%i==0) { s=s+i; } } return s;}void PrintPN( int m, int n ){ int s=1,k=0; for(m;m<=n;m++) { int a=m/2; if(factorsum(m)==m) { k=1; printf("%d = 1",m); for(int i = 2;i<=a;i++) { if(m%i==0) printf(" + %d",i); } printf("\n"); } } if(k==0) printf("No perfect number");}
15.习题6-4 使用函数输出指定范围内的Fibonacci数
int fib( int n ){ int a=1,b=1,c=0; if(n==1||n==2) return 1; else { for(int i=0;i<n-2;i++) { c=a+b; a=b; b=c; } return c; }}void PrintFN( int m, int n ){ int i=1,k=0,a=0; while(fib(i)<m) i++; while(fib(i)<=n) { k=1; if(a==0) { printf("%d",fib(i)); a=1; } else printf(" %d",fib(i)); i++; } if(k==0) printf("No Fibonacci number");}
16.习题6-5 使用函数验证哥德巴赫猜想
int prime( int p ){ if(p<=1) return 0; else if(p==2) return 1; else { for(int i=2;i<=p/2;i++) { if(p%i==0) return 0; } return 1; }}void Goldbach( int n ){ for(int i=1;i<n;i++) { if(prime(i)==1&&prime(n-i)==1) { printf("%d=%d+%d",n,i,n-i); break; } }}
17.习题6-6 使用函数输出一个整数的逆序数
int reverse( int number ){ int a=1,s=0,b,c=1; if(number<0) { number=-number; c=-1; } while(number>0) { b=number%10; s=s*10+b; number=number/10; } return s*c;}
18.练习8-2 计算两数的和与差
void sum_diff( float op1, float op2, float *psum, float *pdiff ){ *psum=op1+op2; *pdiff=op1-op2;}
19.练习8-8 移动字母
void Shift( char s[] ){ int i,j; for(i=0;i<3;i++) { char b=s[0]; for(j=0;s[j+1]!='\0';j++) s[j]=s[j+1]; s[j]=b; }}
20.习题8-1 拆分实数的整数与小数部分
void splitfloat( float x, int *intpart, float *fracpart ){ *intpart=(int)x; *fracpart=x-*intpart;}
21.习题8-2 在数组中查找指定元素
int search( int list[], int n, int x ){ for(int i=0;i<n;i++) { if(list[i]==x) return i; } return -1;}
22.习题8-3 数组循环右移
int ArrayShift( int a[], int n, int m ){ m=m%n; int b[n]; for(int i=0;i<n;i++) b[i]=a[i]; for(int i=0;i<n;i++) { if(i>=n-m) a[i+m-n]=b[i]; else a[i+m]=b[i]; }}
23.习题8-4 报数
void CountOff( int n, int m, int out[] ){ int a[MAXN],b=0,c=1,d=n; for(int i=0;i<n;i++) a[i]=1; while(d!=0) { for(int i=0;i<n;i++) { if(a[i]!=0) { b++; while(b==m) { a[i]=0; out[i]=c; c++; d--; b=0; } } } }}
24.习题8-5 使用函数实现字符串部分复制
#include<string.h> void strmcpy( char *t, int m, char *s ) { int n=strlen(t); if(n<m) return; else { for(int i=m-1;i<n;i++) { *s=*(t+i); s++; } *s='\0'; return s; } }
25.习题8-6 删除字符
#include<string.h>void delchar( char *str, char c ){ int n=strlen(str),j; for(int i=n;i>=0;i--) { if(str[i]==c) { for( j=i;j<n-1;j++) str[j]=str[j+1]; str[j]='\0'; } }}
26.习题8-8 判断回文字符串
#include<string.h>bool palindrome( char *s ){ int n=strlen(s); for(int i=0;i<n/2;i++) { while(s[i]!=s[n-1-i]) return false; } return true;}
27.习题8-9 分类统计各类字符个数
#include<string.h>void StringCount( char *s ){ int a=0,b=0,c=0,d=0,e=0; int n=strlen(s); for(int i=0;i<n;i++) { if(s[i]>='A'&&s[i]<='Z') a++; else if(s[i]>='a'&&s[i]<='z') b++; else if(s[i]==' ') c++; else if(s[i]>='0'&&s[i]<='9') d++; else e++; } printf("%d %d %d %d %d",a,b,c,d,e);}
28.习题9-2 计算两个复数之积
struct complex multiply(struct complex x, struct complex y){ struct complex protect; protect.real=x.real*y.real-x.imag*y.imag; protect.imag=x.imag*y.real+x.real*y.imag; return protect;}
29.习题9-6 按等级统计学生成绩
int set_grade( struct student *p, int n ){ int a=0; for(int i=0;i<n;i++) { if((*p).score>=85) (*p).grade='A'; if((*p).score<=84&&(*p).score>=70) (*p).grade='B'; if((*p).score<=69&&(*p).score>=60) (*p).grade='C'; if((*p).score<=59) { (*p).grade='D'; a++; } p++; } return a;}
30.练习10-1 使用递归函数计算1到n之和
int sum(int n){ int i=0,s=0; if(n<=0) return 0; else { while(i<n) { i++; s=s+i; } return s; } }
31.习题10-1 判断满足条件的三位数
int search( int n ){ int count=0,i=11,a,b,c,s; while(i*i<=n) { s=i*i; a=s%10; b=((s-a)/10)%10; c=s/100; if(a==b||a==c||b==c) count++; i++; } return count;}
32.习题10-2 递归求阶乘和
double fact( int n ){ int s; if(n==0||n==1) return 1; else { s=n*fact(n-1); return s; }}double factsum( int n ){ int s; if(n==0) return 0; else { s=fact(n)+factsum(n-1); return s; }}
33.习题10-3 递归实现指数函数
double calc_pow( double x, int n ){ double s; if(n==1) return x; else s=x*calc_pow(x,n-1); return s;}
34.习题10-4 递归求简单交错幂级数的部分和
double fn( double x, int n ){ double s; if(n==1) s=x; else s=pow(-1,n-1)*pow(x,n)+fn(x,n-1); return s;}
35.习题10-5 递归计算Ackermenn函数
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(m>0&&n>0) return Ack(m-1,Ack(m,n-1));}
36.习题10-6 递归求Fabonacci数列
int f( int n ){ if(n==0) return 0; else if(n==1) return 1; else return f(n-2)+f(n-1);}
37.习题10-7 十进制转换二进制
void dectobin( int n ){ if(n==0) printf("0"); else if(n==1) printf("1"); else { dectobin(n/2); printf("%d",n%2); }}
38.习题10-8 递归实现顺序输出整数
void printdigits( int n ){ if(n<10) printf("%d\n",n); else { printdigits(n/10); printf("%d\n",n%10); }}
39.习题10-11 有序表的增删改查操作
int insert(int a[], int value){ int i; for (i = 0; i < Count; i++) { if (value < a[i]) break; if (value == a[i]) return -1; } for (int j = Count; j > i; j--) a[j] = a[j - 1]; a[i] = value; Count++; return 0;} int del(int a[], int value){ int i; for (i = 0; i < Count; i++) { if (value == a[i]) break; } if (i == Count) return -1; for (int j = i; j < Count - 1; j++) a[j] = a[j + 1]; Count--; return 0;} int modify(int a[], int value1, int value2){ if (del(a, value1) == -1) return -1; if (insert(a, value2) == -1) return -1; return 0;} int query(int a[], int value){ int mid, left = 0, right = Count - 1; while (left <= right) { mid = (left + right) / 2; if (a[mid] < value) left = mid + 1; else if (a[mid] > value) right = mid - 1; else return mid; } return -1;}
40.练习11-4 字符定位(最后一次找到的字符)
char *match(char *s, char ch){char *ps = NULL;while(*s!='\0'){if(*s==ch){ps = s;}s++;}return ps;}
41.习题11-1 输出月份英文名
char *getmonth( int n ){ switch(n) {case 1:return "January" ;break;case 2:return "February" ;break;case 3:return "March" ;break;case 4:return "April" ;break;case 5:return "May" ;break;case 6:return "June" ;break;case 7:return "July" ;break;case 8:return "August" ;break;case 9:return "September" ;break;case 10:return "October" ;break;case 11:return "November" ;break;case 12:return "December" ;break;} return 0 ; }
42.习题11-2 查找星期
int getindex(char *s){ int week; char *day[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; for(week=0;week<=6;week++) { if(strcmp(s,day[week])==0) break; } if(week==7) week=-1; return week;}
43.习题11-3 计算最长的字符串长度
int max_len( char *s[], int n ){ int m,max=0; for(int i=0;i<n;i++) { m=strlen(s[i]); if(m>max) max=m; } return max;}
44.习题11-4 字符串的连接
char *str_cat( char *s, char *t ){ char *p=s; while(*p) p++; while(*t) { *p=*t; p++; t++; } return s;}
45.习题11-5 指定位置输出字符串
char *match(char *s,char ch1,char ch2){ char a[MAXS]="",*p; int i=0,j=0; while((*(s+i)!=ch1)&&(*(s+i)!='\0')) { i++; } p=s+i; a[j]=*(s+i); j++; i++; while((*(s+i)!='\0')&&(*(s+i)!=ch2)) { a[j]=*(s+i); j++; i++; } a[j]=*(s+i); printf("%s\n",a); return p;}
46.习题11-6 查找子串
char *search(char *s, char *t){ char *p = NULL; int i,j=0,k=0,a,b; a=strlen(s); b=strlen(t); for(i=0;i<a;i++) { j=i; while(s[j]==t[k]) { j++; k++; } while (k >= b) { p = &s[i]; return p; } k=0; } return p;}
47.习题11-7 奇数值结点链表
//新建链表,依次读取输入的数据,存放在链表的节点中//尾插法建链表struct ListNode *readlist(){ struct ListNode *head, *tail, *pnew; //头节点,尾节点,新节点 head = NULL; tail = NULL; pnew = NULL; int num; while(scanf("%d", &num), num!=-1) { pnew = (struct ListNode*)calloc(1,sizeof(struct ListNode)); //calloc函数新建节点的同时可以实现初始化,建议使用calloc pnew->data = num; if(head == NULL) { head = pnew; tail = pnew; } else{ tail->next = pnew; tail = pnew; } } return head;} //建立奇数链表struct ListNode *getodd( struct ListNode **L ){ struct ListNode *head, *tail, *pnew, *pcur, *ppre, *pfree; head = NULL; tail = NULL; pnew = NULL; pcur = *L; ppre = *L; if(*L == NULL) return NULL; while(pcur) { if(pcur->data%2) { pfree = pcur; pnew = (struct ListNode*)calloc(1,sizeof(struct ListNode)); pnew->data = pcur->data; if(head == NULL) { head = pnew; tail = pnew; } else{ tail->next = pnew; tail = pnew; } if(((*L)->data)%2) // 判断头节点是奇数的情况 { *L=pcur->next; pcur = *L; ppre = *L; } else { ppre->next = pcur->next; pcur = ppre->next; } free(pfree); } else{ ppre = pcur; pcur = pcur->next; } } return head;}
48.习题11-8 单链表结点删除
struct ListNode *readlist(){ struct ListNode *p,*head=NULL; int data; while(scanf("%d",&data)&&data!=-1) { struct ListNode *q=(struct ListNode*)malloc(sizeof(struct ListNode)); if(q!=NULL) { q->data = data; q->next = NULL; } else exit(1); if(head!=NULL) { p->next=q; } else head=q; p=q; } return head;}struct ListNode *deletem( struct ListNode *L, int m ){ struct ListNode *q,*p; p=L; while(p!=NULL) { if(p->data==m) { if(p==L) L = p->next; else q->next = p->next; } else { q=p; } p=p->next; } return L;}