收藏 分销(赏)

第三讲(上)基本编程思想——累加求和.doc

上传人:s4****5z 文档编号:8690564 上传时间:2025-02-26 格式:DOC 页数:5 大小:43.50KB
下载 相关 举报
第三讲(上)基本编程思想——累加求和.doc_第1页
第1页 / 共5页
第三讲(上)基本编程思想——累加求和.doc_第2页
第2页 / 共5页
点击查看更多>>
资源描述
【121105002·Design】数列第一项为81,此后各项均为它前一项的正平方根,统计该数列前30项之和后,并以格式"%.3f"写入到考生文件夹下的新建文件Designl.dat。(frequency:3) #include <stdio.h> #include <math.h> void main() { FILE *p; float s=0,a=81;int i; /* * * * 考生在这里添加代码 * * */ } 【answer】 #include <stdio.h> #include <math.h> void main() { FILE *p; float s=0,a=81;int i; for(i=1;i<=30;i++) { s=s+a; a=sqrt(a); } p=fopen("Design1.dat","w"); fprintf(p,"%.3f ",a); fclose(p); } 【121105004·Design】计算多项式a0-a1*x+a2*x*x/2!-a3*x*x*x/3!+…-a9*x*x*x*x*x*x* x*x*x/9!的值,并将结果一格式"%f"写入 到考生文件夹中Paper子文件夹下的新建文件Des ign1.dat。(frequency:2) #include <stdio.h> #include <math.h> void main() { FILE *p; int i; float x=1.279,t,y; float a[10]={1.1,3.2,-2.5,5.67,3.42,-4.5,2.54,5.6,0.97,4.65}; /* * * * 考生在这里添加代码 * * */ } 【answer】 #include <stdio.h> #include <math.h> void main() { FILE *p; int i; float x=1.279,t,y; float a[10]={1.1,3.2,-2.5,5.67,3.42,-4.5,2.54,5.6,0.97,4.65}; y=a[0]; t=-x; for(i=1;i<=9;i++) { y=y+a[i]*t; t=-t*x/(i+1); } p=fopen("design1.dat","w"); fprintf(p,"%f",y); fclose(p); } 【121105006·Design】计算表达式1+2!+3!……+12!的值,并将计算结果以格式"%ld"写入到考生文件夹中paper子文件夹下的新建文件Design2.dat。 #include <stdio.h> void main() { FILE *p; long s=1,k=1; int i; /* * * * 考生在这里添加代码 * * */ } 【answer】 #include <stdio.h> void main() { FILE *p; long s=1,k=1; int i; for(i=2;i<=12;i++) { k=k*i; s=s+k; } p=fopen("design1.dat","w"); fprintf(p,"%ld",s); fclose(p); } 【121105010·Design】设计编写并运行程序,完成以下功能:利用公式ㄦ/4=1-1/3+1/5-1/7 +……..公式计算的近似值 ,直到某一项的绝对值小于1e-6为止。(将结果不包含此项)将计算结果以格式写入到考生文件夹中Paper子文件夹下的新建Design2.dat。 #include <stdio.h> #include <math.h> void main() { FILE *fp; float n=1,t=1,pi=0; int i; /* * * * 考生在这里添加代码 * * */ } 【answer】 #include <stdio.h> #include <math.h> void main() { FILE *fp; float n=1,t=1,pi=0; int i; while(fabs(n*1/t)>=1e-6) { pi=pi+n*1/t; n=-n; t=t+2; } fp=fopen("Desing2.dat","w"); fprintf(fp,"%f",4*pi); fclose(fp); } 【121105014·Design】有数列:2/1,3/2,5/3,8/5,13/8,21/13,.......求出数列的前40项的和。将计算结果以格式"%.6f"写入到考生文件夹中Paper子文件夹下的新文件夹Design2.dat。 #include<stdio.h> void main() { FILE *p; int i; float f1=1.0,f2=2.0,t1=2.0,t2=3.0,s; float f,t; s=t1/f1+t2/f2; /* * * 考生在这里添加代码 * */ } 【answer】 #include<stdio.h> void main() { FILE *p; int i; float f1=1.0,f2=2.0,t1=2.0,t2=3.0,s; float f,t; s=t1/f1+t2/f2; for(i=3;i<=40;i++) { f=t2; t=t2+f2; s=s+t/f; t2=t; f2=f; } p=fopen("design2.dat","w"); fprintf(p,"%.6f",s); fclose(p); } 【121105028·Design】x[i],y[i]分别表示平面上一点的坐标,求下列10个点与点(1.0,1.0)的距离的总和,并将结果以格式"%.6f"写入到考生文件夹中paper子文件夹下的新建文件design1.dat。 #include<stdio.h> #include<math.h> void main() { FILE *p; int i; float x[10]={-1.5,2.1,6.3,3.2,-0.7,7.0,5.1,3.2,4.5,7.6}; float y[10]={3.5,7.6,8.1,4.5,6.0,1.1,1.2,2.1,3.3,4.4}; float s=0.0; /* * * * 考生在这里添加代码 * * */ } 【answer】 #include<stdio.h> #include<math.h> void main() { FILE *p; int i; float x[10]={-1.5,2.1,6.3,3.2,-0.7,7.0,5.1,3.2,4.5,7.6}; float y[10]={3.5,7.6,8.1,4.5,6.0,1.1,1.2,2.1,3.3,4.4}; float s=0.0; for(i=0;i<10;i++) s=s+sqrt( (x[i]-1.0)*(x[i]-1.0)+(y[i]-1.0)*(y[i]-1.0) ); p=fopen("Design1.dat","w"); fprintf(p,"%.6",s); fclose(p);} 第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 

客服