資源描述:
《下列程序的能是將大于整數(shù)m且緊靠m的k個(gè)素?cái)?shù)存入.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、1、下列程序的功能是:將大于整數(shù)m且緊靠m的k個(gè)素?cái)?shù)存入數(shù)組xx。請(qǐng)編寫函數(shù)num(intm,intk,intxx[])實(shí)現(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、intm,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、1整除的數(shù).如果i%j等于0,說明i不是素?cá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、"%d",xx[m]);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、close(wf);}2、已知數(shù)據(jù)文件IN.DAT中存有200個(gè)四位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請(qǐng)考生編制一函數(shù)jsVal(),其功能是:如果四位數(shù)各位上的數(shù)字均是0或2或4或6或8,則統(tǒng)計(jì)出滿足此條件的個(gè)數(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:v
6、oidjsVal()/*標(biāo)準(zhǔn)答案*/{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、f(b[I]
8、;}writeDat(){FILE*fp;inti;fp=fopen("out.dat","w");fprintf(fp,"%d",cnt);for(i=0;i