《江苏科技大学C 实验报告.docx》由会员分享,可在线阅读,更多相关《江苏科技大学C 实验报告.docx(25页珍藏版)》请在第一文库网上搜索。
1、第一题:从键盘上输入一种整数nB值,按下时求出y8值,并输出n和yB值Y=I!+2!+3!+n!源程序清单:#inc1udevoidmain()(intt=1,n,y=0;coutn=7n;coutnn=n,n,;for(inti=1;i=n;i+)(t=t*i;y=y+t;)couty=yend1;运行成果:n=?25n=25y=71289909?Pressanykeytocontinue第二题:设计一种程序,求出10010000之间的所有素数。源程序清单:#inc1ude#inc1udevoidmain()(inti,n,count=0;for(n=101;n1000;n+)(for(i=
2、2;in-1)(coutsetw(4)n;count+;if(count10=0)coutndI;1coutendI;运行成果:10110310710911312713113713914915115716316717317918119119319719921122322722923323924125125?26326927127728128329330731131331733133734734935335936737337938338939?40140941942143143343944344945?46146346?47948749149950350952152354154?55?56356
3、9571577587593599601607613617619631641643647653659661673677683691701709719727733739743751757761769773787?978098118218238278298398538578598638778818838879079119199299379419479539679?1977983991997Press;an,ke/toCOntintie.第三题:设计一种程序,输入一种任意整数(广4位),求出各位数字之和,并记录出位数源程序清单:#incIudevoidmain()(intn,k,s=0,t=0;cou
4、tn=?end1;cinn;whi1e(n)(k=n%10;n=n10;s=s+k;t=t+1;)couts,t*tr。分别求出C(4,26C(6,4)、C(8,7)的值。规定用函数来实现求阶乘和组合数,在主函数中测试。源程序清单:#inc1ud#inc1udef1oatf(intn)if(n0)cout-n应为正数!n;f1oats;if(n=11n=0)return1;e1ses=n*f(-1);returns;f1oatc(intm,intr)if(mr)coutmr;coutncm,r)=c(m,r),n,;运行成果:42c=6Pressanykeytocontinue64c=15Pr
5、essanykeytocontinueB7c=8Pressanykeytocontinue第五题:设计一种程序,用筛选法求出3100之间的所有素数,规定每行输出5个素数。判断一种整数与否为素数应设计一种函数来实现。源程序清单:#inc1ude。OStream.h#inc1ude。omanip.hintfun(intn)(for(inti=2;in;i+)if(n%i=0)returnO;return1;Jvoidmain(intcount=0;for(inti=3;i100;i+)if(fun(i)(coutisetw(4);count+;if(count%5=0)cout*;1cout,;)
6、运行成果:35711131?192329313?41434753596167717379838997Pressany:key1tocontinue第六题:把二维数组a赋值给b,并把b中元素8值等于a中对应元素的平方。源程序清单:#incIudetinc1udevoidmain(inta34,b34,i,j;for(i=0;i3;i+)for(j=0;jaij;for(i=0;i3;i+)for(j=0;j4;j+)bij=aij*aij;for(i=0;i3;i+)(for(j=0;j4;j+)coutbijt;cout,*;运行成果:第七题:下面的程序求出IOOO以内的事生质数的对数(改错题
7、)。源程序清单:#inc1ude#inc1ude#inc1udeintisprime(intx)(intk9i;k=(int)sqrt(x);for(i=2;i=k;i+)if(x%i=0)return(0);return(1);voidmain()(inta500,count,k=0;intb352;for(inti=3;i1000;i+=2)if(isprime(i)ak+=i;count=0;for(i=0;ik-1;i+)(if(ai+1ai=2)(bcount0=ai;bcount+1=ai+1;Jfor(i=0;icount;i+)(if(i5=0)cout,n,;cout,(,s
8、etw(3)bi0,setw(3)bi1,),t,;coutendIcount=count:41,43137,139)227,229:347,349)431,433)461,463)569,571641,643)809,811857,859):ount=35,ressanykeytocontinue.第八题:有两个字符串,将主字符串中的所有子字符串删去并输出,例如,主字符串为“Thisisaverygoodbook”,子字符串为book,成果为Thisisaverygood”。源程序清单:#inc1ude#inc1udeintwithstr(char*s1,char*subs)(whiIe(*
9、subs&*s1=*subs)s1+;subs+;return!(*subs);char*de1sub(char*s1,char*subs)(char*p1,*p2;intIen=StrIen(subs);p2=p1=s1;whiI(*p1)if(withstr(p1,subs)p1=1en;e1se*p2+=*p1+;*p2=,0,;returns1;1voidmain()(charstr180=Thisisaverygoodbook;charstr240=book;coutstr1endIstr2endI;coutintfun(char*str,char*sear,int&n)n=0char*s,*f;for(;*str;str+)if(*str=*searI*sear)s=str;f=sear;s+;f+;if(*f=)if(*(f+D=0&*s!=10,)n+;e1ses+;f+;whi1e(*s+=*f+)if(*f=0,)n+;break;)1returnn;)voidmaintintn;charstr81,s010;cout“请输入被查询的字符:nw;cin.getIine(str,80);cout“请输入要查询的字符:nM;cin.getIine(s,9);if(fun(str,s,n)coutsO“出现了n次J;e1secoutsO“没