資源描述:
《下列程序的功能是將大于整數(shù)m且緊靠m的k個素數(shù)存入》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、1、下列程序的功能是:將大于整數(shù)m且緊靠m的k個素數(shù)存入數(shù)組xx。請編寫函數(shù)num(intm,intk,intxx[])實現(xiàn)函數(shù)的要求,最后調(diào)用函數(shù)readwriteDAT()把結(jié)果輸出到文件out.dat中。例如:若輸入17,5,則應(yīng)輸出:19,23,29,31,37。#include#includevoidreadwriteDAT();intisP(intm){inti;for(i=2;i2、m,intk,intxx[]){intdata=m+1;inthalf,n=0,I;while(1){half=data/2;for(I=2;I<=half;I++)if(data%I==0)break;if(I>half){xx[n]=data;n++;}if(n>=k)break;data++;}}或者:voidnum(intm,intk,intxx[]){inti,j,s=0;for(i=m+1;k>0;i++){for(j=2;j
3、如果i%j等于0,說明i不是素數(shù),跳出本層循環(huán)*/if(i==j){xx[s++]=i;k--;}}}voidnum(intm,intk,intxx[]){inti=0;for(m=m+1;k>0;m++)if(isP(m)){xx[i++]=m;k--;}}main(){intm,n,xx[1000];clrscr();printf("Pleaseentertwointegers:");scanf("%d,%d",&m,&n);num(m,n,xx);for(m=0;m4、]);printf("");readwriteDAT();}voidreadwriteDAT(){intm,n,xx[1000],i;FILE*rf,*wf;rf=fopen("in.dat","r");wf=fopen("out.dat","w");for(i=0;i<10;i++){fscanf(rf,"%d%d",&m,&n);num(m,n,xx);for(m=0;m5、、已知數(shù)據(jù)文件IN.DAT中存有200個四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請考生編制一函數(shù)jsVal(),其功能是:如果四位數(shù)各位上的數(shù)字均是0或2或4或6或8,則統(tǒng)計出滿足此條件的個數(shù)cnt,并把這些四位數(shù)按從大到小的順序存入數(shù)組b中。最后main()函數(shù)調(diào)用寫函數(shù)writeDat()把結(jié)果cnt以及數(shù)組b中符合條件的四位數(shù)輸出到OUT.DAT文件中。#include#defineMAX200inta[MAX],b[MAX],cnt=0;2:voidjsVal()/*標(biāo)準(zhǔn)答
6、案*/{intbb[4];intI,j,k,flag;for(I=0;I<200;I++){bb[0]=a[I]/1000;bb[1]=a[I]%1000/100;bb[2]=a[I]%100/10;bb[3]=a[I]%10;for(j=0;j<4;j++){if(bb[j]%2==0)flag=1;else{flag=0;break;}}if(flag==1){b[cnt]=a[I];cnt++;}}for(I=0;I7、];b[I]=b[j];b[j]=k;}}voidreadDat(){inti;FILE*fp;fp=fopen("in.dat","r");for(i=0;i8、inti;fp=fopen("out.dat","w");fprintf(fp,"%d",cnt);for(i=0;i