C语言程序设计课后习题答案(第四版)谭浩强 下载本文

printf(\ l=lcd(u,v,h); printf(\ return 0; }

int hcf(int u,int v) {int t,r; if (v>u)

{t=u;u=v;v=t;} while ((r=u%v)!=0) {u=v; v=r;} return(v); }

int lcd(int u,int v,int h) {

return(u*v/h); }

7-1-2

#include <> int Hcf,Lcd; int main()

{void hcf(int,int); void lcd(int,int); int u,v;

scanf(\ hcf(u,v); lcd(u,v);

printf(\ printf(\ return 0; }

void hcf(int u,int v) {int t,r; if (v>u)

{t=u;u=v;v=t;} while ((r=u%v)!=0) {u=v; v=r;

} Hcf=v; }

void lcd(int u,int v) {

Lcd=u*v/Hcf; } 7-2

#include <> #include <>

float x1,x2,disc,p,q; int main()

{void greater_than_zero(float,float); void equal_to_zero(float,float); void smaller_than_zero(float,float); float a,b,c;

printf(\

scanf(\

printf(\ disc=b*b-4*a*c; printf(\ if (disc>0) {

greater_than_zero(a,b);

printf(\ }

else if (disc==0)

{equal_to_zero(a,b);

printf(\ } else

{smaller_than_zero(a,b);

printf(\ }

return 0; }

void greater_than_zero(float a,float b) {x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); }

void equal_to_zero(float a,float b) {

x1=x2=(-b)/(2*a); }

void smaller_than_zero(float a,float b) {

p=-b/(2*a);

q=sqrt(-disc)/(2*a); } 7-3

#include <> int main()

{int prime(int); int n;

printf(\ scanf(\ if (prime(n))

printf(\ else

printf(\ return 0; }

int prime(int n) {int flag=1,i;

for (i=2;i

#include <> #define N 3 int array[N][N]; int main()

{ void convert(int array[][3]);

int i,j;

printf(\ for (i=0;i

scanf(\ printf(\ for (i=0;i

printf(\ printf(\ }

convert(array);

printf(\ for (i=0;i

printf(\ printf(\ }

return 0; }

void convert(int array[][3]) {int i,j,t;

for (i=0;i

array[i][j]=array[j][i]; array[j][i]=t; } }

#include <> #include <> int main()

{void inverse(char str[]); char str[100];

printf(\ scanf(\ inverse(str);

printf(\ return 0; }