华科周纯杰版c语言答案

上传人:二*** 文档编号:143909492 上传时间:2022-08-26 格式:DOC 页数:49 大小:244.50KB
收藏 版权申诉 举报 下载
华科周纯杰版c语言答案_第1页
第1页 / 共49页
华科周纯杰版c语言答案_第2页
第2页 / 共49页
华科周纯杰版c语言答案_第3页
第3页 / 共49页
资源描述:

《华科周纯杰版c语言答案》由会员分享,可在线阅读,更多相关《华科周纯杰版c语言答案(49页珍藏版)》请在装配图网上搜索。

1、第三章 程序和流程控制 1.输入两个整数,输出较大者。 #includevoid main()int a,b; int max=0;printf(Please input two numbers:);scanf(%d%d,&a,&b);if(a=b) max=b;else max=a;printf(The larger number is %dn,max);2.有3个整数a,b,c,由键盘输入,输出其中最大的数。#includevoid main()int a,b,c,max;printf(Please input three numbers:);scanf(%d%d%d,&a,&b,&c);

2、if(ab) if(ac) max=a; else max=c;elseif(bc) max=b; else max=c;printf(The largest number is %dn,temp);3.从1累加到100,用while语句。#include#define N 100void main()int i=1,sum=0;while(i=N) sum+=i; i+;printf(sum:%dn,sum);4.已知a110,a2 3,an3an-1 an-2 ,求 a 的前十项。#include#define N 10void main()int a20=10,-3;int i=0,m=

3、0;for(i=2;iN;i+) ai=3*ai-1+ai-2; m=N;printf(The %d numbers are:n,m);for(i=0;iN;i+) printf(a%d=%dn,i+1,ai); 5.输入一个自然数,判断它是奇数还是偶数。 #includevoid main()int a, temp;printf(Please input a numbers:n);scanf(%d,&a);temp=a%2;if(temp=0) printf(The number %d is an even!n,a);else printf(The number %d is an odd n

4、umber!n,a);6.已知a1 8,an an-1 bn ,b1 1,bn bn-1 3,求 a 前10项之和。 #include#define N 10void main()int aN=8,bN=1,i,sum=0;for(i=1;iN;i+) bi=bi-1+3; for(i=1;iN;i+) ai=ai-1+bi; for(i=0;iN;i+) sum+=ai; printf(The sum is:%dn,sum);7.有一个函数 写一程序,输入x,输出Y的值。 #includevoid main()float x,y;printf(Please input a float num

5、ber x=:);scanf(%f,&x);printf(x=%f,x);if(x=10) y=3*x-11; else y=2*x-1; printf(The value of y is:%.3fn,y);8.给一个不多于5位的的正整数,要求:求出它是几位数,分别打印出每一位数字,最后按照逆序打印各位数字,例如原数为321,应输出为123。 #include#define N 99999#define M 5void main()int i,j,k,m,bM;long int a,temp;printf(Please input a number:n);scanf(%ld,&a);if(aN

6、) printf(Error in input data!n);else temp=a; for(i=0;temp!=0;i+) temp=temp/10; m=i; printf(The number a=%ld have %d single numbersn,a,m); temp=a; for(j=0;j=0;j-) printf(%dt,bj); printf(n); printf(The inverted numbers are:n); for(j=0;jm;j+) printf(%d,bj); printf(n);9编写一猜数游戏程序,随机产生某个整数,从键盘反复输入整数进行猜数,当

7、未猜中时,提示输入过大或过小.猜中时,指出猜的次数.最多允许猜20次. #include#include#include#define N 10void main() int a,b,i; randomize(); a=random(N); printf(Please input number you guess:n); for(i=0;ia) printf(Your number is too big!n); else printf(Your number is too small!n); printf(n); 10.计算1999中能被3整除,且至少有一位数字是5的所有整数。 #includ

8、e#define N 999void main() int i,b,c,d; printf(The numbers are:n); for(i=1;i=N;i+) if(i%3) b=i/100; c=(i-100*b)/10; d=i%10; if(b=5|c=5|d=5) printf(%dt,i); 11.输入两个整数,求它们的最大公约数和最小公倍数。#include stdio.hvoid main()int a,b,c,d;int comYueShu;int comBeishu;printf(please input two number!);scanf(%d %d,&a,&b);c

9、=a;d=b;int r;/求公约数算法while(c%d)r=c%d;c=d;d=r;comYueShu=d;comBeishu=a*b/comYueShu;printf(%d,%d,comYueShu,comBeishu);12.输入一个整数,求它包含又多少个2的因子。(例如,8含有3个2的因子,10含有一个2的因子,15不含有2的因子。)#include stdio.hvoid main()int a=0;int integerGene=0;printf(input a number!n);scanf(%d,&a);while(a%2=0)integerGene+;a/=2; print

10、f(%dn,integerGene);13.计算1!,2!,3!10!#include#define N 10void main() int i; long int a=1; printf(The outcome are:n); for(i=1;i=10;i+) a*=i; printf(The result of %d! is %ldn,i,a); 14.猴子吃桃问题:第一天吃掉总数的一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第十天准备吃的时候见只剩下一个桃子,求第一天开始吃的时候桃子的总数。#include#define N 10void mai

11、n() int i,x10; x9=1; for(i=9;i=1;i-) xi-1=2*(xi+1); printf(The total numbers in the first day are:%dn,x0);15.输入圆锥体的底半径r和高h,计算出圆锥体的体积并输出#include#define PI 3.1415926void main() float r,h,v; printf(Please input the radius and the height:n); scanf(%f%f,&r,&h); v=PI*r*r*h/3; printf(The volume of is %.6fn

12、,v);16题#include stdio.hvoid main() int a,n; long tmp,SUM=0; do printf(nPlease input the number a=); scanf(%d,&a); while(!(a=0 & a=9); printf(nPlease input the number,its value is not too big n=); scanf(%d,&n); tmp = a; while(n-) SUM += tmp; tmp = tmp * 10 + a; printf(na+aa+aaa+.=%ld(a= %d)n,SUM,a);1

13、7题#include stdio.hvoid main()unsigned char week720=Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,saturday; unsigned char weekday;do printf(nPlease input the weeday(0 =the value of weekday = 0 & weekday 1e-6);printf(nThe natural constant e, its %f pow = %f,x,Result);19题#include stdio.h#include math

14、.hvoid main() float a; float x1,x2; printf(nPlease input the number a=); scanf(%f,&a); x1 = x2 = a/2; do x1=x2; x2=(x1+(a/x1)/2; while(fabs(x2 - x1) 1e-5); printf(nInput a namber %f,Its sqrt = %f,a,x2); 第5章课后编程习题参考答案2.已有一个已排好序的数组,现在输入一个数,要求按照原来排序的规则将它插入数组中。#include#define N 7void main() int aN=1,5,1

15、6,45,87,120; int n,i,j,temp1,temp2,end; printf(please enter a number:n); scanf(%d,&n); end=aN-2; if(nend) aN-1=n; else for(i=0;iN-1;i+) if(ni+1;j-) aj=aj-1; ai=n; break; printf(put out the new array:n); for(i=0;iN;i+) printf(%4d,ai);3.打印魔方阵,所谓魔方阵是指这样的方阵,它的每一行,每一列和对角线之和均相等。例如,三阶魔方阵为: 8 1 6 3 5 7 4 9

16、2#includevoid main()int a1616;int n,i,j,k;do printf(enter a number n(115):n); scanf(%d,&n); while(!(n0)&(n=15)&(n%2=1); for(i=1;i=n;i+)for(j=1;j=n;j+)aij=0;j=n/2+1;a1j=1;for(k=2;k=n*n;k+)i=i-1;j=j+1;if(in)i=i+2;j=j-1;elseif(in) j=1;if(aij=0)aij=k;elsei=i+2;j=j-1;aij=k;for(i=1;i=n;i+)for(j=1;j=n;j+)p

17、rintf(%4d,aij); printf(n); 4,有一篇文章,共有3行文字,每行有80个字符,要求分别统计出其中英文大写字母,小写字母,数字,空格以及其它字符的个数。#include#include void main()char s380;int i,j,up,low,num,spa,oth;up=low=num=spa=oth=0;for(i=0;i3;i+)printf(enter the %d line:n,i+1);gets(si);for(j=0;j=A&sij=a&sij=0&sij=9)num+;else if(sij= )spa+;elseoth+;printf(up

18、=%dn,up);printf(low=%dn,low);printf(num=%dn,num);printf(spa=%dn,spa);printf(oth=%dn,oth);5.编写一个程序,将两个字符串连起来,不要用strcat函数。#include#include void main()char s1100,s2100;gets(s1);gets(s2);int i,j;i=j=0;while(s1i!=0)i+;while(s2j!=0)s1i+=s2j+;s1i=0;puts(s1);6.输入每个学生的平均成绩和姓名,将成绩按照递减顺序排序,姓名做相应的调整。输出排序后的成绩和姓名

19、。#include#includedefine N 10/* 假定有十个学生*/void main()char name5010,name110;int markN,i,j,mark1;for(i=0;iN;i+)printf(the %d th students name and mark:n,i+1);scanf(%s%d,namei,&marki); for ( i=0;iN-1;i+) for (j=i+1;jmarki) mark1 = markj; markj = marki; marki = mark1; strcpy(name1,namej); strcpy(namej,nam

20、ei); strcpy(namei,name1); for(i=0;iN;i+) printf(%s,%dn,namei,marki); 7.输出2维数组中行为最大列上为最小的元素(称为鞍点)及其位置。如果不存在任何鞍点,则也应输出相应信息。#includevoid main()int i,j,k,m,n,max,maxj;int a1010;int flag=0;printf(enter the hang number n:n);scanf(%d,&n); printf(enter the lie number m:n); scanf(%d,&m); for(i=0;in;i+)for(j=

21、0;jm;j+)scanf(%d,&aij);printf(n);for(i=0;in;i+) max=ai0; for(j=0;jmax) max=aij; maxj=j; for(k=0;kakmaxj) break; if(k=n)printf(nthe %d hang,the %d lie ,the number %d is an dian!n,i, maxj,max);flag=1;if(!flag)printf(no an dian!);8.计算矩阵A 的转置矩阵A ,例如:A= , A = #includevoid main()int a43,b34;int i,j;printf

22、(enter array a:n);for(i=0;i4;i+)for(j=0;j3;j+)scanf(%d,&aij);for(i=0;i4;i+)for(j=0;j3;j+)printf(%4d,aij);printf(n); for(i=0;i4;i+)for(j=0;j3;j+)bji=aij;for(i=0;i3;i+)for(j=0;j4;j+)printf(%4d,bij);printf(n);9. 输入一个八进制数的字符串,将他转换成等价的十进制字符串,用printf的s格式输出转换结果以检验转换的正确性。例如:输入字符串1732,转换成十进制数的字符串为986。#includ

23、e stdio.h#include string.h#include math.h#define MAX 20void main()int i,len,tmp;long int sum = 0;char inputMAX;char outputMAX=;scanf(%s,input);len = strlen(input);for(i = 0;i len;i +) sum=sum*8+(inputi - 48);for(i = 0;i MAX;i +)outputi = (char)(sum % 10 +48);sum /= 10;if(sum=0)break;len = strlen(out

24、put);for(i = 0;i len/2;i +)tmp = outputi;outputi = outputlen-1;outputlen-1 = tmp;printf(%s,output);10. 输入一行字母串,统计其中各个字母和空格的分别出现的次数。#includevoid main()char s80;int i,up,low,spa;up=low=spa=0;gets(s);for(i=0;si!=0;i+)if(si=A)&(si=a)&(si=z)low+;else if(si= )spa+;else ;printf(the up=%d,the low=%d,the spa

25、=%d,up,low,spa);第6章编程题参考答案 1编写程序,实现:利用指向字符数组的指针变量,统计两个字符数组中相同的字符个数.#include#includevoid main()char s180,s280;char *p1,*p2;int n=0,len1,len2,i,j;p1=s1,p2=s2;gets(s1);gets(s2);len1=strlen(s1);len2=strlen(s2);for(i=0;ilen1&ilen2;i+)if(*(p1+i)=*(p2+i) n+;printf(%d,n);2输入三个整数,按从小到大的顺序输出, 用三种不同方式实现.方法1:#

26、include # include void main() int a3,i,j,temp;/*清屏指令*/ clrscr(); printf(nnPlease input 3 integers :nn); for (i=0;i3;i+) scanf(%d,&ai); for(i=0;i2;i+) for (j=i+1;j3;j+) if (ajai) temp=ai; ai=aj; aj=temp; printf(nnAfter sorted, the 3 inegers:nn); for (i=0;i3;i+) printf(%dt,ai); 方法2:# include # include

27、 void main() int a3,i,j,temp; clrscr(); printf(nnPlease input 3 integers :nn); for (i=0;i3;i+) scanf(%d,a+i); for(i=0;i2;i+) for (j=i+1;j3;j+) if (*(a+j)*(a+i) temp=*(a+i); *(a+i)=*(a+j); *(a+j)=temp; printf(nnAfter sorted, the 3 inegers:nn); for (i=0;i3;i+) printf(%dt,*(a+i); 方法3:# include # includ

28、e void main() void swap(int *p1, int *p2); int a,b,c; int *pa=&a,*pb=&b,*pc=&c; clrscr(); printf(nnPlease input 3 integers :nn); scanf(%d,&a); scanf(%d,&b); scanf(%d,&c); if (bc) swap(pb,pc); if (ab) swap(pa,pb); if (bc) swap(pb,pc); printf(nnAfter sorted, the 3 inegers:nn); printf(%dt%dt%dt,a,b,c);

29、 void swap(int *p1, int *p2) int temp; temp=*p1; *p1=*p2; *p2=temp; 3有一字符串,包含n个字符。使用指针,将此字符串从第m个字符开始的全部字符复制成为另一个字符串。# include # include # include # define N1 20# define N2 20void main() char str1N1,str2N2; int m,n,i,j; clrscr(); printf(nnPlease input a string:nn); gets(str1); n=strlen(str1); do prin

30、tf(nnPlease input place m:nn); scanf(%d,&m); while(nm); for (i=m-1,j=0; in; i+,j+) *(str2+j)=*(str1+i); *(str2+j)=0; printf(nnThe new string :nn); printf(%s,str2); 4 输入一个字符串,内有数字和非数字字符,如:a123x456 17960?302tab5876将其中连续的数字作为一个整数,依次存放到一数组a中。例如123放在a0中,456放在a1中统计共有多少个整数,并输出这些数。# include # include # incl

31、ude # include void main() char str60,ch; int i,j,bit,dit,n; long int a20; clrscr(); printf(nnPlease input a string:nn); scanf(%s,str); for (i=0,j=0,a0=0,bit=1,dit=0;istrlen(str) ;i+) if(isdigit(ch=*(str+i) dit=1; aj*=bit; aj+=(ch-48); bit=10; n=j; else if (dit=1) j+; aj=0 ; dit=0; bit=1; printf (nna

32、i :nn) ; for (i=0;i=n;i+) printf(nna%d=%ldn,i,*(a+i); 5 利用数组和指针,将一个4*4的矩阵转置,并输出矩阵中的最大值及其位置# include # include # define N 4void initpm(float * pm, float mtN);void main() void inputm(float mtN); void printm( float mtN); void schmax(float mtN); void tranmtx(float mtN); float mtxNN ; clrscr(); inputm(mt

33、x); printf(nn); printm(mtx); schmax(mtx); tranmtx(mtx); printm(mtx); printf(n); void inputm(float mtN) int i,j; float *pmN; initpm(pm,mt); printf(nnPlease input a 4*4 matrix:nn); for(i=0;i4;i+) for (j=0;j4;j+,pmi+) scanf(%f,pmi); void printm( float mtN) int i,j; float *pmN; initpm(pm,mt); for(i=0;iN

34、;i+) for(j=0;jN;j+,pmi+) printf(%3.2ft,*pmi); printf(n); void schmax(float mtN) int i,j,line ,colum; float *pmN,max; initpm(pm,mt); max=mt00; line=0; colum=0; for (i=0;iN;i+) for(j=0;jN;j+) if (max*(pmi+j) max=*(pmi+j); line=i; colum=j; printf(nnThe max digital in the matrix :%3.2fnn,max); printf(Th

35、e line of the max digital : %d nn,line+1); printf(The colum of the max digital : %d nn,colum+1); void tranmtx(float mtN) int i,j; float *pmN; initpm(pm,mt); for (i=0;iN;i+) for(j=i+1;jN;j+) float temp; temp=*(pmi+j); *(pmi+j)=*(pmj+i); *(pmj+i)=temp; void initpm(float * pm, float mtN) int i; for (i=

36、0;iN;i+) pmi=mti; 6利用指向指针的指针方法对6个字符串排序并输出.# include # include # include # define N 6void main() char strN20; char *pstrN; int i,j; for (i=0;iN;i+) pstri=stri; char *pp; pp=pstr; clrscr(); printf(nnPlease input %d strings:nn,N); for(i=0;iN;i+) gets(pstri); for(i=0;iN-1;i+) for(j=i+1;j0) char *temp=*(

37、pp+i);*(pp+i)=*(pp+j);*(pp+j)=temp; printf(nnAfter sorted ,the %d strings:nn,N); for(i=0;iN;i+) printf(%sn,*pp+); 7写一程序,求任意一输入字符串的长度,将此输入的字符串按逆序的方式存入所在位置中,显示输入字符串和其逆字符串.# include # include void main() char str80, *pstr=str,ch; int n=0,i; clrscr(); printf(Please input a string:nn); gets(str); printf(

38、nnThe inputing string:nn); printf(%s, str); while(*pstr!=0) pstr+; n+; printf(nnThe length of the string :%d,n); for (i=0;i(n/2); i+) ch=*(str+i); *(str+i)=*(str+n-1-i); *(str+n-1-i)=ch; printf(nnAfter changed ,the string:nn); printf(%snn, str); 8编写一个程序,先读入一段正文,然后删除其中的单词from、in、at、an和on,最后显示该结果文本段.#

39、 include # include # include void delWords(int *pi,int *pN,char *pt);void main() char text10020,*ptext100; int i,N,*pN=&N,*pi=&i; for (i=0;i100;i+) ptexti=texti; clrscr(); printf(Attention : After you input the end of text, please input wordEOF!nn); printf(Please input a text(words=100),EOF end:nn);

40、 for(i=0;strcmp(ptextN,EOF);i+) scanf(%s,ptexti); N=i; for (i=0;i=(N-1);i+) if (strcmp(ptexti,from)=0) delWords(pi,pN,ptext); else if (strcmp(ptexti,in)=0) delWords(pi,pN,ptext); else if (strcmp(ptexti,at)=0) delWords(pi,pN,ptext); else if (strcmp(ptexti,an)=0)delWords(pi,pN,ptext);else if (strcmp(ptexti,on)=0)delWords(pi,pN,ptext); printf(nAfter deleting the words:from ,in at, an, on , the text:nn); for (i=0; i=(N-1);i+) printf(%s ,ptexti); printf(nn); void delWords(int *

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!