4.
(1)0
(2)1(3)1(4)0(5)0
5.max=(max=a>b?
a:
b)>c?
max:
c
6.-4
7.1
8.5,0,3
9.85belongstoB
10.
(1)(a==0)
(2)(b==0)(3)(disc<0)
4-3编程题
1.#include“math.h”
main()
{floata,b,c,s,area;
scanf(“%f,%f,%f”,&a,&b,&c);
if(a+b>c&&a+c>b&&b+c>a)
{s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“%f”,area);
}
}
2.main()
{intx,y;
scanf(“%d,%d”,&x,&y);
if(x*x+y*y>1000)
printf(“%d\n”,(x*x+y*y)/100);
else
printf(“%d\n”,x+y);
}
3.main()
{intx,
scanf(“%d”,&x);
if(x%3==0&&x%5==0&&x%7==0)
printf(“yes\n”);
else
printf(“no\n”);
}
4.#include“math.h”
main()
{floatx,y;
scanf(“%f”,&x);
if(x<-2)y=x*x-sin(x);
elseif(x<=2)y=pow(2,x)+1;
elsey=sqrt(x*x+x+1);
printf(“%f\n”,y);
}
5.main()
{
longa,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出个位*/
if(a!
=0)printf("thereare5,%ld%ld%ld%ld%ld\n",e,d,c,b,a);
elseif(b!
=0)printf("thereare4,%ld%ld%ld%ld\n",e,d,c,b);
elseif(c!
=0)printf("thereare3,%ld%ld%ld\n",e,d,c);
elseif(d!
=0)printf("thereare2,%ld%ld\n",e,d);
elseif(e!
=0)printf("thereare1,%ld\n",e);
}
6.1.程序分析:
main()
{
longge,shi,qian,wan,x;
scanf("%ld",&x);
wan=x/10000;
qian=x%10000/1000;
shi=x%100/10;
ge=x%10;
if(ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/
printf("thisnumberisahuiwen\n");
else
printf("thisnumberisnotahuiwen\n");
}
7.main()
{floatp,w,s,d,f;
scanf(“%f,%,%f”,p,s,w);
if(s>3000)d=0.15
elseif(s>=2000)d=0.1;
elseif(s>=1000)d=0.08;
elseif(s>=500)d=0.05;
elseif(s>=250)d=0.02;
elsed=0
f=p*w*s*(1-d);
printf(“%f”,f);
}
习题5
5-1单选题
1~5CDACB6~10DCAAB11~16DBDBCB
5-2填空题
1.20
2.k=5,s=4
3.333
4.HowAreYou
5.2#18#
6.
(1)i<10
(2)j%3!
=0
7.
(1)flag*(float)k/(k+1)
(2)flag=-flag
8.
(1)s=0,t=1;
(2)t=t*x/i*pow(-1,i+1);(3)printf(“%f”,s);
9.
(1)max=x
(2)x!
=-1(3)scanf("%d",&x)
10.
(1)n=1
(2)s
5-3编程题
1.
(1)main()
{inti;lonts=0;
for(i=1;i<=100;i++)
s+=i*i;
printf(“%ld”,s);
}
(2)main()
{inti;lontp=1,s=0;
for(i=1;i<=10;i++)
{p=p*i;
s+=p;
}
printf(“%ld”,s);
}
(3)main()
{inti=1;lontp=1;floats=0;
do{
s+=1.0/p;
p*=++i;
}while(1.0/p>1e-6)
printf(“%f”,s);
}
(4)main()
{inti=1,flag=1,pi=0;
do{
s+=flag*1.0/(2*i-1);
i++;
}while(1.0/(2*i-1)>1e-6);
printf(”%f”,s);
}
2.main()
{intm,n,t,a,b;
scanf(“%d,%d” ,&m,&n) ;
if(ma=m;b=n;
t=m%n ;
while(t)
{m=n ;n=t ;t=m%n;}
printf(”%d,%d”,n,a*b/n);
}
3.main()
{intx,y,s=1;
scanf(”%d,%d ”,&x,&y) ;
for( ;y>0 ;y--)s*=x ;
printf(”%d,%d,%d\n ”,s%10,s/10%10,s/100%10);
}
4.main()
{inti=1,k=2,sum=0;
do{
sum+=k;
k=k*2;
i++;
}while(sum<=100);
printf(”total=%f\n”,0.4*sum/(i-1));
}
5.main()
{intx,y,z;
for(x=1 ;x<20 ;x++)
for(y=1 ;y<33 ;y++)
{z=100-x-y ;
if(z>=0&&(5*x+3*y+z/3)-100<1e-5)
printf(”x=%d,y=%d,z=%d\n”,x,y,z) ;
}
}
6.main()
{intj,k ;
for(j=1 ;j<=4 ;j++)
{for(k=1;k<=4-j;k++;)printf(””);
for(k=1 ;k<=2*j-1 ;k++)
printf(”*”) ;
printf(”\n”) ;
}
for(j=3;j>=1;j--)
{for(k=1;k<=4-j;k++;)printf(””);
for(k=1;k<=2*j-1;k++)
printf(”*”);
printf(”\n”);
}
}
7.分析:
其实此问题的解法非常简单。
从数学上来说,可以用穷举法。
比如让x从2开始,判断表达式“(x%3==2&&x%5==3&&x%7==2)”是否成立,若不成立,让x自增1,直到某个x满足条件了,则这个x即为所求。
程序如下:
#include
main()
{
intx=2;
while(!
(x%3==2&&x%5==3&&x%7==2))
{
x++;
}
printf("x=%d\n",x);
}
程序运行结果:
x=23
8分析:
定义一个字符变量c和一个整型变量n,c用于接受从键盘输入的非回车行字符,n用于统计从键盘输入的非回车符字符的个数,也就是说,以(c=getchar())!
='\n'为循环条件,反复进行“n++;”的运算。
程序代码如下:
#include
voidmain(){
intn=0;
charc;
printf("inputastring:
\n");
while((c=getchar())!
='\n')n++;
printf("%d",n);
}
9.例如:
153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
程序分析:
利用for循环控制100-999个数,每个数分解出个位,十位,百位。
main()
{
inti,j,k,n;
printf("'waterflower'numberis:
");
for(n=100;n<1000;n++)
{
i=n/100;/*分解出百位*/
j=n/10%10;/*分解出十位*/
k=n%10;/*分解出个位*/
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
{
printf("%-5d",n);
}
}
printf("\n");
}
10.main()
{intx,y,z;
for(x=1 ;x<17 ;x++)
for(y=1 ;y<25 ;y++)
{z=30-x-y ;
if(z>0&&3*x+2*y+z==50)
printf("man=%d,woman=%d,child=%d\n”,x,y,z);
}
}
习题6
6-1单选题
1-5 CBBAD6-10 DBAAC11-15 CCDDC16-20 BBABC
6-2填空题
1. 120
2. x
3. 3,2,2,34./I
5. 8,17
6. 9
7. 1.0/(i*i)
8. fun-in:
30,20,10
fun-end:
1015,35,1050
10,20,30
9. 01234510.93636
6-3编程题
1.
voidzhuan()
{charch;
while((ch=getchar())!
=’\n’)
{if(ch>=’a’&&ch<=’z’)ch=ch-32;
putchar(ch);
}
}
2.
floatexpp(intn)
{intk,fac=1;
floatsum=0;
for(k=1;k<=n;k++)
{fac*=k;
sum+=1.0/fac;
}
return(sum);
}
3.
intxy3(intx,inty)
{intk,num=1;
for(k=1;k<=y;k++)
num*=x;
num=num%1000;
returnnum;
}
4.
voidfactors(intn)
{intk;
printf(“%ditsfactorsare:
”,n);
for(k=1;kif(n%k==0)
printf(“%d,”,k);
printf(“\n”);
}
5.
intage(intn)
{intc;
if(n==1)c=10;
elsec=age(n-1)+2;
returnc;
}
习题7
7-1单选题
1-5 DBCCB6-8BDC
7-2填空题
1. c
2. 60
3. 1000 10
4. 16
7-3编程题
1.
#defineF(a)a*a+sqrt(3*a*a+2*a+1)
main()
{floatx,f;
scanf(“%f”,&x);
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x);
printf(“%f\n”,f);
}
习题8
8-1单选题
1~15CADAC6~10CCDAA11~16BCBBCD
10000
01000
00100
00010
00001
8-2填空题
1
(1)@*&$
(2)1345(3)10010(4)QuickC(5)
2.
(1)j+=2
(2)a[i]>a[j]
3.
(1)r+b[k]
(2)*x
8-3编程题
1.#defineN10
main()
{inta[N]={1,2,3,4,5,6,7,8,9,0},osum=0,qsum=0,j;
for(j=0;j<10;j++)
if(j%2)qsum+=a[j];
elseosum+=a[j];
printf(“osum=%d,qsum=%d\n”,osum,qsum);
}
2.#defineN10
main()
{inta[N]={10,20,30,40,50,60,70,80,90},j,k,x;
scanf(“%d”,&x);
for(j=0;jif(xfor(k=N-1;k>j;k--)
a[k]=a[k-1];
a[j]=x;
for(j=0;jprintf(“%d”,a[j]);
}
3.#defineN10
main()
{inta[N]={1,2,3,4,5,6,7,8,9,0}.b[N-1],j;
for(j=0;jb[j]=a[j+1]-a[j];
for(j=0;j{if(j%3==0)printf(“\n”);
printf(“%d”,b[j]);}
}
4.#defineM3
main()
{inta[M][M]={{1,2,3},{4,5,6},{7,8,9}},j,sum1=0,sum2=0;
for(j=0;j{sum1+=a[j][j];
sum2+=a[j][M-(j+1)];
}
printf(“%d,%d\n”,sum1,sum2);
}
5.#defineM3
main()
{inta[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1;;
for(j=0;jfor(k=0;kif(a[j][k]!
=a[k][j]){flag=0;break;}
if(flag)printf(“ok”);
elseprintf(”NO”);
}
6.#include“string.h”
main()
{charc[10],j;
gets(c);
for(j=0;c[j];j++)
c[j]=(c[j]>=’a’&&c[j]<=’z’)?
c[j]-32:
c[j];
puts(c);
}
7.#include“string.h”
main()
{charc1[10],c2[10],j;
gets(c1);gets(c2);
for(j=0;(c1[j]==c2[j])&&c1[j]&&c2[j];j++);
printf(“%d\n”,c1[j]-c2[j]);
}
8.#include”string.h”
#defineM3
#defineN80
main()
{chara[M][N],j,k,n[5]={0};
for(j=0;jgets(a[j]);
for(j=0;jfor(k=0;a[j][k];k++)
if(a[j][k]>=’A’&&a[j][k]<=’Z’)n[1]++;
elseif(a[j][k]>=’a’&&a[j][k]<=’z’)n[2]++;
elseif(a[j][k]>=’0’&&a[j][k]<=’9’)n[3]++;
elseif(a[j][k]=’’)n[4]++;
else
n[5]++;
for(j=0;j<5;j++)
printf(“%4d”,n[j]);
}
习题9
9-1单选题
1~5DDBAC6~10BADCB11~15ADCDA16~20CCABD
9-2填空题
1.
(1)2,1
(2)10#30#5#(3)FOUR,O(4)60
2.
(1)49
(2)AC(3)2(4)2(5)3,12,39(6)75319(7)15
01234
56789
1011121314
0522713
(8)12,10,9,6,5,4,3,2,1(9)
1,1,1
1,1
3,3,3
3,3
(10)
10001
01010
00100
01010
10001
(11)
3.
(1)*x
(2)t
4.
(1)'\0'或0
(2)n++或n+=1或n=n+1
5.ABCDCD
6.024
7.voidfun(doubleb[][22])
或voidfun(doubleb[0][22])
或v