欢迎来到天天文库
浏览记录
ID:56719028
大小:18.00 KB
页数:2页
时间:2020-07-06
《统计所有小于等于n的素数的个数累加求和.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1、函数fun的功能是:统计所有小于等于n(n>2)的素数的个数,素数的个数作为函数值返回。题中的是得到小于和等于20的素数和素数的个数。#includeintfun(intn){inti,j,count=0;printf("Theprimenumberbetween3to%d",n);for(i=3;i<=n;i++){/**********found**********/for(j=2;j2、found**********/if(j>=i){count++;printf(count%15?"%5d":"%5d",i);}}returncount;}main(){intn=20,r;r=fun(n);printf("Thenumberofprimeis:%d",r);}2、数列中,第一项值为3,后一项都比前一项的值增5;给定程序中的函数fun的功能是:计算前n(43、8,23,28,……,93,98。符合此条件的累加值应为42,126,366,570,1010。#include#defineN20intfun(intn,int*a){inti,j,k,sum;/**************found**************/sum=j=0;for(k=3,i=0;i4、,i;printf("Entern(4doublefun(intn){inti,k=-1;doubles=0;for(i=1;i<=2*n;i++){k5、=-k;s=s+k*1.0/i;}returns;}main(){intn;doubles;printf("Inputn:");scanf("%d",&n);s=fun(n);printf("s=%f",s);NONO();}NONO(){/*请在此函数内打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/FILE*rf,*wf;intn,i;doubles;rf=fopen("D:\Exam\24010001\in.dat","r");wf=fopen("D:\Exam\24010001\out.da6、t","w");for(i=0;i<10;i++){fscanf(rf,"%d",&n);s=fun(n);fprintf(wf,"%lf",s);}fclose(rf);fclose(wf);}
2、found**********/if(j>=i){count++;printf(count%15?"%5d":"%5d",i);}}returncount;}main(){intn=20,r;r=fun(n);printf("Thenumberofprimeis:%d",r);}2、数列中,第一项值为3,后一项都比前一项的值增5;给定程序中的函数fun的功能是:计算前n(43、8,23,28,……,93,98。符合此条件的累加值应为42,126,366,570,1010。#include#defineN20intfun(intn,int*a){inti,j,k,sum;/**************found**************/sum=j=0;for(k=3,i=0;i4、,i;printf("Entern(4doublefun(intn){inti,k=-1;doubles=0;for(i=1;i<=2*n;i++){k5、=-k;s=s+k*1.0/i;}returns;}main(){intn;doubles;printf("Inputn:");scanf("%d",&n);s=fun(n);printf("s=%f",s);NONO();}NONO(){/*请在此函数内打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/FILE*rf,*wf;intn,i;doubles;rf=fopen("D:\Exam\24010001\in.dat","r");wf=fopen("D:\Exam\24010001\out.da6、t","w");for(i=0;i<10;i++){fscanf(rf,"%d",&n);s=fun(n);fprintf(wf,"%lf",s);}fclose(rf);fclose(wf);}
3、8,23,28,……,93,98。符合此条件的累加值应为42,126,366,570,1010。#include#defineN20intfun(intn,int*a){inti,j,k,sum;/**************found**************/sum=j=0;for(k=3,i=0;i4、,i;printf("Entern(4doublefun(intn){inti,k=-1;doubles=0;for(i=1;i<=2*n;i++){k5、=-k;s=s+k*1.0/i;}returns;}main(){intn;doubles;printf("Inputn:");scanf("%d",&n);s=fun(n);printf("s=%f",s);NONO();}NONO(){/*请在此函数内打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/FILE*rf,*wf;intn,i;doubles;rf=fopen("D:\Exam\24010001\in.dat","r");wf=fopen("D:\Exam\24010001\out.da6、t","w");for(i=0;i<10;i++){fscanf(rf,"%d",&n);s=fun(n);fprintf(wf,"%lf",s);}fclose(rf);fclose(wf);}
4、,i;printf("Entern(4doublefun(intn){inti,k=-1;doubles=0;for(i=1;i<=2*n;i++){k
5、=-k;s=s+k*1.0/i;}returns;}main(){intn;doubles;printf("Inputn:");scanf("%d",&n);s=fun(n);printf("s=%f",s);NONO();}NONO(){/*请在此函数内打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/FILE*rf,*wf;intn,i;doubles;rf=fopen("D:\Exam\24010001\in.dat","r");wf=fopen("D:\Exam\24010001\out.da
6、t","w");for(i=0;i<10;i++){fscanf(rf,"%d",&n);s=fun(n);fprintf(wf,"%lf",s);}fclose(rf);fclose(wf);}
此文档下载收益归作者所有