《算法竞赛入门经典各章习题答案(共15页).docx》由会员分享,可在线阅读,更多相关《算法竞赛入门经典各章习题答案(共15页).docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上第一章习题1-1#include int main()int a,b,c;double d;scanf(%d%d%d,&a,&b,&c);d=(double)(a+b+c);printf(%.3lfn,d/3.0);return 0;习题1-2#include int main()int f;double c;scanf(%d,&f);c=5*(f-32)/9;printf(%.3lfn,c);return 0;习题1-3#include int main()int n;scanf(%d,&n);printf(%dn,(n*(1+n)/2);return 0;习题1-
2、4#include #include #define pi 4.0*atan(1.0)int main()int n;scanf(%d,&n);printf(%lfn,sin(pi*n)/180);printf(%lfn,cos(pi*n)/180);return 0;习题1-5#include int main()double x1,y1,x2,y2,a;scanf(%lf %lf %lf %lf,&x1,&y1,&x2,&y2);a=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);printf(%lfn,a);return 0;习题1-6#include int m
3、ain()int n;scanf(%d,&n);if(n%2=0)printf(YESn);elseprintf(NOn);return 0;习题1-7#include int main()int n;double a;scanf(%d,&n);a=n*95.0;if(a300)printf(%.2lfn,a);elseprintf(%.2lfn,a*0.85);return 0;习题1-8#include #include int main()double n;scanf(%lf,&n);printf(%.2lf,fabs(n);return 0;习题1-9#include int main
4、()int a,b,c;scanf(%d%d%d,&a,&b,&c);if(a=b&b=c)printf(non);if(a*a+b*b=c*c)|(a*a+c*c=b*b)|(b*b+c*c=a*a)printf(yesn);elseprintf(non);return 0;习题1-10#include int main()int n;scanf(%d,&n);if(n%4=0)if(n%100!=0)printf(non);elseif(n%400=0)printf(yesn);elseprintf(non);elseprintf(non);return 0;第二章习题2-1#includ
5、e int main()int n,count=0;scanf(%d,&n);while(n0)count+;n=n/10;printf(%dn,count);return 0;习题2-2#include int main()int a,b,c;for(int i=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(%dn,i);return 0;习题2-3#include int main()int i,a,b,c;scanf(%d%d%d,&a,&b,&c);for(i=10;i=100;i+)if(i%3
6、=a&i%5=b&i%7=c)printf(%dn,i);if(i=101)printf(no answern);return 0;习题2-4#include int main()int i,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);return 0;习题2-5文件题,南邮竞赛基本不涉及。习题2-6#include int main()int i,n;double sum=1.0;scanf(%d,&n);for(i=2;i=n;i+)sum+
7、=(1.0/i);printf(%.3lfn,sum);return 0;习题2-7#include #include int main()int t=-1;double a=1.0,sum=1.0;while(fabs(a)=0.)a=1.0/(a+2);a=a*t;sum=sum+a;t=t*(-1);printf(%.9lfn,sum);return 0;习题2-8#include int main()int i,n,m,temp;double sum=0;scanf(%d%d,&n,&m);if(nm)temp=n;n=m;m=temp;for(i=n;i=m;i+)sum=sum+(
8、1.0/i/i);printf(%.5lfn,sum);return 0;习题2-9printf的特殊用法:对于m.n的格式可以用如下方法表示 char ch20; printf(%*.*sn,m,n,ch); 前边的*定义的是总的宽度,后边的定义的是输出的个数。分别对应外面的参数m和n 。 这种方法的好处是可以在语句之外对参数m和n赋值,从而控制输出格式。 #include int main() int a,b,c;scanf(%d%d%d,&a,&b,&c);printf(%.*lfn,c,(double)a/b);return 0;习题2-10#include int main() in
9、t a,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!
10、=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,%dn,a*100+b*10+c,d*100+e*10+f,g*100+h*10+i);return 0; 第三章习题3-1#include #include int main()int i,a101,n,max;me
11、mset(a,sizeof(a),0);while(scanf(%d,&n)=1)an+;max=a0;for(i=1;i=max)max=ai;for(i=0;i101;i+)if(ai=max)printf(%d ,i);printf(n);return 0;习题3-2#include #include char s1000;int main()char ch;int i,j,m=0,sumlong=0,count=0;while(1)scanf(%c,&ch);if(ch=n|ch=EOF)break;elsesm+=ch;for(i=m-1;i0;i-)if(si= &si-1= )f
12、or(j=i-1;jm-1;j+)sj=sj+1;m-;for(i=0;im;i+)if(isalpha(si)sumlong+;else if(si= )count+;printf(%.2lfn,(double)(sumlong+count+1)/(count+1);return 0;习题3-3(重新做!)#include #include #define mod 1000char s10000;char buf10000;int main()int i,m=0,n=0,sum=1;char ch;while(1)scanf(%c,&ch);if(ch=EOF|ch=n)break;else
13、 sm+=ch;for(i=0;im;i+)if(isdigit(si)bufn+=si-0;for(i=0;in;i+)sum=sum*bufi;printf(%dn,sum%mod);return 0;习题3-4习题3-5#include char a10001000;int main()int i,j,n;scanf(%d,&n);scanf( );char temp;for(i=0;in;i+)for(j=0;jn;j+)scanf(%c,&aij);for(i=0;in;i+)for(j=i;j=0;i-)for(j=0;jn;j+)printf(%c ,aij);printf(n); return 0;习题3-6#include int main()int b,n,m=0;char p100;scanf(%d%d,&b,&n);while(b!=0)pm+=b%n+0;b=b/n;for(int i=m-1;i=0;i-)printf(%c,pi);printf(n);return 0;习题3-7#include int ndjc(int n,int m)int i,a=1;for(i=0;i0)pm+=n%10;n=n/10;m-;for(i=m;i=0;i-)a+=pi*ndjc(i,b);printf(%dn,a);return 0;习题3-8专心-专注-专业