《C语言程序填空练习题(二).docx》由会员分享,可在线阅读,更多相关《C语言程序填空练习题(二).docx(8页珍藏版)》请在第壹文秘上搜索。
1、将下面的程序补充完整,以完成指定的功能。1 .尼科彻斯定理ThisprogramistoverifyTheoremofNicoqish.Thatisthecubeofanyintegercanberepresentedasthesumofsomecontinueoddnumbers.Forexample,83=512=57+59+61+63+65+67+69+71.includeusingnamespacestd;intmain()(intn,a,i;while(1)(coutn;if(n=O);/(D/输出等差数列,首项为a*a-a+1,公差为2,项数为na=n*n-n+1;coutn,*,
2、n*n,=n*n*n=,a;for(i=1;i+)/(2)cout+,;/(3)coutendl;)returnO;)2 .角谷猜想ThisprogramistoverifyJiaoguGuess.Thatisgivenanynaturalnumber,ifitisaneven,divides2,ifitisanodd,multiple3andadd1,theresultcontinuestobecalculatedanalogously.Aftersometimes,theresultisalways1.#includeusingnamespacestd;intmain()(intn,a,i
3、,cnt;while(1)(coutn;if(n=O)break;/(1)cnt=O;coutResultsofverification:n;doif(/(2)n=n*3+1;cout,StepNo.+cnt,(n-1)3*3+1=nendl;)else(n=2;coutStepNo.+cnt2*n72=nendl;)while();/(3)coutendl;)return0;)3 .四方定理ThisprogramistoverifyTheoremofFourSquares.Thatisallnaturalnumberscanberepresentedassumofnomorethan4squ
4、aresofthenumbers.e.g.,123=7*7+7*7+4*4+3*3.#includeusingnamespacestd;intmain()(inti,j,kJ,number;while(1)coutnumber;if(number=O);/(1)coutResultsofverification:n;for(i=1;i=number/2;i+)for0=O;j=i;j+)for(k=0;k=j;k+)for(l=0;l=k;l+)if()/(2)(coutnumber=i*i,+j*j+k*,k+l*endl;gotoexit;)exit:coutn;returnO;)4 .亲
5、密数Thisisaprogramtofindfriendlynumberspair.WhichmeansthesumofintegerAsallfactors(exceptA)equalstothesumofintegerBsallfactors(exceptB)#includeusingnamespacestd;intmain()(inta,i,b,n,m;coutn;coutnTherearefollowingfriendly-numberspairsmallerthannendl;for(a=1;an;a+)(for(;i=a/2;i+)/(1)if(!(a%i)b+=i;for(;i=
6、b/2;i+)/(2)if(!(b%i)m+=i;if(&ab)/(3)couta.,bm;)coutendl;return0;)5 .自守数Thisprogramwillfindtheautomorphicnumbers.Thedefinationofaautomorphicnumberis:themantissaofanaturalnumberssquareequalstoitself.e.g.,52=25,762=5776,93762=87909376.#includeusingnamespacestd;intmain()(intmul,number,k,kk;for(number=0;
7、number1OOOOjnumber+)(for(mul=number,k=1;k*=10);/(1)kk=k*10;mul=number*number%kk;if()/(2)coutnumber)coutendl;return0;)6 .特殊的四位数Thisprogramwillfindthefourfigureswhichhavethecharacteristicasfollows:abcd=(ab+cd)2.e.g.,3025=(30+25)*(30+25).#includeusingnamespacestd;intmain()intn,a,b;for(n=1000;n10000;n+)
8、(;/(1);/(2)if(a+b)*(a+b)=n)coutn,;)coutendl;return0;)7 .求方程COS(X)x=0的根Thisprogramistofindtherealrootoffunctioncos(x)-x=0.includeincludeusingnamespacestd;intmain()float0,x1=0.0;while(1)(; /(1); /(2)if(fabs(x-x1)1e-6)break;)coutTherealrootis,x1endl;return0;)8 .特殊的3位数ThisprogramistofindthePerfectSquare
9、Numbers.whichhave3digits,and2ofthemarethesame,e.g.100,121,144,225,400,441,484,676,900.include#includeusingnamespacestd;intmain()(inti,n,a,b,c;for(i=10;i=sqrt(1000);i+)(;/(1)a=n100;/(2)c=n%10;if(a=bHa=cb=c)coutn;)coutendl;return0;)9 .三重回文数ThisprogramistofindthePalindromeNumbers,whosesquareandcubicare
10、alsoPalindromeNumbers.includeusingnamespacestd;boolisPalindrome(intn)(intx,y=0;x=n;while(x!=0)(;/(1);/(2)returny=n;)intmain()(intm;for(m=11;m1000;m+)(if(isPalindrome(m)&isPalindrome(m*m)&isPalindrome(m*m*m)coutmm*mMm*m*mendl;)returnO;10 .SIXandNINEThisprogramistofindthenumbersofSIXandNINE,whichsatis
11、fytheformulaSIX+SIX+SIX=NINE+NINE,whereS,I,X,N,Estandfordigitsbetween0and9.#includeusingnamespacestd;intmain()inti,x,a,b,c,d;for(i=668;i=999;)/(1)(x=3*i2;a=i10%10;b=x100%10;c=x1000;d=x%10010;if()/(2)(coutixendl;)return0;)或includeusingnamespacestd;intmain()(ints,i,x,n,e,six,nine;for(s=1;s10;s+)for(i=
12、0;i10;i+)for(x=0;x10;x+)for(n=1;n10;n+)for(e=0;e10;e+)(six=;/(3)nine=;/(4)if()/(5)coutsix,nineendl;)return0;)11 .马克思手稿中的数学题ThisprogramistosolveaninterestingmathquestioninKarlMarxsmanuscript.TheProblemisasfollows:30personsspent50yuaninarestaurant,amonythem,eachmanspent3yuan,eachwomanspent2yuan,andeachchildspent1yuan.Thequestionishowmanymen,womenandchildrenarethere?#includeusingnamespacestd;intmain()(intx,y,z;coutMentWomentChildren,endl;for(x=0;x=10;x+)(;/(1);/(2)if()