最新算法竞赛入门经典第二版习题答案资料.docx
《最新算法竞赛入门经典第二版习题答案资料.docx》由会员分享,可在线阅读,更多相关《最新算法竞赛入门经典第二版习题答案资料.docx(22页珍藏版)》请在冰豆网上搜索。
![最新算法竞赛入门经典第二版习题答案资料.docx](https://file1.bdocx.com/fileroot1/2022-10/27/fb75a26a-bb0a-4720-9a32-5db39c6724f7/fb75a26a-bb0a-4720-9a32-5db39c6724f71.gif)
最新算法竞赛入门经典第二版习题答案资料
求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(xif(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