您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据挖掘与识别 > 算法竞赛入门经典各章习题答案
第一章习题1-1#includestdio.hintmain(){inta,b,c;doubled;scanf(%d%d%d,&a,&b,&c);d=(double)(a+b+c);printf(%.3lf\n,d/3.0);return0;}习题1-2#includestdio.hintmain(){intf;doublec;scanf(%d,&f);c=5*(f-32)/9;printf(%.3lf\n,c);return0;}习题1-3#includestdio.hintmain(){intn;scanf(%d,&n);printf(%d\n,(n*(1+n))/2);return0;}习题1-4#includestdio.h#includemath.h#definepi4.0*atan(1.0)intmain(){intn;scanf(%d,&n);printf(%lf\n,sin((pi*n)/180));printf(%lf\n,cos((pi*n)/180));return0;}习题1-5#includestdio.hintmain(){doublex1,y1,x2,y2,a;scanf(%lf%lf%lf%lf,&x1,&y1,&x2,&y2);a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));printf(%lf\n,a);return0;}习题1-6#includestdio.hintmain(){intn;scanf(%d,&n);if(n%2==0){printf(YES\n);}else{printf(NO\n);}return0;}习题1-7#includestdio.hintmain(){intn;doublea;scanf(%d,&n);a=n*95.0;if(a300){printf(%.2lf\n,a);}else{printf(%.2lf\n,a*0.85);}return0;}习题1-8#includestdio.h#includemath.hintmain(){doublen;scanf(%lf,&n);printf(%.2lf,fabs(n));return0;}习题1-9#includestdio.hintmain(){inta,b,c;scanf(%d%d%d,&a,&b,&c);if(a==b&&b==c){printf(no\n);}if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a)){printf(yes\n);}else{printf(no\n);}return0;}习题1-10#includestdio.hintmain(){intn;scanf(%d,&n);if(n%4==0){if(n%100!=0){printf(no\n);}else{if(n%400==0){printf(yes\n);}else{printf(no\n);}}}else{printf(no\n);}return0;}第二章习题2-1#includestdio.hintmain(){intn,count=0;scanf(%d,&n);while(n0){count++;n=n/10;}printf(%d\n,count);return0;}习题2-2#includestdio.hintmain(){inta,b,c;for(inti=100;i=999;i++){a=i%10;b=i/10%10;c=i/100;if(i==a*a*a+b*b*b+c*c*c){printf(%d\n,i);}}return0;}习题2-3#includestdio.hintmain(){inti,a,b,c;scanf(%d%d%d,&a,&b,&c);for(i=10;i=100;i++){if(i%3==a&&i%5==b&&i%7==c){printf(%d\n,i);}}if(i==101){printf(noanswer\n);}return0;}习题2-4#includestdio.hintmain(){inti,j,k,n;scanf(%d,&n);for(i=n;i0;i--){for(k=0;kn-i;k++){printf();}for(j=0;j2*i-1;j++){printf(#);}printf(\n);}return0;}习题2-5文件题,南邮竞赛基本不涉及。。。习题2-6#includestdio.hintmain(){inti,n;doublesum=1.0;scanf(%d,&n);for(i=2;i=n;i++){sum+=(1.0/i);}printf(%.3lf\n,sum);return0;}习题2-7#includestdio.h#includemath.hintmain(){intt=-1;doublea=1.0,sum=1.0;while(fabs(a)=0.000001){a=1.0/(a+2);a=a*t;sum=sum+a;t=t*(-1);}printf(%.9lf\n,sum);return0;}习题2-8#includestdio.hintmain(){inti,n,m,temp;doublesum=0;scanf(%d%d,&n,&m);if(nm){temp=n;n=m;m=temp;}for(i=n;i=m;i++){sum=sum+(1.0/i/i);}printf(%.5lf\n,sum);return0;}习题2-9printf的特殊用法:对于m.n的格式可以用如下方法表示charch[20];printf(%*.*s\n,m,n,ch);前边的*定义的是总的宽度,后边的定义的是输出的个数。分别对应外面的参数m和n。这种方法的好处是可以在语句之外对参数m和n赋值,从而控制输出格式。#includestdio.hintmain(){inta,b,c;scanf(%d%d%d,&a,&b,&c);printf(%.*lf\n,c,(double)a/b);return0;}习题2-10#includestdio.hintmain(){inta,b,c,d,e,f,g,h,i;for(a=1;a=9;a++){for(b=1;b=9;b++){for(c=1;c=9;c++){for(d=1;d=9;d++){for(e=1;e=9;e++){for(f=1;f=9;f++){for(g=1;g=9;g++){for(h=1;h=9;h++){for(i=1;i=9;i++){if((2*(a*100+b*10+c)==1*(d*100+e*10+f))&&(3*(a*100+b*10+c)==1*(g*100+h*10+i))&&(a!=b)&&(a!=c)&&(a!=d)&&(a!=e)&&(a!=f)&&(a!=g)&&(a!=h)&&(a!=i)&&(b!=c)&&(b!=d)&&(b!=e)&&(b!=f)&&(b!=g)&&(b!=h)&&(b!=i)&&(c!=d)&&(c!=e)&&(c!=f)&&(c!=g)&&(c!=h)&&(c!=i)&&(d!=e)&&(d!=f)&&(d!=g)&&(d!=h)&&(d!=i)&&(e!=f)&&(e!=g)&&(e!=h)&&(e!=i)&&(f!=g)&&(f!=h)&&(f!=i)&&(g!=h)&&(g!=i)&&(h!=i)){printf(%d,%d,%d\n,a*100+b*10+c,d*100+e*10+f,g*100+h*10+i);}}}}}}}}}}return0;}第三章习题3-1#includestdio.h#includestring.hintmain(){inti,a[101],n,max;memset(a,sizeof(a),0);while(scanf(%d,&n)==1){a[n]++;}max=a[0];for(i=1;i101;i++){if(a[i]=max){max=a[i];}}for(i=0;i101;i++){if(a[i]==max){printf(%d,i);}}printf(\n);return0;}习题3-2#includestdio.h#includectype.hchars[1000];intmain(){charch;inti,j,m=0,sumlong=0,count=0;while(1){scanf(%c,&ch);if(ch=='\n'||ch==EOF){break;}else{s[m++]=ch;}}for(i=m-1;i0;i--){if(s[i]==''&&s[i-1]==''){for(j=i-1;jm-1;j++){s[j]=s[j+1];}m--;}}for(i=0;im;i++){if(isalpha(s[i])){sumlong++;}elseif(s[i]==''){count++;}}printf(%.2lf\n,(double)((sumlong+count+1)/(count+1)));return0;}习题3-3(重新做!!!)#includestdio.h#includectype.h#definemod1000chars[10000];charbuf[10000];intmain(){inti,m=0,n=0,sum=1;charch;while(1){scanf(%c,&ch);if(ch==EOF||ch=='\n'){break;}else{s[m++]=ch;}}for(i=0;im;i++){if(isdigit(s[i])){buf[n++]=s[i]-'0';}}for(i=0;in;i++){sum=sum*buf[i];}printf(%d\n,sum%mod);return0;}习题3-4习题3-5#includestdio.hchara[1000][1000];intmain(){inti,j,n;scanf(%d,&n);scanf();chartemp;for(i=0;in;i++){for(j=0;jn;j++){scanf(%c,&a[i][j]);}}for(i=0;in;i++){for(j=i;jn;j++){temp=a[i][j];a[i][j]=a[j][i];a[j][i]=temp;}}for(i=n-1;i=0;i--){for(j=0;jn;j++){printf(%c,a[i][j]);}printf(\n);}return0;}习题3-6#includestdio.hintmain(){intb,n,m=0;charp[100];scanf(%d%d,&b,&n);while(b!=0){p[m++]=b%n+'0';b=b/n;}for(inti=m-1;i=0;i--){printf(%c,p[i]);}printf(\n);return0;}习题3-7#includestdio.hintndjc(intn,intm){inti,a=1;for(i=0;in;i++){a*=m;}returna;}intmain(){inti,b,n,p[100],m=0,a=0;scanf(%d%d,&n,&b);while(n0){p[m++]=n%10;n=n/10;}m--;for(i=m;i=0;i--){a+=p[i]*ndjc(i,b);}printf(%d\n,a);return0;}习题3-8
本文标题:算法竞赛入门经典各章习题答案
链接地址:https://www.777doc.com/doc-6444738 .html