• / 27
  • 下载费用:12 金币  

C语言程序设计(何钦铭-)课后习题作业.doc

关 键 词:
语言程序设计 课后 习题 作业
资源描述:
''第一章习题1、对C语言来说,下列标示符中哪些是合法的,哪些是不合法的? Total, _debug, Large&Tall, Counter1, begin_ 答:合法的:_debug、Counter1、begin_。 不合法的:Total、Large&Tall。2、改写1.4节中的流程图1.2,求1—100中能被6整除的所有整数的和。Sum=0,i=1I<=100I能被6整除?sum=sum+ii=i+1真假打印sum3、改写1.4节中的程序,求1—100中能被6整除的所有整数的和,并在编程环境中验证该程序的运行结果。 答:程序为:#includeint main(void){int i,sum=0;for(i=1;i<=100;i++)if(i%6==0)sum=sum+i;printf("sum=%d\n",sum);return 0;}4、对于给定的整数n(n>1),请设计一个流程图判别n是否为一个素数(只能被1和自己整除的整数),并分析该流程图中哪些是顺序结构、哪些是分支结构和循环结构。开始结束输入ni=2r=n/i的余数r=0?i=i+1i>=(n-1)输出n“是素数”输出n“不是素数”假真真假开始结束输入ni=2r=n/i的余数r=0?i=i+1i>=(n-1)输出n“是素数”输出n“不是素数”假真真假第二章作业1.编写程序,输入两个整数num和num2,计算并输出她们的和、差、积、商。答:程序:#include void main(){int num1,num2,y1,y2,y3,y5;double y4;printf("请输入两个数:num1,num2\n");scanf("%d%d",&num1,&num2);y1=num1+num2;y2=num1-num2;y3=num1*num2;y4=num1*1.0/num2;y5=num1%num2;printf("num1,num2和y1=%d\nnum1,num2差y2=%d\nnum1,num2积y3=%d\nnum1,num2商y4=%lf\nnum1,num2余y5=%d\n",y1,y2,y3,y4,y5); }2.为了倡导居民节约用电,某省电力公司执行“阶梯电价”,安装一户一表的居民用户电价分为两个“阶梯”:月用电量50千瓦时以内的,电价为0.53元/千瓦时;超出50千瓦时的用电量,电价上调0.05元/千瓦时。编写程序,输入用户的月用电量(千瓦时),计算并输出该用户应支付的电费(元)。答:程序:#include void main(){double x,y;printf("请输入月用电量:x\n");scanf("%lf",&x);if(x<=50){y=0.53*x;printf("电费y=%lf\n",y);}else {y=0.58*x;printf("应支付电费y=%lf\n",y);} }3.编写程序,输入实数x和正整数n,计算xn,,不允许调用pow()函数求幂。答:程序:#include void main(){ double x,power; int i,n;printf("请输入:x,n\n");scanf("%lf%d",&x,&n);power=1.0;for(i=1;i<=n;i++)power=power*x;printf("power(x,n)=%lf\n",power);}4.编写程序,输入两个正整数m和n,求∑(i2+1/i)(i从m到n。)答:程序:#includevoid main(){int i,m,n;double sum;sum=0;printf("input:m,n\n");scanf("%d%d",&m,&n);for(i=m;i<=n;i++)sum=sum+(i*i+1.0/i);printf("sum=%lf\n",sum);}5.编写程序,输入一个正整数n,计算1-2/3+3/5-4/7+5/9-6/11+…的前n项之和。答:程序:#includevoid main(){int i,n;double sum,item,flag;flag=1;sum=0;printf("input:n\n");scanf("%d",&n);for(i=1;i<=n;i++){item=(flag*i)/(2*i-1);flag=-flag;sum=sum+item;}printf("sum=%lf\n",sum);}6.某用户为购房办理商业贷款,选择了按月等额本息还款法,计算公式如下,在贷款本金(loan)和月利率(rate)一定的情况下,住房贷款的月还款(money)取决于还款月数(month)。客户打算在5—30年的范围内选择还清贷款的年限,想得到一张“还款年限—月还款额表”以供参考。编写程序,输入贷款本金loan和月利率rate,输出“还款年限—月还款额表”,还款年限的范围是5—30年,输出时分别精确到年和元。Money=loan*rate(1+rate)month/[(1+rate)month-1]答:程序:#include#includevoid main(){int lower,upper,month,year,loan;double money,rate;lower=5;upper=30;printf("请输入贷款本金:loan\n");scanf("%d",&loan);printf("请输入月利率:rate\n");scanf("%lf",&rate);printf("还款年限\t月还款额\n");for(year=lower;year<=upper;year++){month=year*12;money=(loan*rate*pow((1+rate),month))/(pow((1+rate),month)-1);printf("%d\t\t%lf\n",year,money);}}7.编写程序,输入一个正整数n,求∑2i(i从1到n),可调用pow()函数求幂。答:程序:#include#includevoid main(){int i,n;double sum,item;sum=0;printf("input:n\n");scanf("%d",&n);for(i=0;i<=n;i++){item=pow(2,i);sum=sum+item;}printf("sum=%lf\n",sum);}8.编写程序,输入一个正整数n,求e=∑i!(i从0到n)。要求定义和调用函数fact(n)计算n!,函数类型是double.答:程序:#includedouble fact(int n);void main(){int i,n;double sum;sum=0;printf("input:n\n");scanf("%d",&n);for(i=0;i<=n;i++){sum=sum+fact(i);}printf("sun=%lf\n",sum);}double fact(int n){int i;double product;product=1.0;for(i=1;i<=n;i++){product=product*i;}return product;}第三章作业1.#include#includevoid main(){int a,b,c;double s,area,perimeter;printf("请输入三角形三条边:a,b,c\n");scanf("%d%d%d",&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){perimeter=a+b+c;s=(a+b+c)/2.0;area=sqrt(s*(s-a)*(s-b)*(s-c));printf("三角形的周长perimeter=%.2lf\n三角形的面积area=%.2lf\n",perimeter,area);}}2、#include#includevoid main(){double salary,tax,rate;printf("请输入职工的月薪:salary\n");scanf("%lf",&salary);if(salary<=850) rate=0;else if(salary<=1350) rate=5;else if(salary<=2850) rate=10;else if(salary<=5850) rate=15;else rate=20;tax=rate*(salary-850)/100;printf("职工应缴个人所得税tax=%.2lf\n",tax);}3、#includevoid main(){double m,n,y;printf("请输入行驶里程(公里):m\n等待时间(分钟):n\n");scanf("%lf%lf",&m,&n);if((m+n/5.0)<=3) y=10;else if((m+n/5.0)<=10) y=2*(m+n/5.0);else y=2*10+3*(m+n/5.0-10);;printf("乘客应付车费y=%d\n",(int)((y*10+0.5)/10.0));}4、#includevoid main(){int i,n,A,B,C,D,E;double score,total;printf("请输入一个数n:\n");scanf("%d",&n);total=0;A=0;B=0;C=0;D=0;E=0;for(i=1;i<=n;i++){printf("请输入第#%d个学生成绩:\n",i);scanf("%lf",&score);total=total+score;if(score>=90)A++;else if(score>=80)B++;else if(score>=70)C++;else if(score>=60)D++;else E++;}printf("平均成绩=%lf\n",total/n);printf("等级成绩个数:\nA=%d\nB=%d\nC=%d\nD=%d\nE=%d\n",A,B,C,D,E);}5、#includevoid main(){int year;printf("21世纪所有的闰年:\n");for(year=2000;year<3000;year++)if(year%4==0&&year%100!=0||year%400==0)printf("%d\n",year);}第四章作业2、#include"stdio.h"#include"math.h"double fact(int n);void main(){int i,n;double x,s;printf("请输入一个实数x:\n");scanf("%lf",&x);s=0;i=1;while((pow(x,i)/fact(i))>=0.00001){s=pow(x,i)*1.0/fact(i)+s;i++;}printf("s=%.2lf\n",s);}double fact(int n){int i;double result;result=1;for(i=1;i<=n;i++)result=result*i;return result;}3、#include"stdio.h"int main(void){int x,a,count,sum;printf("请输入一个整数x:\n");scanf("%d",&x);sum=0;count=0;if(x<0) x=-x;do{a=x%10;sum=sum+a;x=x/10;count++;}while(x!=0);printf("位数=%d\n",count);printf("各个位数之和为:%d\n",sum);return 0;}5、#include"stdio.h"void main(){int a,n,i=1,s=0,t=0;printf("请输入两个正整数a,n=:\n");scanf("%d%d",&a,&n);while(i<=n){t=t+a;s=s+t;a=a*10;++i;}printf("a+aa+aaa+…+aa…a=%d\n",s);}7、#include"stdio.h"void main(){int i,j,k,l,n;printf("1-10000之间的水仙花数有:\n");for(n=10;n<10000;n++){i=n/1000;j=n/100%10;k=n/10%10;l=n%10;if(i*1000+j*100+k*10+l==i*i*i+j*j*j+k*k*k+l*l*l){printf("%-5d",n);}}printf("\n");}9、#include"stdio.h"#include"math.h"void main(){int i,n,m,j,k;i=0;printf("请输入一个多位整数:\n");scanf("%d",&n);k=n;while(k!=0){i++;k=k/10;}k=n;m=pow(10,i-1);printf("逐位输出:%d",n/m%10);printf(",");for(j=m/10;j>0;j=j/10){n=k/j;printf("%d",n%10);if(j!=1)printf(",");}}11、#include"stdio.h"void main(){int p,r,n,m,temp;printf("请输入两个正整数:\n");scanf("%d%d",&n,&m);if(n#include long fac(int n,int a){long sum;if(n==1)sum=a; else sum=(long)(pow(10,n-1)*a)+fac(n-1,a);return(sum);}main(){int a,n,i;long temp,sum=0;printf("请输入一个数a,n\n");scanf("%d%d",&a,&n); for(i=1;i<=n;i++) { temp=fac(i,a); sum+=temp;} printf("它们的和是=%ld",sum); return 0; }2.#includeint countdight(int number,int dight);int main(void){int number;printf("输入一个数:");scanf("%d",&number);countdight(number,2);return 0;}countdight(int number,int dight){int a,count;count=0;while(number!=0){a=number%10;number=number/10;if(a==dight)count++;}printf("%d\n",count);return count;}3.#includeint fib(int n);int main(void){int m,n,num1;num1=1;printf("请输入两个正整数m和n(m>=1,n<=10000)\n");scanf("%d%d",&m,&n);if(num1>=m&&num1<=n)fib(n);return 0;}int fib(int n){int i,x1,x2,x,t;x1=1;x2=1;for(i=0;ivoid is(int num);int main(void){int n,m,num1;num1=1;printf("输入两个数m和n(m>=1,n<=1000)\n");scanf("%d%d",&m,&n);if(num1>=m&&num1<=n)is(num1);return 0;}void is(int num){int i,j,k;for(num=1;num<1000;num++){i=num/100;j=num/10%10;k=num%10;if(i*100+j*10+k*1==i*i*i+j*j*j+k*k*k)printf("%5d",num);}printf("\n");}6.#includeint reverse(int number);int main(void){int x;printf("请输入一个正整数:\n");scanf("%d",&x);reverse(x);return 0;}int reverse(int number){while(number!=0){printf("%d",number%10);number=number/10;}printf("\n");return number;}第七章作业1.#includevoid main(){ int i,j=0,k=0,n,p,q,max,min; int a[10]; printf("请输入n(1max){max=a[i];j=i;}}p=a[9];a[9]=a[j];a[j]=p;q=a[0];a[0]=a[k];a[k]=q;for(i=0;iint main(void){int m,n,i,j,sum;int a[6][6],x[80];printf("请输入m,n(1<=m,n<=6)的值: \n");scanf("%d%d",&m,&n);printf("请输入%d个数: \n",m*n);for(i=0;ivoid main(){int a[3][3],max,max1,max2,i,j,flag=1;printf("请输入9个数: \n");for(i=0;i<=2;i++)for(j=0;j<=2;j++)scanf("%d",&a[i][j]);for(i=0;i<=2;i++){max=a[i][0];for(j=0;j<=2;j++){if(a[i][j]>max){max=a[i][j];max1=j;}}for(max2=0;max2<=2;max2++){if(max>a[max1][max2]){flag=0;break;}}if(flag==1) printf("鞍点是 %d\n",a[max2][max1]); if(flag==0) printf("no number\n");flag=1;}}8.#include#includeint main(void){int i,n;char s[80];printf("请输入n(n<80)的值: \n");scanf("%d",&n);printf("请输入%d个字符: \n",n);for(i=0;i='A'&&s[i]<='Z')s[i]=155-s[i];}for(i=0;s[i]!='\0';i++)putchar(s[i]);printf("\n");return 0;}#include#includeint main(void){ int i, n; char f,s[80];printf("Enter a string: "); /*输入字符串*/i = 0;while((s[i] = getchar( )) != '\n') i++; s[i] = '\0';n = 0;for(i = 0; s[i] != '\0'; i++){if(s[i] <= 7f&& s[i] >= 20)s[i]=s[i++];elsebreak; }for(i = 0; s[i] != '\0'; i++)putchar(s[i]);return 0;} 第八章作业1.#includeint main(void){char ch;int a[10];int i,j,temp;printf("请输入10个整数:\n");for(i=0;i<10;i++)scanf("%d",&a[i]);printf("请输入A或D:");scanf(" %c",&ch);if(ch=='A'){for(i=0;i<9;i++){for(j=i+1;j<10;j++){if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}}for(i=0;i<10;i++){printf("%d\n",a[i]);}}else if(ch=='D'){for(i=0;i<9;i++){for(j=i+1;j<10;j++){if(a[i]#includevoid mcopy(char *s,int m);int main(void){ int i,m,l;char str[100];printf("请输入您要输入的字符串:");gets(str);printf("您将从原字符串的第m位复制新字符串,请输入m:\n");scanf("%d",&m);printf("复制后,新的字符串为:"); mcopy(str,m);return 0;}void mcopy(char *s,int m){printf("%s\n",s+m-1);}11.#include#includevoid fushu(char *p);int main(void){char str[80];printf("请输入一个英语名词单词:");scanf("%s",str); fushu(str);puts(str);return 0;}void fushu(char *p){ int i,l;l=0;for(i=0;*(p+i)!='\0';i++){l++;}if(*(p+l-1)=='y'){*(p+l)='e';*(p+l+1)='s';*(p+l+2)='\0';}else if((*(p+l-1)=='s')||(*(p+l-1)=='x')||((*(p+l-2)=='c')&&(*(p+l-1)=='h'))||((*(p+l-2)=='s')&&(*(p+l-1)=='h'))){*(p+l)='e';*(p+l+1)='s';*(p+l+2)='\0';}else if(*(p+l-1)=='o'){*(p+l)='e';*(p+l+1)='s';*(p+l+2)='\0';}else{*(p+l)='s';*(p+l+1)='\0';}}12.#include#includeint main(void){int i,j,a,n,sum,avg;int *p;printf("Enter n:");scanf("%d",&n);if((p=(int*)calloc(n,sizeof(int)))==NULL){printf("Not able to allocate memory.\n");exit(1);}printf("请输入%d个学生的成绩:\n",n);for(i=0;i*(p+j)){a=*(p+i);*(p+i)=*(p+j);*(p+j)=a;}}}printf("学生的平均成绩是:%d\n",avg);printf("学生的最高成绩是:%d\n",*(p+n-1));printf("学生的最低成绩是:%d\n",*p);return 0;}第九章作业1.#includestruct{int hour,minute,second;}time;int main(void){scanf("%d:%d:%d",time.hour,time.minute,time.second);time.second++;if(time.second==60){time.minute++;time.second=0;if(time.minute==60){time.hour++;time.minute=0;if(time.hour==24)time.hour=0;}}printf("%d:%d:%d",time.hour,time.minute,time.second);return 0;}2.struct s1{char c1,c2;int n;};struct s2{int n; struct s1 m;};int main(void){printf("\n\t%d\t%d\t%c\n",m.n,m.m,m.n,m.m.c1,m.m.c2);return 0;}8.#include#define N 10struct person{char num[10];char name[20];double jbgz,jj,zc,sfgz;};struct person gz[N];int main(){ int i,index;printf("请输入%d名工人的基本信息:\n", N);for(i=0;igz[index].sfgz)index=i;}printf("实发工资最高的工人:\n姓名:%s\t实发工资:%.2lf\n",gz[index].name,gz[index].sfgz);return 0;}9.#include#define N 3struct book{char sm[30];double dj;};struct book xx[N];int main(){ int i,max,min;printf("请输入%d本书的基本信息:\n", N);for(i=0;ixx[max].dj)max=i;}printf("定价最高的书:\n");printf("\t%s\t%.2lf\n",xx[max].sm,xx[max].dj);printf("定价最低的书:\n");printf("\t%s\t%.2lf\n",xx[min].sm,xx[min].dj);return 0;}第十章作业1.#include"stdio.h"int main(){int n;int fact(int n);printf("请输入一个3位数:n\n");scanf("%d",n);printf("%d",fact(n));return 0;}int fact(int n){int i,a,b,c,count=0;for(i=101;1<=n;i++){a=i%10;b=(i/10)%10;c=i%100;if(a==b||a==c||b==c)count++;}return count;}2.#include #include double fact(int n,double x); void main() { int n; double result,x; printf("请输入整数n及x:"); scanf("%d%lf",&n,&x); result=fact(n,x); printf("结果为:%.4f\n",result); } double fact(int n,double x) { if(n==1) return x; else return pow(-1,n-1)*pow(x,n)+fact(n-1,x); }3.# include "stdio.h"int invert(long m){ printf("%ld", m%10); m = m/10; if (m>0) invert(m) ;} main(){ long n; printf("Enter n: "); scanf("%ld", &n); invert(n); printf("\n");}
展开阅读全文
  语墨文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
0条评论

还可以输入200字符

暂无评论,赶快抢占沙发吧。

关于本文
本文标题:C语言程序设计(何钦铭-)课后习题作业.doc
链接地址:http://www.wenku38.com/p-145387.html

                                            站长QQ:1002732220      手机号:18710392703    


                                                          copyright@ 2008-2020 语墨网站版权所有

                                                             经营许可证编号:蜀ICP备18034126号

网站客服微信
收起
展开