算法竞赛入门经典各章第二版前4章课后习题答案.docx
《算法竞赛入门经典各章第二版前4章课后习题答案.docx》由会员分享,可在线阅读,更多相关《算法竞赛入门经典各章第二版前4章课后习题答案.docx(35页珍藏版)》请在冰豆网上搜索。
![算法竞赛入门经典各章第二版前4章课后习题答案.docx](https://file1.bdocx.com/fileroot1/2023-6/14/d35369be-31eb-4f1c-8ae5-a63731405160/d35369be-31eb-4f1c-8ae5-a637314051601.gif)
算法竞赛入门经典各章第二版前4章课后习题答案
第一章
习题1-1
#include
intmain()
{
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
#include
intmain()
{
intf;
doublec;
scanf("%d",&f);
c=5*(f-32)/9;
printf("%.3lf\n",c);
return0;
}
习题1-3
#include
intmain()
{
intn;
scanf("%d",&n);
printf("%d\n",(n*(1+n))/2);
return0;
}
习题1-4
#include
#include
#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
#include
intmain()
{
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
#include
intmain()
{
intn;
scanf("%d",&n);
if(n%2==0)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return0;
}
习题1-7
#include
intmain()
{
intn;
doublea;
scanf("%d",&n);
a=n*95.0;
if(a<300)
{
printf("%.2lf\n",a);
}
else
{
printf("%.2lf\n",a*0.85);
}
return0;
}
习题1-8
#include
#include
intmain()
{
doublen;
scanf("%lf",&n);
printf("%.2lf",fabs(n));
return0;
}
习题1-9
#include
intmain()
{
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
#include
intmain()
{
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
#include
intmain()
{
intn,count=0;
scanf("%d",&n);
while(n>0)
{
count++;
n=n/10;
}
printf("%d\n",count);
return0;
}
习题2-2
#include
intmain()
{
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
#include
intmain(){
inta,b,c,i,kase=1;
while(~scanf("%d%d%d",&a,&b,&c)){
for(i=10;i<=100;i++){
if(i%3==a&&i%5==b&&i%7==c)
printf("Case%d:
%d\n",kase++,i);
elseif(i=100)
printf("Case%d:
Noanswer\n",kase++);}
}
return0;
}
习题2-4
#include
intmain(){
intn;
while(~scanf("%d",&n)){
for(inti=n;i>=0;i--){
for(intj=n-i;j>0;j--)
printf("");
for(intj=2*i-1;j>0;j--)
printf("#");
printf("\n");
}
}
}
习题2-5
文件题,南邮竞赛基本不涉及。
。
。
习题2-6
#include
intmain()
{
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
#include
#include
intmain()
{
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
#include
intmain(){
intn,m,temp,kase=0;
while(~scanf("%d%d",&n,&m)){
doubles=0;
if(n>m){temp=n;n=m;m=temp;}
if(n==0&&m==0)break;
for(inti=n;i<=m;i++)
{
s+=1.0/i/i;}
printf("Case%d:
%.5f\n",++kase,s);}
return0;}
习题2-9
printf的特殊用法:
对于m.n的格式可以用如下方法表示
charch[20];
printf("%*.*s\n",m,n,ch);
前边的*定义的是总的宽度,后边的定义的是输出的个数。
分别对应外面的参数m和n。
这种方法的好处是可以在语句之外对参数m和n赋值,从而控制输出格式。
#include
intmain()
{
inta,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%.*lf\n",c,(double)a/b);
return0;
}
习题2-10
#include
#defineFOR(i)for(i=1;i<10;i++)
intmain(){
inta,b,c,d,e,f,g,h,i;
FOR(a){
FOR(b){
FOR(c){
FOR(d){
FOR(e){
FOR(f){
FOR(g){
FOR(h){
FOR(i){
if(2*(a*100+b*10+c)==(d*100+e*10+f)&&3*(a*100+b*10+c)==(g*100+h*10+i)){
if((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!
=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\n%d\n%d\n",a*100+b*10+c,d*100+e*10+f,g*100+h*10+i);}}}}}}}}}}}}
第三章
习题3-1
#include
#include
intmain()
{
intnum[80];
charstr[81];
intt;
scanf("%d",&t);
while(t--)
{
intsum=0;
scanf("%s",str);
str[0]=='O'?
num[0]=1:
num[0]=0;
for(inti=1;i{
str[i]=='O'?
num[i]=num[i-1]+1:
num[i]=0;
sum+=num[i];
}
printf("%d\n",sum+num[0]);
}
}
习题3-2
#include
#include
#include
doubleM(charx)
{
if(x=='C')
return12.01;
if(x=='H')
return1.008;
if(x=='O')
return16.00;
if(x=='N')
return14.01;
}
intmain()
{
charstr[100];
intN,i,j;
doublesum;
scanf("%d",&N);
while(N--)
{
scanf("%s",str);
sum=0;
for(i=0;i{
if(isalpha(str[i]))
{
if(str[i+1]<='9'&&str[i+1]>='1')
{
if(str[i+2]<='9'&&str[i+2]>='1')
{
sum=sum+M(str[i])*((str[i+1]-'0')*10+str[i+2]-'0');
i=i+2;
}
else
{
sum=sum+M(str[i])*(str[i+1]-'0');
i++;
}
}
else
{
sum+=M(str[i]);
}
}
}
printf("%.3lf\n",sum);
}
}
习题3-3
#include
#include
chars[1000000];
inta[10000];
intmain(){
while(~scanf("%s",s)){
intb[10]={};
for(inti=0;i<10000;i++){
b[s[i]-'0']++;
}
for(inti=0;i<9;i++)printf("%d",b[i]);
printf("%d\n",b[9]);
}
return0;
}
习题3-4
#include
#include
chars[85];
intmain()
{
while(~scanf("%s",s)){
intlen=strlen(s);
for(inti=1;i<=len;++i)
{
if(len%i==0)
{
intk;
for(k=1;k<=len;++k)
{
if(s[k]!
=s[k%i])
break;
}
if(k==len)
{printf("%d\n",i);break;}
}
}
}
}
习题3-5
#include
#include
constintLEN=5;
constintMAX=100;
constinty[]={0,0,1,-1};
constintx[]={-1,1,0,0};
charmap[LEN][LEN];
inttra[110];
boollegal(intpos){
return0<=pos&&pos}
voidPmap(){
for(intcow=0;cow{
printf("%c",map[cow][0]);
for(intcol=1;colprintf("%c",map[cow][col]);
printf("\n");
}
}
intmain(){
tra['A']=0;
tra['B']=1;
tra['R']=2;
tra['L']=3;
boolfirst=true;
intCase=0;
intbx,by;
while(gets(map[0])){
if(map[0][0]=='Z')break;
for(intcol=1;colgets(map[col]);
for(inti=0;ifor(intj=0;jif(map[i][j]==''){
bx=i;by=j;
}
boolok=true;
charc;
while(scanf("%c",&c),c!
='0'){
if(!
ok)continue;
intnx=bx+x[tra[c]],ny=by+y[tra[c]];
if(!
legal(nx)||!
legal(ny)){
ok=false;
continue;
}
map[bx][by]=map[nx][ny];
map[nx][ny]='';
bx=nx;by=ny;
}
getchar();
if(first)
first=false;
else
printf("\n");
printf("Puzzle#%d:
\n",++Case);
if(ok)
Pmap();
else
printf("Thispuzzlehasnofinalconfiguration./n");
}
return0;
}
习题3-6
#include
intfirst=1;
charmap[12][12];
structpoint
{
intx,y;
intr,c;
}str[111];
intmain()
{
intr,c;
while(~scanf("%d%d",&r,&c),r,c)
{
for(inti=0;iscanf("%s",map[i]);
intnum=0;
for(inti=0;ifor(intj=0;j{
if(map[i][j]!
='*'){
if(map[i][j-1]=='*'||j-1<0)
{str[num].y=j;
str[num].x=i;
str[num].r=1;
num++;
}
elsestr[num].r=0;
if(map[i-1][j]=='*'||i-1<0)
{
str[num].x=i;
str[num].y=j;
str[num].c=1;
num++;
}
elsestr[num].c=0;
}
}
if(first)
first=0;
else
printf("\n");
printf("Across:
\n");
for(inti=0;i{
if(str[i].r)
{
for(intj=str[i].y;j{
if(map[str[i].x][j]=='*')break;
printf("%c",map[str[i].x][j]);
}
printf("\n");
}
}
printf("Down:
\n");
for(inti=0;i{
if(str[i].c)
{
for(intj=str[i].x;j{
if(map[j][str[i].y]=='*')break;
printf("%c",map[j][str[i].y]);
}
printf("\n");
}
}
}
}
习题3-7
#include
#include
constintN=1005;
constintM=105;
intfirst=1;
intn,m,cnt[M];
chardna[M][N];
voidinit(){
scanf("%d%d",&m,&n);
for(inti=0;i}
voidsolve(){
intans=0;
if(first)
first=0;
elseprintf("\n");
for(inti=0;iintMax=0,id;
memset(cnt,0,sizeof(cnt));
for(intj=0;jinttmp=dna[j][i];
cnt[tmp]++;
if(cnt[tmp]>Max){
Max=cnt[tmp];
id=tmp;
}elseif(cnt[tmp]==Max&&tmpid=tmp;
}
ans+=m-Max;
printf("%c",id);
}
printf("\n%d",ans);
}
intmain(){
intcas;
scanf("%d",&cas);
while(cas--){
init();
solve();
}
return0;
}
习题3-8
#include
#include
usingnamespacestd;
constintN=3005;
inta[N],v[N];
intmain()
{
intn,m,cnt;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
memset(v,0,sizeof(v));
printf("%d/%d=%d.",n,m,n/m);
n=n%m;
while(!
v[n])
{
a[++cnt]=(n*10)/m;
v[n]=cnt;
n=n*10%m;
}
for(inti=1;i<=cnt&&i<51;++i)
{
if(i==v[n])printf("(");
printf("%d",a[i]);
if(i==50)printf("...");}
printf(")\n%d=numberofdigitsinrepeatingcycle\n\n",cnt-v[n]+1);
}
return0;
}
习题3-9
#include
#include
#include
#defineSIZE1100000
usingnamespacestd;
charsub[SIZE],base[SIZE];
intmain()
{
while(scanf("%s%s",base,sub)!
=EOF)
{
intlen=strlen(sub);
intbase_len=strlen(base);
intj=0;
if(len>base_len)
{
printf("No\n");
continue;
}
for(inti=0;i{
if(sub[j]==base[i])j++;
if(j>=len)break;
}
if(j>=len)printf("Yes\n");
elseprintf("No\n");
}
return0;
}
习题3-10
#include
#include
#include
#include
usingnamespacestd;
structnode
{
inth,w;
booloperator<(constnode&r)co