高等教育出版社《C语言程序设计(苏小红)》课后习题答案

更新时间:2023-05-08 06:53:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1

2.2 #include

main()

{

float x=2.5,y=2.5,z=2.5;

printf("x=%f\n",x);

printf("y=%f\n",y);

printf("z=%f\n",z);

}

3.1(1)

#include

main()

{

int a=12,b=3;

float x=18.5,y=4.6;

printf("%f\n",(float)(a*b)/2);

printf("%d\n",(int)x%(int)y);

}

3.1(2)

#include

main()

{

int x=32,y=81,p,q;

p=x++;

q=--y;

printf("%d %d\n",p,q); printf("%d %d\n",x,y); }

3.2

#include

main()

{

int x,b0,b1,b2,s;

printf("Inputx:");

scanf("%d",&x);

b2=x/100;

b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10; b0=x%10;

s=b0*100+b1*10+b2;

printf("s=%d\n",s);

}

3.3

#include

#include

main()

{

float rate=0.0225;

float n,capital,deposit;

printf("Input n,capital:");

scanf("%f,%f",&n,&capital);

deposit=capital*pow(1+rate,n);

printf("deposit=%f\n",deposit);

}

3.4

#include

#include

main()

{

float a, b, c;

double x, y;

printf("Input a, b, c:");

scanf("%f %f %f", &a, &b, &c);

x=(-b+sqrt(b*b-4*a*c))/(2*a);

y=(-b-sqrt(b*b-4*a*c))/(2*a);

printf("x=%f,y=%f\n",x,y);

}

习题4 Array 4.1(1)

#include

main()

{

char c1='a',c2='b',c3='c';

printf("a%cb%cc%c\n",c1,c2,c3);

}

4.1(2)

main()

{

int a=12,b=15;

printf("a=%d%%,b=%d%%\n",a,b);

}

2

4.1(3)

#include

main()

{

int a,b;

scanf("%2d%*2s%2d",&a,&b);

printf("%d,%d\n",a,b);

}

4.2

#include

main()

{

long a,b;

float x,y;

scanf("%d,%d\n",&a,&b);

scanf("%f,%f\n",&x,&y);

printf("a=%d,b=%d\n",a,b);

printf("x=%f,b=%f\n",x,y);

}

5.1

#include

main()

{

float a;

printf("Input a:");

scanf("%f",&a);

if(a>=0)

{

a=a;

printf("a=%f\n",a);

}

else

{

a=-a;

printf("a=%f\n",a);

}

}

5.2

#include

main()

{

int a;

printf("Input a:");

scanf("%d", &a);

3

if(a%2==0)

{

printf("a是偶数");

}

else

{

printf("a是奇数");

}

}

5.3

#include

#include

main()

{

float a,b,c,s,area;

printf("Input a, b, c:");

scanf("%f %f %f", &a, &b, &c);

if(a+b>c&&a+c>b&&b+c>a)

{

s=(a+b+c)/2;

area=(float)sqrt(s*(s-a)*(s-b)*(s-c));

printf("area=%f\n",area);

}

else

{

printf("不是三角形");

}

}

5.4

#include

#include

main()

{

float a,b,c,x,y;

printf("Inputa,b,c:");

scanf("%f,%f,%f",&a,&b,&c);

if(a==0)

{

printf("该方程不是一元二次方程\n");

}

if(b*b-4*a*c>0)

{

x=(-b+sqrt(b*b-4*a*c))/(2*a);

y=(-b-sqrt(b*b-4*a*c))/(2*a);

printf("x=%f,y=%f\n",x,y);

4

}

else if(b*b-4*a*c==0)

{

x=-b/(2*a);

y=-b/(2*a);

printf("x=%f,y=%f\n",x,y);

}

else

{

printf("该方程无实根\n");

}

}

5.5

#include

main()

{

int year,flag;

printf("Input a year:");

scanf("%d",&year);

if(year%4==0&&year%400!=0||year%400==0)

{

flag=1;

}

else

{

flag=0;

}

if(flag==1)

{

printf("%d is a leap year!\n",year);

}

else

{

printf("%d is not a leap year!\n",year);

}

}

5.6

#include

main()

{

int year,flag;

printf("Input a year:");

scanf("%d",&year);

flag=year%400==0||year%4==0&&year%100!=0?1:0;

if(flag==1&&flag!=0)

5

{

printf("%d is a leap year!\n",year);

}

else

{

printf("%d is not a leap year!\n",year);

}

}

5.7

#include

main()

{

char ch;

printf("Inputch:");

scanf("%c",&ch);

if(ch>='a'&&ch<='z')

{

ch=getchar();

ch=ch-32;

printf("%c,%d\n",ch,ch);

}

else if(ch>='A'&&ch<='Z')

{

ch=getchar();

ch=ch+32;

printf("%c,%d\n",ch,ch);

}

else

{

printf("%c",ch);

}

}

6

5.8

#include

main()

{

char ch;

printf("Inputch:");

scanf("%c",&ch);

if(ch>=48&&ch<=57)

{

printf("ch是数字字符\n");

}

else if(ch>=65&&ch<=90)

{

printf("ch是大写字母\n");

}

else if(ch>=97&&ch<=122)

{

printf("ch是小写字母\n");

}

else if(ch==32)

{

printf("ch是空格\n");

}

else

{

printf("ch是其他字符\n");

}

}

5.9

#include

main()

{

int score,grade;

printf("Input score:");

scanf("%d",&score);

grade=score/10;

if(score<0||score>100)

{

printf("Input error\n");

}

if(score>=90&&score<=100)

{

printf("%d--A\n",score);

}

else if(score>=80&&score<90)

{

1

printf("%d--B\n",score);

}

else if(score>=70&&score<80)

{

printf("%d--C\n",score);

}

else if(score>=60&&score<70)

{

printf("%d--D\n",score);

}

else if(score>=0&&score<60)

{

printf("%d--E\n",score);

}

}

5.10

#include

main()

{

int year,month;

printf("Input year,month:");

scanf("%d,%d",&year,&month);

if(month>12||month<=0)

{

printf("error month\n");

}

else

{

switch(year,month)

{

case12:

case10:

case8:

case7:

case5:

case3:

case1:

printf("31天\n");

break;

case11:

case9:

case6:

case4:

printf("30天\n");

break;

1

case2:

if(year%4==0&&year!=0||year%400==0)

{

printf("29天\n");

}

else

{

printf("28天\n");

}

break;

default:

printf("Input error\n");

}

}

}

6.1(1)

#include

main()

{

int i,j,k;

char space='';

for(i=1;i<=4;i++)

{

for(j=1;j<=i;j++)

{

printf("%c",space);

}

for(k=1;k<=6;k++)

{

printf("*");

}

printf("\n");

}

}

6.1(2)#include

main()

{

int k=4,n;

for(n=0;n

{

if(n%2==0)continue;

k--;

}

printf("k=%d\n,n=%d\n",k,n);

}

1

6.1(3)#include

main()

{

int k=4,n;

for(n=0;n

{

if(n%2==0)break;

k--;

}

printf("k=%d,n=%d\n",k,n);

}

6.2(1)#include

main()

{

int i,sum=0;

for(i=1;i<=101;i++)

{

sum=sum+i;

}

printf("sum=%d\n",sum);

}

6.2(2)#include

main()

{

long i;

long term,sum=0;

for(i=1;i<=101;i=i+2)

{

term=i*(i+1)*(i+2);

sum=sum+term;

}

printf("sum=%ld\n",sum);

}

6.2(4)#include

#include

main()

{

int n=1;

float term=1.0,sign=1,sum=0;

while(term<=-1e-4||term>=1e-4)

{

term=1.0/sign;

sum=sum+term;

sign=sign+n;

1

n++;

}

printf("sum=%f\n",sum);

}

6.2(5)#include

#include

main()

{

int n=1,count=1;

float x;

double sum,term;

printf("Input x:");

scanf("%f",&x);

sum=x;

term=x;

do{

term=-term*x*x/((n+1)*(n+2));

sum=sum+term;

n=n+2;

count++;

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

printf("sin(x)=%f,count=%d\n",sum,count);

}

6.3#include

main()

{

int x=1,find=0;

while(!find)

{

if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)

{

printf("x=%d\n",x);

find=1;

x++;

}

}

}

/*int x,find=0;

for(x=1;!find;x++)

{

if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0)

{

printf("x=%d\n",x);

find=1;

1

}

}

}*/

6.4#include

main()

{

int i,n;

long p=1,m=1;

printf("Input n:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

p=i*i;

m=i*i*i;

printf("p=%d,m=%d\n",i,p,i,m);

}

}

6.5#include

main()

{

float c,f;

for(c=-40;c<=110;c=c+10)

{

f=9/5*c+32;

printf("f=%f\n",f);

}

}

6.6#include

#include

main()

{

int n;

double c=0.01875,x;

do{

x=x*pow(1+c,12)-1000;

n++;

}while(x>0);

printf("x=%d\n",x);

}

6.7#include

main()

{

int n=0;

1

float a=100.0,c;

printf("Inputc:");

scanf("%f",&c);

do

{

a=a*(1+c);

n++;

}while(a<=200);

printf("n=%d\n",n);

}

6.8#include

#include

main()

{

int n=1,count=1;

double sum=1,term=1;

while(fabs(term)>=1e-5)

{

term=pow(-1,count)*(1.0/(n+2));

sum=sum+term;

n=n+2;

count++;

}

sum=4*sum;

printf("sum=%f,count=%d\n",sum,count); }

6.9#include

#include

main()

{

int n=1,count=1;

double sum=1,term=1;

while(fabs(term)>=1e-5)

{

term=term*(1.0/n);

sum=sum+term;

n++;

count++;

}

printf("sum=%f,count=%d\n",sum,count); }

6.10#include

#include

1

main()

{

int x;

for(x=100;x<=999;x++)

{

if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))

printf("x=%d\n",x);

}

}

6.11#include

main()

{

int i=0,n;

long sum=0,term=1;

printf("Inputn:");

scanf("%d",&n);

do{

i++;

term=term*i;

sum=sum+term;

}while(sum

printf("%d\n",i-1);

}

6.12#include

main()

{

int i,n,m,count=0,sum=0;

printf("Input n:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

printf("Inputm:");

scanf("%d",&m);

if(m>0)

{

sum=sum+m;

count++;

}

else

{

break;

}

printf("sum=%d,count=%d\n",sum,count);

1

}

}

6.13#include

main()

{

int i,n,m,count=0,sum=0;

printf("Inputn:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

printf("Inputm:");

scanf("%d",&m);

if(m>0||m<0)

{

sum=sum+m;

count++;

}

else

{

break;

}

printf("sum=%d,count=%d\n",sum,count);

}

}

6.14#include

main()

{

int x,y,z;

for(x=0;x<=17;x++)

{

for(y=0;y<=25;y++)

{

3*x+2*y+z==50;

z=30-x-y;

if(3*x+2*y+z==50&&x+y+z==30)

printf("x=%d,y=%d,z=%d\n",x,y,z);

}

}

}

6.15#include

main()

{

int x,y;

for(x=0;x<=98;x++)

1

{

y=98-x;

2*x+4*y==386;

if(x+y==98&&2*x+4*y==386)

{

printf("x=%d,y=%d\n",x,y);

}

}

}

6.16#include

main()

{

int x,y,z;

for(x=0;x<=20;x++)

{

for(y=0;y<=33;y++)

{

3*y+5*x+z/3.0==100;

z=100-x-y;

if(5*x+3*y+z/3.0==100&&z+x+y==100)

{

printf("x=%d,y=%d,z=%d\n",x,y,z);

}

}

}

}

6.17#include

main()

{

int x,y,z;

for(x=1;x<=9;x++)

{

for(y=1;y<=17;y++)

{

10*x+5*y+z==100;

z=50-x-y;

if(10*x+5*y+z==100&&x+y+z==50&&z>0)

{

printf("x=%d,y=%d,z=%d\n",x,y,z);

}

}

}

1

}

7.1#include

int Square(int i)

{

return i*i;

}

int main()

{

int i=0;

i=Square(i);

for(;i<3;i++)

{

static int i=1;

i+=Square(i);

printf("%d,",i);

}

printf("%d\n",i);

return0;

}

7.2#include

int hour,minute,second;

void update()

{

second++;

if(second==60)

{

second=0;

minute++;

}

if(minute==60)

{

minute=0;

hour++;

}

if(hour==24)

hour=0;

}

void display()

{

printf("%d,%d,%d\n",hour,minute,second);

}

void delay()

{

1

int t;

for(t=0;t<100000000;t++);

}

int main()

{

int i;

void updaye(),display(),delay();

for(i=0;i<1000000;i++)

{

update();

display();

delay();

}

return0;

}

7.3#include

int GetMax(int a,int b);

int main()

{

int x,y,max;

printf("Inputx,y:");

scanf("%d,%d",&x,&y);

max=GetMax(x,y);

printf("max=%d\n",max);

return0;

}

int GetMax(int m,int n)

{

if(m>=n)

return m;

else

return n;

}

7.4#include

int LCM(int n,int m);

int main()

{

int a,b;

printf("Inputa,b:");

scanf("%d,%d",&a,&b);

printf("%d\n",LCM(a,b));

return0;

}

int LCM(int n,int m)

1

{

int x;

int find=0;

for(x=1;!find;x++)

{

if(x%n==0&&x%m==0)

{

find=1;

}

}

return x-1;

}

7.5#include

long Fact(int n);

int main()

{

int m,a;

printf("Inputm:");

scanf("%d",&m);

for(a=1;a<=m;a++)

{

printf("%d!=%ld\n",a,Fact(a));

}

return0;

}

long Fact(int n)

{

int i;

long result=1;

for(i=2;i<=n;i++)

result*=i;

return result;

}

7.6#include

long Fact(int n);

int main()

{

int m;

long ret;

printf("Inputm:");

scanf("%d",&m);

ret=Fact(m);

printf("ret=%d\n",ret);

1

return0;

}

long Fact(int n)

{

int i;

long result=1,sum=0;

for(i=2;i<=n;i++)

{

result*=i;

sum=sum+result;

}

return sum;

}

7.7(1)#include

int Gcd(int a,int b);

int main()

{

int m,n;

printf("Inputm,n:");

scanf("%d,%d",&m,&n);

printf("%d\n",Gcd(m,n));

return0;

}

int Gcd(int a,int b)

{

int t,min,find=0;

min=a

t=min;

for(t=min;!find;t--)

{

a%t==0;

b%t==0;

if(a%t==0&&b%t==0)

return t;

}

find=1;

}

7.7(2)#include

int Gcd(int a,int b);

int main()

{

int m,n;

printf("Inputm,n:");

scanf("%d,%d",&m,&n);

1

printf("%d\n",Gcd(m,n));

return0;

}

int Gcd(int a,int b)

{

int r,temp;

r=a%b;

if(r==0)

return b;

else

do{

temp=b;

b=r;

a=temp;

r=a%b;

}while(r!=0);

return b;

}

8.1(1)#include

void Func(int x)

{

x=2;

}

int main()

{

int x=10;

Func(x);

printf("%d",x);

return0;

}

8.1(2)#include

void Func(int b[])

{

int j;

for(j=0;j<4;j++)

{

b[j]=j;

}

}

int main()

{

static int a[]={5,6,7,8},i;

Func(a);

for(i=0;i<4;i++)

1

本文来源:https://www.bwwdw.com/article/qd2e.html

Top