return0;
}
19.4-1
#include
usingnamespacestd;
intmain()
{inthcf(int,int);
intlcd(int,int,int);
intu,v,h,l;
cin>>u>>v;
h=hcf(u,v);
cout<<"H.C.F="<l=lcd(u,v,h);
cout<<"L.C.D="<return0;
}
inthcf(intu,intv)
{intt,r;
if(v>u)
{t=u;u=v;v=t;}
while((r=u%v)!
=0)
{u=v;
v=r;}
return(v);
}
intlcd(intu,intv,inth)
{return(u*v/h);
}
20.4-2
#include
#include
usingnamespacestd;
floatx1,x2,disc,p,q;
intmain()
{voidgreater_than_zero(float,float);
voidequal_to_zero(float,float);
voidsmaller_than_zero(float,float);
floata,b,c;
cout<<"inputa,b,c:
";
cin>>a>>b>>c;
disc=b*b-4*a*c;
cout<<"root:
"<if(disc>0)
{
greater_than_zero(a,b);
cout<<"x1="<}
elseif(disc==0)
{equal_to_zero(a,b);
cout<<"x1="<}
else
{smaller_than_zero(a,b);
cout<<"x1="<
cout<<"x2="<
}
return0;
}
voidgreater_than_zero(floata,floatb)/*定义一个函数,用来求disc>0时方程的根*/
{x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
}
voidequal_to_zero(floata,floatb)/*定义一个函数,用来求disc=0时方程的根*/
{
x1=x2=(-b)/(2*a);
}
voidsmaller_than_zero(floata,floatb)/*定义一个函数,用来求disc<0时方程的根*/
{
p=-b/(2*a);
q=sqrt(-disc)/(2*a);
}
21.4-3
#include
usingnamespacestd;
intmain()
{intprime(int);/*函数原型声明*/
intn;
cout<<"inputaninteger:
";
cin>>n;
if(prime(n))
cout<else
cout<return0;
}
intprime(intn)
{intflag=1,i;
for(i=2;iif(n%i==0)
flag=0;
return(flag);
}
22.4-4
#include
usingnamespacestd;
intmain()
{intfac(int);
inta,b,c,sum=0;
cout<<"entera,b,c:
";
cin>>a>>b>>c;
sum=sum+fac(a)+fac(b)+fac(c);
cout<+"<
+"<