




已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验5三种基本结构的综合应用4一个素数(设为p)依次从最高位去掉一位,二位,三位,若得到的各数仍都是素数(注:除1和它本身外,不能被其它整数整除的正整数称为素数,1不是素数,2是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求100,999之内的所有逆向超级素数的个数。#include stdio.hmain()int i,j,k,m,p,q,n=0;for(i=100;i=999;i+)for(j=2;j=i) /*三位数是素数时*/ k=i%100; /*去掉百位数字*/ if(k=10) /*十位数字不是0时*/ for(m=2;m=k) /*两位数是素数时*/ p=i%10; /*p为个位数字*/for(q=2;q=p)n+;printf(%dn,n);Key:575求2,400中相差为10的相邻素数对的对数。#include stdio.hmain()int i,j,k,m,p,q,n=0;for(i=2;i=400;i+)for(j=2;j=i) /*i是素数时*/ for(k=i+1;ki+10;k+) for(m=2;m=k)break; /*k是素数时终止if语句的外层循环*/ if(k=i+10) /*i+1,i+9不是素数时*/ for(q=2;q=k) /*i+10是素数时*/n+;printf(%dn,n);Key:56.求1,21范围内的梅森尼数 #include math.h main() long n,k=2,m,p,q; for(n=2;n=21;n+) k=k*2; p=k-1; m=sqrt(p); for(q=2;qm) printf(%5ld,n);Key: 2 3 5 7 13 17 197.求1000以内的亲密数对 main() int a,b,n,m,q,p; for(a=1;a1000;a+) n=0; for(q=1;q=a/2;q+) if(a%q=0) n+=q; for(b=a;b=1000;b+) m=0; for(p=1;p=b/2;p+) if(b%p=0) m+=p; if(a=m&b=n) printf(%5d%5d,a,b);/*Key:6 6 28 28 220 284 496 496*/实验6 数组及其应用3由N个有序整数组成的数列已放在一维数组中,下列给定程序中函数fun的功能是:利用折半查找法查找整数m在数组中的位置。若找到,返回其下标值;否则,返回1。折半查找的基本算法是:每次查找前先确定数组中待查的范围low和high(lowhigh,查找结束。请改正程序中的错误,使它能得出正确的结果。#include #define N 10main() int i,aN=-3,4,7,9,13,45,67,89,100,180 ,k=-1,m; int low=0,high=N-1,mid; printf(a数组中的数据如下:); for(i=0;iN;i+) printf(%d , ai); printf(Enter m: ); scanf(%d,&m); while(low=high) mid=(low+high)/2; if(m amid) low=mid+1;/*found*/ else k=mid;continue; if(k=0) printf(m=%d,index=%dn,m,k); else printf(Not be found!n);Key:第一个错误:else If(m amid) 应改为:else if(m amid) 因C语言关键字必须小写。 第二个错误:else k=mid;continue; 应改为:else k=mid;break; 因找到就可终止循环。4有一个3*4矩阵,找出每行中最大的元素并与第1列元素交换。具体要求如下:(1)使用二维数组存放该3*4矩阵。(2)定义并初始化该二维数组。(3)输出原矩阵和处理后的矩阵进行比较。【程序源代码】#include stdio.hmain() int i,j,k,temp; int data34=12,15,7,11,14,6,10,21,25,7,16,45; printf(the original matrix:n); for(i=0;i=2;i+) for(j=0;j=3;j+) printf(%dt,dataij); printf(n); /*打印原矩阵*/ for(i=0;i=2;i+) k=0; for(j=1;jdataik) k=j; temp=datai0; datai0=dataik; dataik=temp; /*求每行最大数并与第1列上的元素交换*/ printf(nthe handled matrix:n); for(i=0;i=2;i+) for(j=0;j=3;j+) printf(%dt,dataij); printf(n); /*打印处理后矩阵,此时第1列上的元素为每行的最大数*/5猴子选大王问题:n个人围坐一圈,并顺序编号1n,从1号开始数,每数到m个就让其出局,重复.。求最后出局者的编号。当n=50,m=3时 ,答案为11具体要求如下:(1)使用一维数组存放每个人的编号,每数到m个数就让其出局,出局者编号为0。(2)使用for循环嵌套实现。【程序源代码】#include stdio.hmain()int a51,i,j,m=0,n; for(i=1;i=50;i+)ai=i;/*为了符合习惯,数组下标从1开始,下标就是编号*/ for(j=1;j=50/3;j+) /*外层循环最多循环50/3次*/ for(i=1;i=50;i+) if(ai!=0)m+; /*对未出局者计数*/if(m=3)ai=0,n=i,m=0;/*对出局者编号为0,并保存原编号,计数从0开始*/ printf(last: %dn,n); /*最后出局者的编号*/实验7函数及其应用3排错题:下列给定程序中函数fun 的功能是:用递归算法求形参a 的平方根。请改正程序中的错误,使它能计算出正确的结果。注意:源程序不得增行或删行,也不得更改程序的结构!例如,a 为2 时,平方根值为:1.414214。#include #include /*found*/void fun(double a, double x0) double x1, y;x1=(x0+a/x0)/2.0;/*found*/if( fabs(x1-x0)=0.00001 )y=fun(a,x1);else y=x1;return y;main( ) double x;printf(Enter x: ); scanf(%lf,&x);printf(The square root of %lf is %lfn,x,fun(x,1.0);Key:第一个错误:void fun(double a, double x0) 应改为:double fun(double a, double x0) 因根据题意,需通过调用fun()函数返回a的平方根,故必须将void改为double,否则不能用带值的return语句。自增。 第二个错误:if( fabs(x1-x0)=0.00001 ) 因只有 fabs(x1-x0)=0.00001才须递归。4设计一个程序,判断一个整数n 是否是素数。具体要求如下:(1)编制一个函数int prime(number),判断整数number 是否是素数。(2)编制主函数,由键盘输入整数number,调用(1)中的函数,若返回值为真则是素数,否则不是素数。(3)分别用以下数据运行该程序:103,117。【程序源代码】#include stdio.hmain() int number; printf(请输入一个正整数:n);scanf( %d,&number);if (prime(number)printf(n %d是素数. ,number);elseprintf(n %d不是素数. ,number);int prime(number)int number;int flag=1,n;for(n=2;nnumber/2&flag=1;n+)if (number%n=0)flag=0;return(flag);5编写程序,通过两个自定义函数分别求两个正数的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果。两个正数由键盘输入。具体要求如下:(1)用辗转相除法实现。(2)输出要有文字说明,输出形式为:最大公约数具体值,最小公倍数具体值(3)分别用以下数据运行该程序: 8 和26, 64 和258(4) 辗转相除法:对于两个数m 和n,将大数放在m 中,小数放在n 中,用n 去除m,若余数为0,则n 为最大公约数,否则将n 作为m,余数作为n,再用n 去除m,直到余数为0,则n 为最大公约数。【程序源代码】#include stdio.hhcf(u,v)int u,v;int a,b,t,r;a=u; b=v;while(r=b%a)!=0)b=a; a=r;return(a);lcd(u,v,h)int u,v,h;return(u*v/h);main()int u,v,h,l;scanf(%d,%d,&u,&v);h=hcf(u,v);printf(H.C.F=%dn,h);l=lcd(u,v,h);printf(L.C.D=%dn,l);6请编写函数fun,函数的功能是:求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。例如,若二维数组中的值为1 3 5 7 92 9 9 9 46 9 9 9 81 3 5 7 0则函数值为61。【程序源代码】#include#define M 4#define N 5int fun( int a MN)int i,j,s=0; for(i=0;iM;i+) for(j=0;jN;j+) if(i=0|j=0|i=M-1|j=N-1) s+=aij; return s; void main() int aaMN=1,3,5,7,9,2,9,9,9,4,6,9,9,9,8,1,3,5,7,0; int i, j, y; printf (The original data is :n ); for(i=0; iM;i+) for (j=0; j%cn,getone,putone);void hanoi(int n,char one ,char two,char three)if(n=1) move(one,three);else hanoi(n-1,one,three,two);move(one ,three);hanoi(n-1,two,one,three);main()int n;printf(Input the number of disks: );scanf(%d,&n);printf(The step to moving %3d disks: n,n);hanoi(n,A,B,C);【程序改进源代码】#include stdio.hint n=0;void move(char getone ,char putone)n+;printf(step %d: %c%cn,n,getone,putone);void hanoi(int n,char one ,char two,char three)if(n=1) move(one,three);else hanoi(n-1,one,three,two);move(one ,three);hanoi(n-1,two,one,three);main()int n;printf(Input the number of disks: );scanf(%d,&n);printf(The step to moving %3d disks: n,n);hanoi(n,A,B,C);实验9 指针及其应用1以下函数调用Reverse()函数按逆序重新放置数组A 中的元素,请补全程序。#include stdio.h#define N 10void Reverse(int *p, int a, int b)int c;while (_)c=*(p+a);*(p+a)= _;*(p+b)=c;_;int main()int AN, i;for (i=0; iN; i+)scanf(%d, _);Reverse(A, 0, N-1);for (i=0; iN; i+)printf(%-4d, _);printf(n);return 0;Key:第一空应填:ab 第二空应填:*(p+b) 或 pb /*用指针方法或用下标方法*/ 第三空应填:a+,b+ 或+a/a+=1/a=a+1,+b/b+=1/b=b+1 第四空应填:&Ai 或 A+i /*用数组元素的地址或数组的首地址加偏移量的方法*/ 第五空应填:*(A+i) 或 Ai /*用指针方法或用下标方法*/2记录以下程序的运行结果,并进行分析。#include stdio.hint main()int a=10, b=0, *pa, *pb;pa=&a, pb=&b;printf(%d,%d,a,b);printf(%d,%dn,*pa,*pb);a=20, b=30;*pa=a+, *pb=b+;printf(%d,%d,a,b);printf(%d,%dn,*pa,*pb);(*pa)+;(*pb)+;printf(%d,%d,a,b);printf(%d,%dn,*pa,*pb);return 0;分析程序运行:_Key:运行结果:10,0,10,0 21,31,21,31 22,32,22,32 分析:因为pa=&a;pb+&b;所以*pa和a是同一个变量,占据内存同一存储单元。注意该程序与下面程序的区别:#include stdio.hint main()int a=10, b=0, c=a, d=b;printf(%d,%d,a,b);printf(%d,%dn,c,d);a=20, b=30;c=a+, d=b+;printf(%d,%d,a,b);printf(%d,%dn,c,d);c+;d+;printf(%d,%d,a,b);printf(%d,%dn,c,d);return 0;Key:运行结果:10,0,10,0 21,31,20,30 21,31,21,31 分析:因为c=a+;相当于c=a;a+;而a和c是两个不同的变量,所以c+不影响a的值。3设计一个程序,统计字符串中大写字母的个数。具体要求如下: (1)字符串的长度从键盘输入,并以此分配存储空间。(2) 使用指针变量遍历字符串。【程序源代码1:用数组存放字符串】#define N 80#includeint capital(char *p)int count=0; while(*p) if(*p=A&*p=Z) count+; p+;return count;main()char cN,n; printf(Input a string:); gets(c); printf(Output the string:); puts(c); n=capital(c); printf(There are %d uppercase alphas in %sn,n,c);【程序源代码2:用指针指向字符串(此法可能更合出题者的意图)】#include#includeint capital(char *p)int count=0; while(*p) if(*p=A&*pnum, pmax-name,pmax-score0, pmax-score1, pmax-score2, pmax-avg); /*输出最高分学生数据*/*子函数*/void input(struct student stu5)void output(struct student stu5)float average(struct student stu5) /*求3 门课总平均分,并作为函数值返回*/struct student *max(struct student stu5) /*查找最高分学生,函数返回最高分学生的地址*/Key: /*子函数*/void input(struct student stu5)int i,j; for(i=0;i5;i+) printf(Input No.%d student num(Enter to end):,i+1); scanf(%d,&stui.num); getchar(); printf(Input No.%d student name(Enter to end):,i+1); gets(); for(j=0;j3;j+) printf(Input No.%d student score%d(Enter to end):,i+1,j); scanf(%f,&stui.scorej);void output(struct student stu5)int i,j; printf( numt namet cj1t cj2t cj3n); for(i=0;i5;i+) printf(%dt%st,stui.num,); for(j=0;j3;j+) printf(%.2ft,stui.scorej); printf(n);float average(struct student stu5) /*求3 门课总平均分,并作为函数值返回*/int i,j; float aver=0; for(i=0;i5;i+) stui.avg=0; for(j=0;j3;j+) stui.avg+=stui.scorej/3; aver+=stui.avg/5; return aver;struct student *max(struct student stu5) /*查找最高分学生,函数返回最高分学生的地址*/struct student *p=stu,*q; for(q=&stu1;qavgavg)p=q; return p;(2)建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。要求:用一个create 函数创建链表,输入若干学生的信息存入链表中,以学号为0 作为结束标志;用一个output 函数输出链表中存放的学生信息;用一个delete 函数删除链表中其年龄等于输入年龄的结点。【程序框架】#includestdio.h#includestdlib.hstruct node int num;char name20;char sex;int age;struct node *next; /*next 指向下一个结点*/; /*链表结点类型定义*/*函数声明*/struct node *create( );void output(struct node *head);struct node *delete(struct node *head, int age);/*主函数*/void main()struct node *head; int age;printf(create linklist:n);head=create(); /*创建链表*/printf(ouput linklist:n);output(head); /*输出链表*/printf(please input an age:);scanf(%d,&age);head=delete(head,age); /*删除链表中其年龄等于输入年龄age 的结点*/printf(output linklist after deleting:n);output(head); /*输出删除后的链表*/*子函数*/struct node *create( ) /*输入若干学生信息存入链表中,以学号为0 作为结束标志,从而创建一个链表,函数返回链表的头指针*/void output(struct node *head) /*输出head 所指链表中的学生信息*/struct node *delete(struct node *head, int age) /*删除head 所指链表中其年龄等于给定值age 的结点,函数返回删除后的链表头指针*/Key:/*子函数*/struct node *create( ) /*输入若干学生信息存入链表中,以学号为0 作为结束标志,从而创建一个链表,函数返回链表的头指针*/int num,age,n=1;char name20,sex; struct node *h=malloc(sizeof(struct node),*s,*r=h; printf(Input num of NO.%d student(0 to end):,n); scanf(%d,&num); while(num) getchar();printf(Input name of NO.%d student:,n); gets(name); printf(Input sex of NO.%d student(f or m):,n); sex=getchar(); printf(Input age of NO.%d student:,n); scanf(%d,&age); s=malloc(sizeof(struct node); s-num=num; strcpy(s-name,name); s-sex=sex; s-age=age; r=r-next=s;n+; printf(Input num of NO.%d student(0 to end):,n); scanf(%d,&num); r-next=0; return h; void output(struct node *head) /*输出head 所指链表中的学生信息*/struct node *p=head-next; if(p=0)printf(Linklist is null!n); else printf(head); while(p) printf(-num:%d,name:%s,sex:%c,age:%d,p-num,p-name,p-sex,p-age), p=p-next; printf(-endn);struct node *delete(struct node *head, int age) /*删除head 所指链表中其年龄等于给定值age 的结点,函数返回删除后的链表头指针*/struct node *q=head,*p=q-next; if(p=0)printf(Linklist is null,cannot deleted!); else while(p&p-age!=age) p=p-next,q=q-next; if(p-age=age)q-next=p-next,free(p); else printf(not found!n); return head; (3)从键盘输入一个类型为unsigned long 型的整数,分别将其前两个字节和后两个字节作为unsigned int 型数据输出。要求:使用共用体变量实现;有必要的提示信息;用以下数据测试:12345678。提示:typedef union u unsigned int byte2; /*分别存放unsigned long 型数据的高低两个字节的数据*/unsigned long word; /*存放unsigned long 型数据*/BYTE;【程序源代码:考虑兼容性,两个字节的整型定义用short,输出用%hd,长整型输入用%ld】#includetypedef union u unsigned short byte2; /*分别存放unsigned long 型数据的高低两个字节的数据*/unsigned long word; /*存放unsigned long 型数据*/BYTE;main()BYTE num; printf(Input a unsigned long int(for example:12345678):); scanf(%ld,&num.word); printf(Decimal:%ld=first/low two bytes:%hd,high/last two bytes:%hdn, num.word,num.byte0,num.byte1);/*十进制输出对照*/ printf(Hexidecimal:%lx=first/low two bytes:%hx,high/last two bytes:%hxn, num.word,num.byte0,num.byte1);/*十六进制输出对照*/实验11文件及其应用2 以下函数的功能是把文本文件 B 的内容追加到文本文件A 的内容之后。补全程序并验证。#include#define N 80main()FILE *fp,*fp1,*fp2;int i;char cN=a,s,d,f,g,t,ch;if( =NULL)printf(cannot open file An);exit(1);printf(file A:);for(i=0;i5;i+) putchar(ci);fclose(fp);if(fp=fopen(B.dat,r)=NULL)printf(cannot open file Bn);exit(1);printf(nfile B:);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci=ch;putchar(ci);fclose(fp);if(fp1=fopen(A.dat,a)&(fp2=fopen(B.dat,r)while(ch=fgetc(fp2)!=EOF)fputc(ch,fp1);elseprintf(cannot open A B!n);fclose(fp1);fclose(fp2);printf(n*New A*);if(fp=fopen(A.dat,r)=NULL)printf(cannot open file An);exit(1);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci=ch;putchar(ci);putchar(n);fclose(fp);Key:第一空应填:(fp=fopen(A.dat,w) 第二空应填:putc(ci,fp); 第三空应填:& 第四空应填:fputc(ch,fp1); 注意:若此程序运行前,先用C+集成开发环境新建文本文件b.dat,其内容为qwerty。则每次运行结果为:3程序设计 假设有若干个学生,每个学生有 3 门功课,从键盘上输入学生有关信息(学号、姓名、成绩),并计算其平均成绩,将原有数据和平均分保存在磁盘文件record 中。具体要求如下:(1)学生有关信息采用结构体类型存储。(2)fwrite()函数实现数据块输出到文件record。(3)结合循环结构实现数据输入和输出。【程序源代码】#include#include#define N 3typedef structint num;char name10;float score3,ave;STU;main()FILE *fp; int i,j; STU stN; if(fp=fopen(record,wb)=NULL) printf(Cannot open file record);exit(0); for(i=0;iN;i+) printf(Input number of NO.%d student:,i+1); scanf(%d,&sti.num); getchar(); printf(Input name of NO.%d student:,i+1); gets(); sti.ave=0; for(j=0;j3;j+) printf(Input score%d of NO.%d student:,j,i+1); scanf(%f,&sti.scorej); sti.ave+=sti.scorej; sti.ave/=j; fwrite(&sti,sizeof(STU),1,fp);fclose(fp);实验12 基于结构体的学生信息管理系统#include #include #include #include #define M 3#define N 5struct s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 专业职业技能试题及答案
- 药学专业自荐试题及答案
- 能源专业试题及答案
- 测绘专业考研试题及答案
- 黑龙江省新时代高中教育联合体2024-2025学年高一上学期期末联合考试政治试卷(含答案)
- 内墙腻子拆除施工方案
- 2026届安徽省合肥市高三物理第一轮复习综合检测试卷2(力学部分B卷)
- 在线直播行业发展报告
- 婚礼主持人开场白模版
- 金乡蔬菜冷库施工方案
- GB/T 18277-2025收费公路收费制式和收费方式
- 高一语文学法指导(绝对经典)
- 包装车间基础知识培训课件
- 2025年贵州建筑中级试题及答案
- 古代服饰复原与租赁服务创新创业项目商业计划书
- 河北社区工作管理办法
- 超声内镜检查及护理配合
- 数字人文与档案重构-洞察及研究
- 关于密码的课件
- 小儿腹泻患者的健康宣教
- 企业有限空间培训课件
评论
0/150
提交评论