收藏 分销(赏)

第五章习题源程序.doc

上传人:仙人****88 文档编号:8553969 上传时间:2025-02-18 格式:DOC 页数:5 大小:46KB
下载 相关 举报
第五章习题源程序.doc_第1页
第1页 / 共5页
第五章习题源程序.doc_第2页
第2页 / 共5页
点击查看更多>>
资源描述
第五章习题参考源程序 5.1输入 入个正整数M和N,求其中最大公约数和最小公倍数 #include <math.h> #include <stdio.h> main() { unsigned int m,n,k,maxgy,mingb; int i; printf("Please input the two number of m,n\n"); scanf("%u,%u",&m,&n); if (m>n) k=n; else k=m; for (i=k;i>1;i--) {if ((m%i==0)&&(n%i==0)) break; } printf("%u\n",i); if (i>1) {printf("These two number have maxgy=%u\n",i); mingb=i*(m/i)*(n/i); printf("The two number have mingb=%u\n",mingb); } else {printf("These two number have maxgy=%d\n",i); printf("These two number have mingb=%u\n",m*n);} } 5.2输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数 #include <math.h> #include <stdio.h> main() { char c; int num_count=0,char_count=0,space_count=0,other_count=0; while ((c=getchar())!='\n') { if ((c>='a' && c<='z') || (c>='A' && c<='Z')) char_count++; else if (c>='0' && c<='9') num_count++; else if (c==32) space_count++; else other_count++; } printf("The number of character is:%d\n",char_count); printf("The number of charcter is :%d\n",num_count); printf("the number of space is :%d\n",space_count); printf("The number of other symbol is :%d\n",other_count); } 5.3 求Sn=a+aa+aaa+….aaaaaa之值,其中a是一个数字。例如:2+22+222+2222+22222(此时n=5),n由键盘输入。 #include <math.h> #include <stdio.h> main() { short int a,b,n,i; long int sum=0; scanf("%1d,%2d",&a,&n); b=a; for (i=1;i<=n;i++) { sum+=a; a=a*10+b; } printf("The result of this equation is :%d\n",sum); } 5.4 求 #include <stdio.h> main() { int i,j; int n; long int m, sum=0; printf("Please input the number of N\n"); scanf("%d",&n); for (i=1;i<=n;i++) {m=1; for (j=1;j<=i;j++) m*=j; sum+=m; } printf("The number of N is :%d\n",n); printf("The sum of N! is :%ld\n",sum); } 5.5 打印出所有的“水仙花数”,所谓:“水仙花数”是指一个3位数,其各位数字立方和等腰三角形于该数本身。例如153是一水仙花数,因为 153=13+53+33 #include <math.h> #include <stdio.h> main() { short int c1,c2,c3; int n; for (n=100;n<=999;n++) {c3=n/100; c2=(n-c3*100)/10; c1=(n-c3*100-c2*10)/1; if (n==c3*c3*c3+c2*c2*c2+c1*c1*c1) printf("The number is a shuixian flower :%d\n",n); } } 5.6 一个数如果恰好等腰三角形于它的因子之各,这个数就称为“完数”。例如,6的因子是1,2,3,而6=1+2+3,因些6是:“完数”。编程序找出1000之内的所有完数,并按下面格式输出其因子。 6 is factors are 1,2,3 #include <math.h> #include <stdio.h> main() { int n,i,sum; for (n=2;n<1000;n++) { sum=0; for (i=1;i<n;i++) if (n%i==0) sum+=i; if (n==sum) {printf(" %d is factors are:",n); for (i=1;i<n;i++) if (n%i==0) printf(" %d,",i); printf("\n"); } } } 5.7 有一个分数序列 求出这个数列的前20项之和。 #include <math.h> #include <stdio.h> main() { int n; float a1=1; float a2=2; float b1=2; float b2=3; float sum=0.0; sum+=b1/a1; printf("1 sum of %f\n",sum); sum+=b2/a2; printf("2 sum of %f\n",sum); for (n=3;n<12;n++) { a1=a1+a2; b1=b1+b2; sum+=b1/a1; printf(" %d sum of %f\n",n,sum); a2=a2+a1; b2=b2+b1; sum+=b2/a2; printf(" %d sum of %f\n",n,sum); } } 5.8 猴子吃桃子问题。猴子第一天摘下若有干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉了一半,又多吃了一个。以后每天早上都吃了前一天剩下一的一半零一个。到第10天早上想吃时,就只剩下一个桃子了。求第一个共摘多少桃子。 #include <math.h> #include <stdio.h> main() { int sum=1; int n; printf("The number of peachsis \n"); for(n=1;n<10;n++) { sum=(sum+1)*2; printf("The %d day peach is:",10-n); printf("%d\n",sum); } } 5.8 用迭代法求。求平方根的迭代公式为,要求前后两次求出的x的差的绝对值小于10-5。 #include <math.h> #include <stdio.h> main() { float a; double x1,x2; printf("Please input the number of a\n"); scanf("%f",&a); if (fabs(a)<=1e-6) printf("The number you input must a positive number\n"); else { x1=1.0; /*注意:在进行实数运算时,1等数要写成1.0*/ printf("x1 %f\n",x1); x2=1.0/2*(x1+a/x1); printf("x2 %f\n",x2); while(fabs(x2-x1)>1e-5) { x1=1.0/2.0*(x2+a/x2); printf("x1 %f\n",x1); x2=1.0/2.0*(x1+a/x1); printf("x2 %f\n",x2);} } printf("the value of sqrt(a) is :%10.4f\n",x1); } 5.10打印出以下图案 * * * * * * * * * * * * * * * * * * * * * * * * * #include <math.h> #include <stdio.h> main() { int n; int i,j; printf("please input the line number:"); scanf("%d",&n); for (i=1;i<=n;i++) if (i<=(n+1)/2) {for (j=1;j<=(n+1)/2-i;j++) printf(" "); for (j=1;j<=2*i-1;j++) printf("*"); printf("\n"); } else { for (j=1;j<=i-(n+1)/2;j++) printf(" "); for (j=1;j<=n-(i-(n+1)/2)*2;j++) printf("*"); printf("\n"); } } 5
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服