《算法竞赛入门经典习一二章题答案.docx》由会员分享,可在线阅读,更多相关《算法竞赛入门经典习一二章题答案.docx(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、文档除了2-10是后来写的,其余都是当初零基础学习的时候写的.适合初学者.求int的上限与下限#include /运行时间长,请等待. int main() int min ,max; FILE *fin,*fout; fin=fopen(min of int.out,wb); fout=fopen(max of int.out,wb); for(min=-1;min0;) max+ ; fprintf(fout,%dn,max-1); fclose(fin); fclose(fout); return 0; 1-1#include int main() int a,b,c; scanf(%d
2、%d%d,&a,&b,&c); double average; average=(a+b+c)/3.0; /一定要将int型转为浮点型 printf(Average=%.3lf,average ); system(pause); return 0; 1-2#include int main() double f,c; printf(请输入华氏温度fn); scanf(%lf,&f); c=(f-32)*5/9 ; printf(摄氏温度c=%.3lfn,c); system(pause); return 0; 1-3#include int main() int n; scanf(%d,&n)
3、; printf(%dn,(1+n)*n/2) ; system(pause); return 0; 1-4#include #include int main() const double pi =4.0*atan(1.0); int n; scanf(%d,&n); while(n=360) printf(请输入小于360的角n); scanf(%d,&n); printf(正弦:%lfn余弦:%lf,sin(n*pi/180),cos(n*pi/180); system(pause); return 0; 1-5#include #include int main() double x1
4、,y1,x2,y2; printf(请输入点A的坐标n); scanf(%lf%lf,&x1,&y1); printf(请输入点B的坐标n); scanf(%lf%lf,&x2,&y2); double d; d=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); printf(%.3lfn,d); system(pause); return 0; 1-6#include int main() int a; scanf(%d,&a); if(a%2=0)printf(该数是偶数); else printf(该数非偶数); system(pause); return 0;
5、1-7#include int main() const int a=95; int n; printf(你要买多少件衣服n); scanf(%d,&n); if(a*n=300)printf(需要%.2lf元n,a*n*0.85); else printf(需要%.2lf元n,(double)a*n); /由于输出是小数%.2lf,故一定要将int型转化为浮点型 system(pause); return 0; 1-8#include #include int main() double a; scanf(%lf,&a); if(a0)printf(%.2lf,a); else printf
6、(%.2lf,-a); system(pause); return 0; 1-9(方法一)#include int main() int a,b,c,max,min,middle; scanf(%d%d%d,&a,&b,&c); while(a0|b0|cb)min=b;if(ac)min=c; max=a;if(ab)max=b;if(amax)printf(yes); else printf(no); system(pause); return 0; 1-9(方法二)#include int main() int a,b,c,t=0; scanf(%d%d%d,&a,&b,&c); whi
7、le(a0|b0|cb)t=a;a=b;b=t; if(ac)t=a;a=c;c=t; if(bc)t=b;b=c;c=t; if(a+bc)printf(yes); else printf(no); system(pause); return 0; 1-10#include int main() int n; scanf(%d,&n); if(n%4=0) if(n%100=0) if(n%400=0) printf(yes); else printf(no); else printf(yes); else printf(no); system(pause); return 0; 3n+1解
8、决篇1#include int main() int count=0;double i,m; scanf(%lf,&i); for(;i1;) m=i/2; if(floor(m+0.5)!=m)i=3*i+1;i/=2;count+=2;/floor(x)取x的整数部分. else i/=2;count+; printf(%dn,count); system(pause); return 0; 3n+1解决篇2#includeint main() long long n, count = 0;/long long 的取值范围:-263263-1 scanf(%I64d, &n); while
9、(n 1) if(n % 2 = 1) n = n*3+1; else n /= 2; count+; printf(%I64dn, count); return 0;数据统计解决篇#include int main() int x,n=0,s=0,min,max; while(scanf(%d,&x)=1) if(n=0)min=max=x;/读取第一个数的时候将第一个数赋值给min和max s+=x; if(xmax)max=x; n+; printf(%d %d %.3lfn,min,max,(double)s/n); system(pause); return 0; 2-1(fin)#
10、include int main() FILE *fin,*fout; fin=fopen(digit.in,rb); fout=fopen(digit.out,wb); /*fin=stdin; fout=stdout;*/ int a,i=0; fscanf(fin,%d,&a); while(1) a/=10; i+; if(a1)break; fprintf(fout,%drn,i); fclose(fin); fclose(fout); /system(pause); return 0;2-1(freopen)#include /#define LOCAL/在编译选项中定义LOCAL
11、 int main() #ifdef LOCAL freopen(digit.in,r,stdin); freopen(digit.out,w,stdout); #endif int a,i=0; scanf(%d,&a); while(1) a/=10; i+; if(a1)break; printf(%dn,i); return 0;2-2(freopen)#include /#define LOCAL/编译选项中定义 int main() #ifdef LOCAL freopen(daffodil.out,w,stdout); #endif int a,b,c,m; for(a=1;a=
12、9;a+) for(b=0;b=9;b+) for (c=0;c=9;c+) m=a*100+b*10+c; if(m=a*a*a+b*b*b+c*c*c)printf(%dn,m); /system(pause); return 0; 2-2(fin)#include int main() FILE *fout; fout=fopen(daffodil.out,wb); int a,b,c,m; for(a=1;a=9;a+) for(b=0;b=9;b+) for(c=0;c=9;c+) m=a*a*a+b*b*b+c*c*c; if(m=a*100+b*10+c) fprintf(fou
13、t,%drn,m); fclose(fout); return 0;2-3(fin)#includeint main() FILE *fin,*fout; fin=fopen(hanxin.in,rb); fout=fopen(hanxin.out,wb); /fin=stdin; /fout=stdout; int a,b,c,x,temp=0;/temp用来判断是否在10到100内存在这样的数 fscanf(fin,%d%d%d,&a,&b,&c); for(x=10;x=100;x+) if(x%3=a&x%5=b&x%7=c) fprintf(fout,%drn,x); temp=1;
14、 break; if(!temp)fprintf(fout,No answerrn); fclose(fin); fclose(fout); return 0;2-3(freopen)#includeint main() /会在编译选项中定义LOCAL #ifdef LOCAL freopen(hanxin.in,r,stdin); freopen(hanxin.out,w,stdout); #endif int a,b,c,x,temp=0; scanf(%d%d%d,&a,&b,&c); for(x=10;x=100;x+) if(x%3=a&x%5=b&x%7=c) printf(%dn
15、,x); temp=1; break; if(!temp)printf(No answern); return 0;2-4(fin)#includeint main() FILE *fin,*fout; fin=fopen(triangle.in,rb); fout=fopen(triangle.out,wb); /fin=stdin; /fout=stdout; int n,i,j,k; fscanf(fin,%drn,&n); for(i=1;i=n;i+) for(j=1;j=1;k-)fprintf(fout,*); fprintf(fout,rn); fclose(fin); fcl
16、ose(fout); return 0;2-4(freopen)#includeint main() /在编译选项内定义LOCAL #ifdef LOCAL freopen(triangle.in,r,stdin); freopen(triangle.out,w,stdout); #endif int n,i,j,k; scanf(%d,&n); for(i=1;i=n;i+) for(j=1;j=1;k-)printf(*); printf(n); return 0;2-5(fin)#includeint main() FILE *fin,*fout; fin=fopen(stat.in,r
17、b); int n,a,i,m,count=0; fscanf(fin,%d,&n); for(i=1;i=n+1;i+) fscanf(fin,%d,&a); if(i=n+1)m=a; fclose(fin); fin=fopen(stat.in,rb); for(i=0;i=n;i+) fscanf(fin,%d,&a); if(i!=0) if(am) count+; fclose(fin); fout=fopen(stat.out,wb); fprintf(fout,%drn,count); fclose(fout); return 0;2-5(freopen)#includeint
18、 main() freopen(stat.in,r,stdin); freopen(stat.out,w,stdout); int n,a,i,m,count=0; scanf(%d,&n); for(i=1;i=n+1;i+) scanf(%d,&a); if(i=n+1)m=a; freopen(stat.in,r,stdin); for(i=0;i=n;i+) scanf(%d,&a); if(i!=0) if(am) count+; printf(%dn,count); return 0;2-6(fin)#includeint main() FILE *fin,*fout; fin=f
19、open(harmony.in,rb); int n,i; double H=0; fscanf(fin,%d,&n); for(i=1;i=n;i+) H+=(double)1/i; fclose(fin); fout=fopen(harmony.out,wb); fprintf(fout,%.3lfrn,H); fclose(fout); return 0;2-6(freopen)#includeint main() #ifdef LOCAL freopen(harmony.in,r,stdin); freopen(harmony.out,w,stdout); #endif int n,i
20、; double H=0; scanf(%d,&n); for(i=1;i=n;i+) H=H+double/i; printf(%.3lf,H); return 0;2-7(fin)#includeint main() FILE *fout; int i; double H=0; for(i=1;2*i-1;i+) if(i%2=1)H+=(double)1/(2*i-1); else H-=(double)1/(2*i-1); fout=fopen(approximation.out,wb); fprintf(fout,%lfrn,H); return 0;2-7(freopen)#inc
21、ludeint main() #ifdef LOCAL freopen(approximation.in,r,stdin); freopen(approximation.out,w,stdout); #endif int i; double H=0; for(i=1;2*i-1;i+) if(i%2=1)H+=(double)1/(2*i-1); else H-=(double)1/(2*i-1); printf(%lf,H); return 0;2-8(fin,double)#include#includeint main() FILE *fin,*fout; fin=fopen(subse
22、quence.in,rb); fout=fopen(subsequence.out,wb); int n,m,i; double H=0; double ii; fscanf(fin,%d%d,&n,&m); for(i=n;i=m;i+) ii=(double)i*i; H+=1/ii; fprintf(fout,%.5lfrn,H); fprintf(fout,%.2lfrn,(double)clock()/CLOCKS_PER_SEC);/比较double和long long运行效率 fclose(fin); fclose(fout); return 0; 2-8(fin,long lo
23、ng)#include#includeint main() FILE *fin,*fout; fin=fopen(subsequence.in,rb); fout=fopen(subsequence.out,wb); int n,m,i; double H=0; fscanf(fin,%d%d,&n,&m); for(i=n;i=m;i+) long long ii=1;/定义 ii=ii*i*i; /不用ii=i*i也不是ii*=i*i,这样做是为了防止i*i溢出; 可以认为这一步将int型转化为long long 型 H+=1/(double)ii;/不是(double)1/ii fpri
24、ntf(fout,%.5lfrn,H); fprintf(fout,%.2lfrn,(double)clock()/CLOCKS_PER_SEC); /比较double和long long运行效率 fclose(fin); fclose(fout); return 0; 2-8(freopen)#include#define LOCALint main() #ifdef LOCAL freopen(subsequence.in,r,stdin); freopen(subsequence.out,w,stdout); #endif int n,m,i; double H=0,ii; scanf(
25、%d%d,&n,&m); for(i=n;i=m;i+) H+=1/(double)i*i); printf(%.5lfn,H); return 0;2-9(fin)#includeint main() FILE *fin,*fout; fin=fopen(decimal.in,rb); fout=fopen(decimal.out,wb); int a,b,c; fscanf(fin,%d%d%d,&a,&b,&c); fprintf(fout,%.*lfrn,c,(double)a/b); fclose(fin); fclose(fout); return 0;2-9(freopen)#i
26、ncludeint main() #ifdef LOCAL freopen(decimal.in,r,stdin); freopen(decimal.out,w,stdout); #endif int a,b,c; scanf(%d%d%d,&a,&b,&c); printf(%.*lf,c,(double)a/b); return 0;2-10(全书看完再看这段代码)#include#includeusing namespace std;int main()freopen(permutation.ans,w,stdout);int d=1,2,3,4,5,6,7,8,9;doint a=d0*100+d1*10+d2;int b=d3*100+d4*10+d5;int c=d6*100+d7*10+d8;if(c=3*a&b=2*a)printf(%d %d %dn,a,b,c);while(next_permutation(d,d+9);return 0;