算法竞赛入门经典第二版习题问题详解.docx
- 文档编号:6379190
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:22
- 大小:17.59KB
算法竞赛入门经典第二版习题问题详解.docx
《算法竞赛入门经典第二版习题问题详解.docx》由会员分享,可在线阅读,更多相关《算法竞赛入门经典第二版习题问题详解.docx(22页珍藏版)》请在冰豆网上搜索。
算法竞赛入门经典第二版习题问题详解
求int的上限与下限
#include
//运行时间长,请等待.
intmain()
{
intmin,max;
FILE*fin,*fout;
fin=fopen("minofint.out","wb");
fout=fopen("maxofint.out","wb");
for(min=-1;min<0;)
{
min--;
}
fprintf(fin,"%d\n",min+1);
for(max=1;max>0;)
{
max++;
}
fprintf(fout,"%d\n",max-1);
fclose(fin);
fclose(fout);
return0;
}
1-1
#include
intmain()
{
inta,b,c;
scanf("%d%d%d",&a,&b,&c);
doubleaverage;
average=(a+b+c)/3.0;//一定要将int型转为浮点型
printf("Average=%.3lf",average);
system("pause");
return0;
}
1-2
#include
intmain()
{
doublef,c;
printf("请输入华氏温度f\n");
scanf("%lf",&f);
c=(f-32)*5/9;
printf("摄氏温度c=%.3lf\n",c);
system("pause");
return0;
}
1-3
#include
intmain()
{
intn;
scanf("%d",&n);
printf("%d\n",(1+n)*n/2);
system("pause");
return0;
}
1-4
#include
#include
intmain()
{
constdoublepi=4.0*atan(1.0);
intn;
scanf("%d",&n);
while(n>=360)
{
printf("请输入小于360°的角\n");
scanf("%d",&n);
}
printf("正弦:
%lf\n余弦:
%lf",sin(n*pi/180),cos(n*pi/180));
system("pause");
return0;
}
1-5
#include
#include
intmain()
{
doublex1,y1,x2,y2;
printf("请输入点A的坐标\n");
scanf("%lf%lf",&x1,&y1);
printf("请输入点B的坐标\n");
scanf("%lf%lf",&x2,&y2);
doubled;
d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
printf("%.3lf\n",d);
system("pause");
return0;
}
1-6
#include
intmain()
{
inta;
scanf("%d",&a);
if(a%2==0)printf("该数是偶数");
elseprintf("该数非偶数");
system("pause");
return0;
}
1-7
#include
intmain()
{
constinta=95;
intn;
printf("你要买多少件衣服\n");
scanf("%d",&n);
if(a*n>=300)printf("需要%.2lf元\n",a*n*0.85);
elseprintf("需要%.2lf元\n",(double)a*n);//由于输出是小数%.2lf,故一定要将int型转化为浮点型
system("pause");
return0;
}
1-8
#include
#include
intmain()
{
doublea;
scanf("%lf",&a);
if(a>0)printf("%.2lf",a);
elseprintf("%.2lf",-a);
system("pause");
return0;
}
1-9(方法一)
#include
intmain()
{
inta,b,c,max,min,middle;
scanf("%d%d%d",&a,&b,&c);
while(a<0||b<0||c<0)
{
printf("三边必须都是大于零的正整数");
scanf("%d%d%d",&a,&b,&c);
}
min=a;if(a>b)min=b;if(a>c)min=c;
max=a;if(a
middle=a+b+c-min-max;
if(min+middle>max)printf("yes");
elseprintf("no");
system("pause");
return0;
}
1-9(方法二)
#include
intmain()
{
inta,b,c,t=0;
scanf("%d%d%d",&a,&b,&c);
while(a<0||b<0||c<0)
{
printf("三边必须都是大于零的正整数");
scanf("%d%d%d",&a,&b,&c);
}
if(a>b){t=a;a=b;b=t;}
if(a>c){t=a;a=c;c=t;}
if(b>c){t=b;b=c;c=t;}
if(a+b>c)printf("yes");
elseprintf("no");
system("pause");
return0;
}
1-10
#include
intmain()
{
intn;
scanf("%d",&n);
if(n%4==0)
{
if(n%100==0)
{
if(n%400==0)
{
printf("yes");
}
elseprintf("no");
}
elseprintf("yes");
}
elseprintf("no");
system("pause");
return0;
}
3n+1解决篇1
#include
intmain()
{
intcount=0;doublei,m;
scanf("%lf",&i);
for(;i>1;)
{
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("%d\n",count);
system("pause");
return0;
}
3n+1解决篇2
#include
intmain(){
longlongn,count=0;//longlong的取值范围:
-2^63~2^63-1
scanf("%I64d",&n);
while(n>1){
if(n%2==1)n=n*3+1;
elsen/=2;
count++;
}
printf("%I64d\n",count);
return0;
}
数据统计解决篇
#include
intmain()
{
intx,n=0,s=0,min,max;
while(scanf("%d",&x)==1)
{
if(n==0){min=max=x;}//读取第一个数的时候将第一个数赋值给min和max
s+=x;
if(x if(x>max)max=x; n++; } printf("%d%d%.3lf\n",min,max,(double)s/n); system("pause"); return0; } 2-1(fin) #include intmain() { FILE*fin,*fout; fin=fopen("digit.in","rb"); fout=fopen("digit.out","wb"); /*fin=stdin; fout=stdout;*/ inta,i=0; fscanf(fin,"%d",&a); while (1) { a/=10; i++; if(a<1)break; } fprintf(fout,"%d\r\n",i); fclose(fin); fclose(fout); //system("pause"); return0; } 2-1(freopen) #include //#defineLOCAL//在编译选项中定义LOCAL intmain() { #ifdefLOCAL freopen("digit.in","r",stdin); freopen("digit.out","w",stdout); #endif inta,i=0; scanf("%d",&a); while (1) { a/=10; i++; if(a<1)break; } printf("%d\n",i); return0; } 2-2(freopen) #include //#defineLOCAL//编译选项中定义 intmain() { #ifdefLOCAL freopen("daffodil.out","w",stdout); #endif inta,b,c,m; for(a=1;a<=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("%d\n",m); } } } //system("pause"); return0; } 2-2(fin) #include intmain() { FILE*fout; fout=fopen("daffodil.out","wb"); inta,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(fout,"%d\r\n",m); } } } fclose(fout); return0; } 2-3(fin) #include intmain() { FILE*fin,*fout; fin=fopen("hanxin.in","rb"); fout=fopen("hanxin.out","wb"); //fin=stdin; //fout=stdout; inta,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,"%d\r\n",x); temp=1; break; } } if(! temp)fprintf(fout,"Noanswer\r\n"); fclose(fin); fclose(fout); return0; } 2-3(freopen) #include intmain() { //会在编译选项中定义LOCAL #ifdefLOCAL freopen("hanxin.in","r",stdin); freopen("hanxin.out","w",stdout); #endif inta,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("%d\n",x); temp=1; break; } } if(! temp)printf("Noanswer\n"); return0; } 2-4(fin) #include intmain() { FILE*fin,*fout; fin=fopen("triangle.in","rb"); fout=fopen("triangle.out","wb"); //fin=stdin; //fout=stdout; intn,i,j,k; fscanf(fin,"%d\r\n",&n); for(i=1;i<=n;i++) { for(j=1;j for(k=-2*i+2*n+1;k>=1;k--)fprintf(fout,"*"); fprintf(fout,"\r\n"); } fclose(fin); fclose(fout); return0; } 2-4(freopen) #include intmain() { //在编译选项内定义LOCAL #ifdefLOCAL freopen("triangle.in","r",stdin); freopen("triangle.out","w",stdout); #endif intn,i,j,k; scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j for(k=2*n+1-2*i;k>=1;k--)printf("*"); printf("\n"); } return0; } 2-5(fin) #include intmain() { FILE*fin,*fout; fin=fopen("stat.in","rb"); intn,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(a count++; } } fclose(fin); fout=fopen("stat.out","wb"); fprintf(fout,"%d\r\n",count); fclose(fout); return0; } 2-5(freopen) #include intmain() { freopen("stat.in","r",stdin); freopen("stat.out","w",stdout); intn,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(a count++; } } printf("%d\n",count); return0; } 2-6(fin) #include intmain() { FILE*fin,*fout; fin=fopen("harmony.in","rb"); intn,i; doubleH=0; fscanf(fin,"%d",&n); for(i=1;i<=n;i++) { H+=(double)1/i; } fclose(fin); fout=fopen("harmony.out","wb"); fprintf(fout,"%.3lf\r\n",H); fclose(fout); return0; } 2-6(freopen) #include intmain() { #ifdefLOCAL freopen("harmony.in","r",stdin); freopen("harmony.out","w",stdout); #endif intn,i; doubleH=0; scanf("%d",&n); for(i=1;i<=n;i++) { H=H+double/i; } printf("%.3lf",H); return0; } 2-7(fin) #include intmain() { FILE*fout; inti; doubleH=0; for(i=1;2*i-1<1000000;i++) { if(i%2==1)H+=(double)1/(2*i-1); elseH-=(double)1/(2*i-1); } fout=fopen("approximation.out","wb"); fprintf(fout,"%lf\r\n",H); return0; } 2-7(freopen) #include intmain() { #ifdefLOCAL freopen("approximation.in","r",stdin); freopen("approximation.out","w",stdout); #endif inti; doubleH=0; for(i=1;2*i-1<1000000;i++) { if(i%2==1)H+=(double)1/(2*i-1); elseH-=(double)1/(2*i-1); } printf("%lf",H); return0; } 2-8(fin,double) #include #include intmain() { FILE*fin,*fout; fin=fopen("subsequence.in","rb"); fout=fopen("subsequence.out","wb"); intn,m,i; doubleH=0; doubleii; fscanf(fin,"%d%d",&n,&m); for(i=n;i<=m;i++) { ii=(double)i*i; H+=1/ii; } fprintf(fout,"%.5lf\r\n",H); fprintf(fout,"%.2lf\r\n",(double)clock()/CLOCKS_PER_SEC);//比较double和longlong运行效率 fclose(fin); fclose(fout); return0; } 2-8(fin,longlong) #include #include intmain() { FILE*fin,*fout; fin=fopen("subsequence.in","rb"); fout=fopen("subsequence.out","wb"); intn,m,i; doubleH=0; fscanf(fin,"%d%d",&n,&m); for(i=n;i<=m;i++) { longlongii=1;//定义 ii=ii*i*i;//不用ii=i*i也不是ii*=i*i,这样做是为了防止i*i溢出;可以认为这一步将int型转化为longlong型 H+=1/(double)ii;//不是(double)1/ii } fprintf(fout,"%.5lf\r\n",H); fprintf(fout,"%.2lf\r\n",(double)clock()/CLOCKS_PER_SEC);////比较double和longlong运行效率 fclose(fin); fclose(fout); return0; } 2-8(freopen) #include #defineLOCAL intmain() { #ifdefLOCAL freopen("subsequence.in","r",stdin); freopen("subsequence.out","w",stdout); #endif intn,m,i; doubleH=0,ii; scanf("%d%d",&n,&m); for(i=n;i<=m;i++) { H+=1/((double)i*i); } printf("%.5lf\n",H); return0; } 2-9(fin) #include intmain() { FI
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 竞赛 入门 经典 第二 习题 问题 详解
![提示](https://static.bdocx.com/images/bang_tan.gif)