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