完整word版,《C语言程序设计》课后习题答案(第四版)谭浩强

m=r; }

printf(\它们的最大公约数为:%d\\n\ printf(\它们的最小公约数为:%d\\n\ return 0; } 5-4

#include int main() {

char c;

int letters=0,space=0,digit=0,other=0; printf(\请输入一行字符:\\n\ while((c=getchar())!='\\n') {

if (c>='a' && c<='z' || c>='A' && c<='Z') letters++; else if (c==' ') space++;

else if (c>='0' && c<='9') digit++; else other++; }

printf(\字母数:%d\\n空格数:%d\\n数字数:%d\\n其它字符数:%d\\n\ return 0; } 5-5

#include int main() {

int a,n,i=1,sn=0,tn=0; printf(\

scanf(\ while (i<=n) {

tn=tn+a; /*赋值后的tn为i个 a组成数的值*/ sn=sn+tn; /*赋值后的sn为多项式前i项之和*/ a=a*10; ++i; }

printf(\ return 0; }

5-6

#include int main()

{double s=0,t=1; int n;

for (n=1;n<=20;n++) {

t=t*n; s=s+t; }

printf(\ return 0; } 5-7

#include int main() {

int n1=100,n2=50,n3=10; double k,s1=0,s2=0,s3=0;

for (k=1;k<=n1;k++) /*计算1到100的和*/ {s1=s1+k;}

for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}

for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/ {s3=s3+1/k;}

printf(\ return 0; } 5-8

#include int main() {

int i,j,k,n;

printf(\ for (n=100;n<1000;n++) {

i=n/100; j=n/10-i*10;

k=n;

if (n==i*i*i + j*j*j + k*k*k) printf(\ }

printf(\ return 0; } 5-10

#include int main() {

int i,n=20;

double a=2,b=1,s=0,t; for (i=1;i<=n;i++) {

s=s+a/b; t=a, a=a+b, b=t; }

printf(\ return 0; } 5-11

#include int main() {

double sn=100,hn=sn/2; int n;

for (n=2;n<=10;n++) {

sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/ }

printf(\第10次落地时共经过%f米\\n\ printf(\第10次反弹%f米\\n\ return 0; } 5-12

#include int main() {

int day,x1,x2; day=9;

x2=1;

while(day>0)

{x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1; day--; }

printf(\ return 0; } 5-13

#include #include int main() {

float a,x0,x1;

printf(\ scanf(\ x0=a/2;

x1=(x0+a/x0)/2; do

{x0=x1;

x1=(x0+a/x0)/2;

}while(fabs(x0-x1)>=1e-5);

printf(\ is %8.5f\\n\ return 0; } 5-15

#include #include int main()

{float x0,x1,x2,fx0,fx1,fx2; do

{printf(\ scanf(\ fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; }while(fx1*fx2>0); do

{x0=(x1+x2)/2;

fx0=x0*((2*x0-4)*x0+3)-6; if ((fx0*fx1)<0) {x2=x0; fx2=fx0; }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4