说明
1. 本文所指的《C语言程序设计教程(第二版)》是李凤霞主编、北京理
工大学出版社出版的,绿皮。
2 第1章 程序设计基础知识
一、单项选择题(第23页)
1-4.CBBC 5-8.DACA
二、填空题(第24页)
1.判断条件 2.面向过程编程 3.结构化 4.程序 5.面向对象的程序设计语言 7.有穷性 8.直到型循环 9.算法 10.可读性 11.模块化 12.对问题的分析和模块的划分
三、应用题(第24页)
2.源程序:
main()
{int i,j,k/* i:公鸡数,j:母鸡数,k:小鸡数的1/3 */ <br>printf("cock hen chick\n")<br>for(i=1i<=20i++) <br>for(j=1j<=33j++) <br>for(k=1k<=33k++) <br>if (i+j+k*3==100&&i*5+j*3+k==100) <br>printf(" %d %d %d\n",i,j,k*3)}
执行结果:
cock hen chick
4 18 78
8 11 81
12 4 84
3.现计算斐波那契数列的前20项。
递推法 源程序:
main()
{long a,bint i<br>a=b=1<br>for(i=1i<=10i++) /*要计算前30项,把10改为15。*/ <br>{printf("%8ld%8ld",a,b)<br>a=a+bb=b+a}}
递归法 源程序:
main()
{int i<br>for(i=0i<=19i++) <br>printf("%8d",fib(i))}
fib(int i)
{return(i<=1?1:fib(i-1)+fib(i-2))}
执行结果:
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765
4.源程序:
#include "math.h"
main()
{double x,x0,deltax<br>x=1.5<br>do {x0=pow(x+1,1./3)<br>deltax=fabs(x0-x)<br>x=x0<br>}while(deltax>1e-12)
printf("%.10f\n",x)}
执行结果:
1.3247179572
5.源程序略。(分子、分母均构成斐波那契数列)
结果是32.66026079864
6.源程序:
main()
{int a,b,c,m<br>printf("Please input a,b and c:")<br>scanf("%d %d %d",&a,&b,&c)<br>if(a<b){m=aa=bb=m}
if(a<c){m=aa=cc=m}
if(b<c){m=bb=cc=m}
printf("%d %d %d\n",a,b,c)}
执行结果:
Please input a,b and c:123 456 789
789 456 123
7.源程序:
main()
{int a<br>scanf("%d",&a)<br>printf(a%21==0?"Yes":"No")}
执行结果:
42
Yes
3 第2章 C语言概述
一、单项选择题(第34页)
1-4.BDCB 5-8.AABC
二、填空题(第35页)
1.主 2.C编译系统 3.函数 函数 4.输入输出 5.头 6. .OBJ 7.库函数 8.文本
三、应用题(第36页)
5.sizeof是关键字,stru、_aoto、file、m_i_n、hello、ABC、SIN90、x1234、until、cos2x、s_3是标识符。
8.源程序:
main()
{int a,b,c<br>scanf("%d %d",&a,&b)<br>c=aa=bb=c<br>printf("%d %d",a,b)}
执行结果:
12 34
34 12
4 第3章 数据类型与运算规则
一、单项选择题(第75页)
1-5.DBACC 6-10.DBDBC 11-15.ADCCC 16-20.CBCCD 21-25.ADDBC 26-27.AB
二、填空题(第77页)
1.补码 2.±(10^-308~10^308) 3.int(整数) 4.单目 自右相左 5.函数调用 6.a或b 7.1 8.65,89
三、应用题(第78页)
1.10 9
2.执行结果:
11
0
0
12
1
5 第4章 顺序结构程序设计
一、单项选择题(第90页)
1-5.DCDAD 6-10.BACBB
二、填空题(第91页)
1.一 ;2. 5.169000 3.(1)-2002500 (2)I=-200,j=2500 (3)i=-200
j=2500 4.a=98,b=765.000000,c=4321.000000 5.略 6.0,0,3 7.3 8.scanf("%lf%lf%lf",&a,&b,&c)9. 13 13.000000,13.000000 10.a=a^cc=c^aa=a^c(这种算法不破坏b的值,也不用定义中间变量。)
三、编程题(第92页)
1.仿照教材第27页例2-1。
2.源程序:
main()
{int h,m<br>scanf("%d:%d",&h,&m)<br>printf("%d\n",h*60+m)}
执行结果:
9:23
563
3.源程序:
main()
{int a[]={-10,0,15,34},i
for(i=0i<=3i++)
printf("%d\370C=%g\370F\t",a[i],a[i]*1.8+32)}
执行结果:
-10℃=14°F 0℃=32°F 15℃=59°F 34℃=93.2°F
4.源程序:
main()
{double pi=3.14159265358979,r=5<br>printf("r=%lg A=%.10lf S=%.10lf\n",r,2*pi*r,pi*pi*r)}
执行结果:
r=5 A=31.4159265359 S=49.3480220054
5.源程序:
#include "math.h"
main()
{double a,b,c<br>scanf("%lf%lf%lf",&a,&b,&c)<br>if (a+b>c&&a+c>b&&b+c>a) <br>{double s=(a+b+c)/2<br>printf("SS=%.10lf\n",sqrt(s*(s-a)*(s-b)*(s-c)))}
else printf("Data error!")}
执行结果:
4 5 6
SS=9.9215674165
6.源程序:
main()
{int a=3,b=4,c=5float d=1.2,e=2.23,f=-43.56<br>printf("a=%3d,b=%-4d,c=**%d\nd=%g\ne=%6.2f\nf=%-10.4f**\n",a,b,c,d,e,f)}
7.源程序:
main()
{int a,b,c,m<br>scanf("%d %d %d",&a,&b,&c)<br>m=aa=bb=cc=m<br>printf("%d %d %d\n",a,b,c)}
执行结果:
5 6 7
6 7 5
8.源程序:
main()
{int a,b,c<br>scanf("%d %d %d",&a,&b,&c)<br>printf("average of %d,%d and %d is %.2f\n",a,b,c,(a+b+c)/3.)<br>执行结果: <br>6 7 9 <br>average of 6,7 and 9 is 7.33 <br>9.不能。修改后的源程序如下: <br>main() <br>{int a,b,c,x,y<br>scanf("%d %d %d",&a,&b,&c)<br>x=a*by=x*c<br>printf("a=%d,b=%d,c=%d\n",a,b,c)<br>printf("x=%d,y=%d\n",x,y)}
6 第5章 选择结构程序设计
一、单项选择题(第113页)
1-4.DCBB 5-8.DABD
二、填空题(第115页)
1.非0 0 2.k==0
3.if (abs(x)>4) printf("%d",x)else printf("error!")
4.if((x>=1&&x<=10||x>=200&&x<=210)&&x&1)printf("%d",x)
5.k=1 (原题最后一行漏了个d,如果认为原题正确,则输出k=%。)
6. 8! Right!11 7.$$$a=0 8.a=2,b=1
三、编程题(第116页)
1.有错。正确的程序如下:
main()
{int a,b,c<br>scanf("%d,%d,%d",&a,&b,&c)<br>printf("min=%d\n",a>b?b>c?c:b:a>c?c:a)}
2.源程序:
main()
{unsigned long a<br>scanf("%ld",&a)<br>for(aprintf("%d",a%10),a/=10)}
执行结果:
12345
54321
3.(1)源程序:
main()
{int x,y<br>scanf("%d",&x)<br>if (x>-5&&x<0)y=x<br>if (x>=0&&x<5)y=x-1<br>if (x>=5&&x<10)y=x+1<br>printf("%d\n",y)}
(2)源程序:
main()
{int x,y<br>scanf("%d",&x)<br>if(x<10) if(x>-5) if(x>=0) if(x>=5)y=x+1<br>else y=x-1else y=x<br>printf("%d\n",y)}
(3)源程序:
main()
{int x,y<br>scanf("%d",&x)<br>if(x<10) if(x>=5)y=x+1<br>else if(x>=0)y=x-1<br>else if(x>-5)y=x<br>printf("%d\n",y)}
(4)源程序:
main()
{int x,y<br>scanf("%d",&x)<br>switch(x/5) <br>{case -1:if(x!=-5)y=xbreak<br>case 0:y=x-1break<br>case 1:y=x+1}
printf("%d\n",y)}
4.本题为了避免考虑每月的天数及闰年等问题,故采用面向对象的程序设计。
现给出Delphi源程序和C++ Builder源程序。
Delphi源程序:
procedure TForm1.Button1Click(Sender: TObject)
begin
edit3.Text:=format('%.0f天',[strtodate(edit2.text) -strtodate(edit1.text)])
end
procedure TForm1.FormCreate(Sender: TObject)
begin
Edit2.Text:=datetostr(now)
button1click(form1)
end
C++ Builder源程序:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit3->Text=IntToStr(StrToDate(Edit2->Text)-StrToDate(Edit1->Text))+"天"
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit2->Text=DateToStr(Now())
Button1Click(Form1)
}
执行结果:(运行于Windows下) http://img378.photo.163.com/nxgt/41463572/1219713927.jpg
5.源程序:
main()
{unsigned a,b,c<br>printf("请输入三个整数:")<br>scanf("%d %d %d",&a,&b,&c)<br>if(a&&b&&c&&a==b&&a==c)printf("构成等边三角形\n")<br>else if(a+b>c&&a+c>b&&b+c>a) <br>if(a==b||a==c||b==c)printf("构成等腰三角形\n")<br>else printf("构成一般三角形\n")<br>else printf("不能构成三角形\n")}
执行结果:
请输入三个整数:5 6 5
构成等腰三角形
6.源程序:
main()
{int x,y<br>scanf("%d",&x)<br>if(x<20)y=1<br>else switch(x/60) <br>{case 0:y=x/10break<br>default:y=6}
printf("x=%d,y=%d\n",x,y)}
7.源程序:
main()
{unsigned mfloat n<br>scanf("%d",&m)<br>if(m<100)n=0<br>else if(m>600)n=0.06<br>else n=(m/100+0.5)/100<br>printf("%d %.2f %.2f\n",m,m*(1-n),m*n)}
执行结果:
450
450 429.75 20.25
8. 2171天(起始日期和终止日期均算在内)
本题可利用第4小题编好的程序进行计算。把起始日期和终止日期分别打入“生日”和“今日”栏内,单击“实足年龄”按钮,将所得到的天数再加上1天即可。
9.源程序:
#include "math.h"
main()
{unsigned long i<br>scanf("%ld",&i)<br>printf("%ld %d\n",i%10,(int)log10(i)+1)}
执行结果:
99887
7 5
10.源程序:
main()
{unsigned long iunsigned j[10],m=0<br>scanf("%ld",&i)<br>for(i){j[m++]=(i+2)%10i/=10}
for(mm--)i=i*10+j[m-1]
printf("%ld\n",i)}
执行结果:
6987
8109
(注:要加密的数值不能是0或以0开头。如果要以0开头需用字符串而不能是整数。)
7 第6章 循环结构程序设计
一、单项选择题(第142页)
1-4.BCCB 5-8.CBCA
二、填空题(第143页)
1.原题可能有误。如无误,是死循环 2.原题有误。如果把b=1后面的逗号改为分号,则结果是8。 3.20 4.11 5. 2.400000 6.*#*#*#$ 7.8 5 2 8.①d=1.0 ②++k ③k<=n 9.①x>=0 ②x<amin
三、编程题(第145页)
1. 源程序:
main()
{int i=1,sum=i<br>while(i<101){sum+=i=-i-2sum+=i=-i+2}
printf("%d\n",sum)}
执行结果:
51
2.源程序:
main()
{double p=0,n=0,fint i<br>for(i=1i<=10i++) <br>{scanf("%lf",&f)<br>if (f>0)p+=felse n+=f}
printf("%lf %lf %lf\n",p,n,p+n)}
3.源程序:
main()
{unsigned a<br>scanf("%ld",&a)<br>for (aprintf("%d,",a%10),a/=10)<br>printf("\b \n")}
执行结果:
23456
6,5,4,3,2
4.源程序:
main()
{unsigned long a,b,c,i<br>scanf("%ld%ld",&a,&b)<br>c=a%1000<br>for(i=1i<bi++)c=c*a%1000<br>if(c<100)printf("0")<br>if(c<10)printf("0")<br>printf("%ld\n",c)}
执行结果:
129 57
009
5.略
6.原题提供的计算e的公式有误(前面漏了一项1)。正确的公式是e= 1 + 1 + 1/2! + 1/3! + … + 1/n! + …
(1)源程序:
main()
{double e=1,f=1int n<br>for(n=1n<=20n++){f/=ne+=f}
printf("e=%.14lf\n",e)}
执行结果:
e=2.71828182845905
(2)源程序:
main()
{double e=1,f=1int n<br>for(n=1f>1e-4n++){f/=ne+=f}
printf("e=%.4f\n",e)}
执行结果:
e=2.7183
7.源程序:
main()
{unsigned long a=0,b=1,c=0int i,d<br>scanf("%d",&d)<br>for (i=1i<=(d+2)/3i++) <br>printf("%10ld%10ld%10ld",a,b,(a+=b+c,b+=c+a,c+=a+b))}
本题还可以用递归算法(效率很低),源程序如下:
unsigned long fun(int i)
{return i<=3?i:fun(i-1)+fun(i-2)+fun(i-3)}
main()
{int i,dscanf("%d",&d)<br>for(i=1i<=di++) <br>printf("%10ld",fun(i))}
执行结果:
15
1 2 3 6 11 20 37 68
125 230 423 778 1431 2632 4841
8.源程序:
main()
{int i<br>for(i=1010i<=9876i+=2) <br>if(i/100%11&&i%100%11&&i/10%100%11&&i/1000!=i%10&&i/1000!=i/10%10&&i/100%10!=i%10)printf(" %d",i)}
执行结果:
1024 1026 1028 1032 1034 1036 …… …… 9874 9876
9.源程序:
main()
{int i,j,k<br>printf("apple watermelon pear\n")<br>for(i=1i<=100i++) <br>for(j=1j<=10j++) <br>if((k=100-i-j)*2==400-i*4-j*40) <br>printf("%4d%7d%9d\n",i,j,k)}
执行结果:
apple watermelon pear
5 5 90
24 4 72
43 3 54
62 2 36
81 1 18
10.源程序:
#include "stdio.h"
#define N 4 /* N为阶数,可以改为其他正整数 */
main()
{int m=N*2,i,j<br>for(i=1i<mprintf("\n"),i++) <br>for(j=1j<m<br>putchar(N-abs(i-N)<=abs(j++-N)?' ':'*'))}
如果把N值改为5,则执行结果如下:
*
***
*****
*******
*********
*******
*****
***
*
第一章1.6
main()
{int a,b,c,max
printf("input three numbers:\n")
scanf("%d,%d,%d",&a,&b,&c)
max=a
if(max<b)max=b
if(max<c)max=c
printf("max=%d",max)
}
第二章
2.3
(1)(10)10=(12)8=(a)16
(2)(32)10=(40)8=(20)16
(3)(75)10=(113)8=(4b)16
(4)(-617)10=(176627)8=(fd97)16
(5)(-111)10=(177621)8=(ff91)16
(6)(2483)10=(4663)8=(963)16
(7)(-28654)10=(110022)8=(9012)16
(8)(21003)10=(51013)8=(520b)16
2.6
aabb(8)cc(8)abc
(7)AN
2.7
main()
{char c1='C',c2='h',c3='i',c4='n',c5='a'
c1+=4, c2+=4, c3+=4, c4+=4, c5+=4
printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5)
}
2.8
main()
{int c1,c2
c1=97c2=98
printf("%c %c",c1,c2)
}
2.9
(1)=2.5
(2)=3.5
2.10
9,11,9,10
2.12
(1)24 (2)10 (3)60 (4)0 (5)0 (6)0
第三章
3.4
main()
{int a,b,c
long int u,n
float x,y,z
char c1,c2
a=3b=4c=5
x=1.2y=2.4z=-3.6
u=51274n=128765
c1='a'c2='b'
printf("\n")
printf("a=%2d b=%2d c=%2d\n",a,b,c)
printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z)
printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x)
printf("u=%6ld n=%9ld\n",u,n)
printf("c1='%c'or %d(ASCII)\n",c1,c1)
printf("c2='%c'or %d(ASCII)\n",c2,c2)
}
3.5
57
5 7
67.856400,-789.123962
67.856400,-789.123962
67.86 -789.12,67.856400,-789.123962,67.856400,-789.123962
6.785640e+001,-7.89e+002
A,65,101,41
1234567,4553207,d687
65535,177777,ffff,-1
COMPUTER, COM
3.6
a=3 b=7/
x=8.5 y=71.82/
c1=A c2=a/
3.7
10 20Aa1.5 -3.75 +1.4,67.8/
(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车
3.8
main()
{float pi,h,r,l,s,sq,sv,sz
pi=3.1415926
printf("input r,h\n")
scanf("%f,%f",&r,&h)
l=2*pi*r
s=r*r*pi
sq=4*pi*r*r
sv=4.0/3.0*pi*r*r*r
sz=pi*r*r*h
printf("l=%6.2f\n",l)
printf("s=%6.2f\n",s)
printf("sq=%6.2f\n",sq)
printf("vq=%6.2f\n",sv)
printf("vz=%6.2f\n",sz)
}
3.9
main()
{float c,f
scanf("%f",&f)
c=(5.0/9.0)*(f-32)
printf("c=%5.2f\n",c)
}
3.10
#include"stdio.h"
main()
{char c1,c2
scanf("%c,%c",&c1,&c2)
putchar(c1)
putchar(c2)
printf("\n")
printf("%c%c\n",c1,c2)
}
第四章
4.3
(1)0 (2)1 (3)1 (4)0 (5)1
4.4
main()
{int a,b,c
scanf("%d,%d,%d",&a,&b,&c)
if(a<b)
if(b<c)
printf("max=%d\n",c)
else
printf("max=%d\n",b)
else if(a<c)
printf("max=%d\n",c)
else
printf("max=%d\n",a)
}
main()
{int a,b,c,temp,max
scanf("%d,%d,%d",&a,&b,&c)
temp=(a>b)?a:b
max=(c>temp)?c:temp
printf("max=%d",max)
}
4.5
main()
{int x,y
scanf("%d",&x)
if(x<1)y=x
else if(x<10)y=2*x-1
else y=3*x-11
printf("y=%d",y)
}
4.6
main()
{int score,temp,logic
char grade
logic=1
while(logic)
{scanf("%d",&score)
if(score>=0&&score<=100)logic=0
}
if(score==100)
temp=9
else
temp=(score-score%10)/10
switch(temp)
{case 9:grade='A'break
case 8:grade='B'break
case 7:grade='C'break
case 6:grade='D'break
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:grade='E'
}
printf"score=%d,grade=%c",score,grade)
}
4.7
main()
{long int num
int indiv,ten,hundred,thousand,ten_thousand,place
scanf("%ld",&num)
if(num>9999) place=5
else if(num>999) place=4
else if(num>99) place=3
else if(num>9) place=2
else place=1
printf("place=%d\n",place)
ten_thousand=num/10000
thousand=(num-ten_thousand*10000)/1000
hundred=(num-ten_thousand*10000-thousand*1000)/100
ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10
indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10
switch(place)
{case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv)
printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand)
break
case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv)
printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand)
break
case 3:printf("%d,%d,%d\n",hundred,ten,indiv)
printf("%d,%d,%d\n",indiv,ten,hundred)
break
case 2:printf("%d,%d\n",ten,indiv)
printf("%d,%d\n",indiv,ten)
break
case 1:printf("%d\n",indiv)
printf("%d\n",indiv)
}
}
4.8
main()
{long i
float bonus,bon1,bon2,bon4,bon6,bon10
bon1=100000*0.1
bon2=bon1+100000*0.075
bon4=bon2+200000*0.05
bon6=bon4+200000*0.03
bon10=bon6+400000*0.015
scanf("%ld",&i)
if(i<=1e5)bonus=i*0.1
else if(i<=2e5)bonus=bon1+(i-100000)*0.075
else if(i<=4e5)bonus=bon2+(i-200000)*0.05
else if(i<=6e5)bonus=bon4+(i-400000)*0.03
else if(i<=1e6)bonus=bon6+(i-600000)*0.015
else bonus=bon10+(i-1000000)*0.01
printf("bonus=%10.2f",bonus)
}
main()
{long i
float bonus,bon1,bon2,bon4,bon6,bon10
int branch
bon1=100000*0.1
bon2=bon1+100000*0.075
bon4=bon2+200000*0.05
bon6=bon4+200000*0.03
bon10=bon6+400000*0.015
scanf("%ld",&i)
branch=i/100000
if(branch>10)branch=10
switch(branch)
{case 0:bonus=i*0.1break
case 1:bonus=bon1+(i-100000)*0.075break
case 2:
case 3:bonus=bon2+(i-200000)*0.05break
case 4:
case 5:bonus=bon4+(i-400000)*0.03break
case 6:
case 7
case 8:
case 9:bonus=bon6+(i-600000)*0.015break
case 10:bonus=bon10+(i-1000000)*0.01
}
printf("bonus=%10.2f",bonus)
}
4.9
main()
{int t,a,b,c,d
scanf("%d,%d,%d,%d",&a,&b,&c,&d)
if(a>b){t=aa=bb=t}
if(a>c){t=aa=cc=t}
if(a>d){t=aa=dd=t}
if(b>c){t=bb=cc=t}
if(b>d){t=bb=dd=t}
if(c>d){t=cc=dd=t}
printf("%d %d %d %d\n",a,b,c,d)
}
4.10
main()
{int h=10
float x,y,x0=2,y0=2,d1,d2,d3,d4
scanf("%f,%f",&x,&y)
d1=(x-x0)*(x-x0)+(y-y0)*(y-y0)
d2=(x-x0)*(x-x0)+(y+y0)*(y+y0)
d3=(x+x0)*(x+x0)+(y-y0)*(y-y0)
d4=(x+x0)*(x+x0)+(y+y0)*(y+y0)
if(d1>1&&d2>1&&d3>1&&d4>1)h=0
printf("h=%d",h)
}
第五章 循环控制
5.1
main()
{int a,b,num1,num2,temp
scanf("%d,%d",&num1,&num2)
if(num1<num2){temp=num1num1=num2num2=temp}
a=num1b=num2
while(b!=0)
{temp=a%b
a=b
b=temp}
printf("%d\n",a)
printf("%d\n",num1*num2/a)
}
5.2
#include"stdio.h"
main()
{char c
int letters=0,space=0,digit=0,other=0
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("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other)
}
5.3
main()
{int a,n,count=1,sn=0,tn=0
scanf("%d,%d",&a,&n)
while(count<=n)
{tn+=a
sn+=tn
a*=10
++count
}
printf("a+aa+aaa+...=%d\n",sn)
}
5.4
main()
{float n,s=0,t=1
for(n=1n<=20n++)
{t*=n
s+=t
}
printf("s=%e\n",s)
}
5.5
main()
{int N1=100,N2=50,N3=10
float k
float s1=0,s2=0,s3=0
for(k=1k<=N1k++)s1+=k
for(k=1k<=N2k++)s2+=k*k
for(k=1k<=N3k++)s3+=1/k
printf("s=%8.2f\n",s1+s2+s3)
}
5.6
main()
{int i,j,k,n
for(n=100n<1000n++)
{i=n/100
j=n/10-i*10
k=n%10
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("n=%d\n",n)
}
}
5.7
#define M 1000
main()
{int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9
int i,j,n,s
for(j=2j<=Mj++)
{n=0
s=j
for(i=1i<ji++)
{if((j%i)==0)
{n++
s=s-i
switch(n)
{case 1:k0=ibreak
case 2:k1=ibreak
case 3:k2=ibreak
case 4:k3=ibreak
case 5:k4=ibreak
case 6:k5=ibreak
case 7:k6=ibreak
case 8:k7=ibreak
case 9:k8=ibreak
case 10:k9=ibreak
}
}
}
if(s==0)
{printf("j=%d\n",j)
if(n>1)printf("%d,%d",k0,k1)
if(n>2)printf(",%d",k2)
if(n>3)printf(",%d",k3)
if(n>4)printf(",%d",k4)
if(n>5)printf(",%d",k5)
if(n>6)printf(",%d",k6)
if(n>7)printf(",%d",k7)
if(n>8)printf(",%d",k8)
if(n>9)printf(",%d\n",k9)
}
}
}
main()
{static int k[10]
int i,j,n,s
for(j=2j<=1000j++)
{n=-1
s=j
for(i=1i<ji++)
{if((j%i)==0)
{n++
s=s-i
k[n]=i
}
}
if(s==0)
{printf("j=%d\n",j)
for(i=0i<ni++)
printf("%d,",k[i])
printf("%d\n",k[n])
}
}
}
5.8
main()
{int n,t,number=20
float a=2b=1s=0
for(n=1n<=numbern++)
{s=s+a/b
t=a,a=a+b,b=t
}
printf("s=%9.6f\n",s)
}
5.9
main()
{float sn=100.0,hn=sn/2
int n
for(n=2n<=10n++)
{sn=sn+2*hn
hn=hn/2
}
printf("sn=%f\n",sn)
printf("hn=%f\n",hn)
}
5.10
main()
{int day,x1,x2
day=9
x2=1
while(day>0)
{x1=(x2+1)*2
x2=x1
day--
}
printf("x1=%d\n",x1)
}
5.11
#include"math.h"
main()
{float a,xn0,xn1
scanf("%f",&a)
xn0=a/2
xn1=(xn0+a/xn0)/2
do
{xn0=xn1
xn1=(xn0+a/xn0)/2
}
while(fabs(xn0-xn1)>=1e-5)
printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1)
}
5.12
#include"math.h"
main()
{float x,x0,f,f1
x=1.5
do
{x0=x
f=((2*x0-4)*x0+3)*x0-6
f1=(6*x0-8)*x0+3
x=x0-f/f1
}
while(fabs(x-x0)>=1e-5)
printf("x=%6.2f\n",x)
}
5.13
#include"math.h"
main()
{float x0,x1,x2,fx0,fx1,fx2
do
{scanf("%f,%f",&x1,&x2)
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
}
else
{x1=x0
fx1=fx0
}
}
while(fabs(fx0)>=1e-5)
printf("x0=%6.2f\n",x0)
}
5.14
main()
{int i,j,k
for(i=0i<=3i++)
{for(j=0j<=2-ij++)
printf(" ")
for(k=0k<=2*ik++)
printf("*")
printf("\n")
}
for(i=0i<=2i++)
{for(j=0j<=ij++)
printf(" ")
for(k=0k<=4-2*ik++)
printf("*")
printf("\n")
}
}
5.15
main()
{char i,j,k
for(i='x'i<='z'i++)
for(j='x'j<='z'j++)
{if(i!=j)
for(k='x'k<='z'k++)
{if(i!=k&&j!=k)
{if(i!='x'&&k!='x'&&k!='z')
printf("\na--%c\tb--%c\tc--%c\n",i,j,k)
}
}
}
}
第六章 数组
6.1
#include <math.h>
#define N 101
main()
{ int i,j,line,a[N]
for (i=2i<Ni++) a[i]=i
for (i=2i<sqrt(N)i++)
for (j=i+1j<Nj++)
{if(a[i]!=0 &&a[j]!=0)
if (a[j]%a[i]==0)
a[j]=0 }
printf("\n")
for (i=2,line=0i<Ni++)
{ if(a[i]!=0)
{ printf("%5d",a[i])
line++ }
if(line==10)
{ printf("\n")
line=0 }
}
}
6.2
#define N 10
main()
{int i,j,min,temp,a[N]
for(i=0i<Ni++)
scanf("%d",&a[i])
for(i=0i<N-1i++)
{min=i
for(j=i+1j<Nj++)
if(a[min]>a[j])min=j
temp=a[i]
a[i]=a[min]
a[min]=temp
}
for(i=0i<Ni++)
printf("%5d",a[i])
}
6.3
main()
{float a[3][3],sum
int i,j
for(i=0i<3i++)
for(j=0j<3j++)
{scanf("%f",&sum)
a[i][j]=sum
}
for(i=0i<3i++)
sum=sum+a[i][i]
printf("sum=%f",sum)
}
6.4
main()
{int a[11]={1,4,6,9,13,16,19,28,40,100}
int temp1,temp2,number, end,i,j
scanf("%d",&number)
end=a[9]
if(number>end) a[10]=number
else
{for(i=0i<10i++)
{if(a[i]>number)
{temp1=a[i]
a[i]=number
for(j=i+1j<11j++)
{temp2=a[j]
a[j]=temp1
temp1=temp2
}
break
}
}
}
for(i=0i<11i++)
printf("%6d",a[i])
}
6.5
#define N 5
main()
{int a[N]={8,6,5,4,1},i,temp
for(i=0i<N/2i++)
{temp=a[i]
a[i]=a[N-i-1]
a[N-i-1]=temp
}
for(i=0i<Ni++)
printf("%4d",a[i])
}
6.6
#define N 11
main()
{int i,j,a[N][N]
for(i=1i<Ni++)
{a[i][i]=1
a[i][1]=1
}
for(i=3i<Ni++)
for(j=2j<ij++)
a[i][j]=a[i-1][j-1]+a[i-1][j]
for(i=1i<Ni++)
{for(j=1j<=ij++)
printf("%6d",a[i][j])
printf("\n")
}
}
6.7
main()
{int a[16][16],i,j,k,p,m,n
p=1
while(p==1)
{scanf("%d",&n)
if((n!=0)&&(n<=15)&&(n%2!=0))p=0
}
for(i=1i<=ni++)
for(j=1j<=nj++)
a[i][j]=0
j=n/2+1
a[1][j]=1
for(k=2k<=n*nk++)
{i=i-1
j=j+1
if((i<1)&&(j>n))
{i=i+2
j=j-1
}
else
{if(i<1)i=n
if(j>n)j=1
}
if(a[i][j]==0)a[i][j]=k
else
{i=i+2
j=j-1
a[i][j]=k
}
}
for(i=1i<=ni++)
{for(j=1j<=nj++)
printf("%3d",a[i][j])
printf("\n")
}
}
6.8
#define N 10
#define M 10
main()
{int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj
scanf("%d,%d",&n,&m)
for(i=0i<ni++)
for(j=0j<mj++)
scanf("%d",&a[i][j])
flag2=0
for(i=0i<ni++)
{max=a[i][0]
for(j=0j<mj++)
if(max<a[i][j])
{max=a[i][j]
maxj=j
}
for(k=0,flag1=1k<n&&flag1k++)
if(max>a[k][maxj])flag1=0
if(flag1)
{ printf("\na[%d][%d]=%d\n",i,maxj,max)
flag2=1
}
}
if(!flag2) printf("NOT")
}
6.9
#include<stdio.h>
#define N 15
main()
{int i,j,number,top,bott,min,loca,a[N],flag
char c
for(i=0i<=Ni++)
scanf("%d",&a[i])
flag=1
while(flag)
{scanf("%d",&number)
loca=0
top=0
bott=N-1
if((number<a[0])||(number>a[N-1]))
loca=-1
while((loca==0)&&(top<=bott))
{min=(bott+top)/2
if(number==a[min])
{loca=min
printf("number=%d,loca=%d\n",number,loca+1)
}
else if(number<a[min])
bott=min-1
else
top=min+1
}
if(loca==0||loca==-1)
printf("%d not in table\n",number)
printf("continue Y/N or y/n\n")
c=getchar()
if(c=='N'||c=='n')flag=0
}
}
6.10
main()
{int i,j,uppn,lown,dign,span,othn
char text[3][80]
uppn=lown=dign=span=othn=0
for(i=0i<3i++)
{gets(text[i])
for(j=0j<80&&text[i][j]!='\0'j++)
{if(text[i][j]>='A'&&text[i][j]<='Z')
uppn++
else if(text[i][j]>='a'&&text[i][j]<='z')
lown++
else if(text[i][j]>='0'&&text[i][j]<='9')
dign++
else if(text[i][j]==' ')
span++
else
othn++
}
}
for(i=0i<3i++)
printf("%s\n",text[i])
printf("uppn=%d\n",uppn)
printf("lown=%d\n",lown)
printf("dign=%d\n",dign)
printf("span=%d\n",span)
printf("othn=%d\n",othn)
}
6.11
main()
{static char a[5]={'*','*','*','*','*'}
int i,j,k
char space=' '
for(i=0i<=5i++)
{printf("\n")
for(j=1j<=3*ij++)
printf("%1c",space)
for(k=0k<=5k++)
printf("%3c",a[k])
}
}
6.12
#include<stdio.h>
main()
{int i,n
char ch[80],tran[80]
gets(ch)
i=0
while(ch[i]!='\0')
{if((ch[i]>='A')&&(ch[i]<='Z'))
tran[i]=26+64-ch[i]+1+64
else if((ch[i]>='a')&&(ch[i]<='z'))
tran[i]=26+96-ch[i]+1+96
else
tran[i]=ch[i]
i++
}
n=i
for(i=0i<ni++)
putchar(tran[i])
}
6.13
main()
{char s1[80],s2[40]
int i=0,j=0
scanf("%s",s1)
scanf("%s",s2)
while(s1[i]!='\0')i++
while(s2[j]!='\0')s1[i++]=s2[j++]
s1[i]='\0'
printf("s=%s\n",s1)
}
6.14
#include<stdio.h>
main()
{int i,resu
char s1[100],s2[100]
gets(s1)
gets(s2)
i=0
while((s1[i]==s2[i])&&(s1[i]!='\0'))i++
if(s1[i]=='\0'&&s2[i]=='\0')resu=0
else
resu=s1[i]-s2[i]
printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu)
}
6.15
#include"stdio.h"
main()
{char from[80],to[80]
int i
scanf("%s",from)
for(i=0i<=strlen(from)i++)
to[i]=from[i]
printf("%s\n",to)
}
第七章
7.1
hcf(u,v)
int u,v
{int a,b,t,r
if(u>v){t=uu=vv=t}
a=ub=v
while((r=b%a)!=0)
{b=aa=r}
return(a)
}
lcd(u,v,h)
int u,v,h
{return(u*v/h)}
main()
{int u,v,h,l
scanf("%d,%d",&u,&v)
h=hcf(u,v)
printf("H.C.F=%d\n",h)
l=lcd(u,v,h)
printf("L.C.D=%d\n",l)
}
7.2
#include"math.h"
float x1,x2,disc,p,q
greater_than_zero(a,b)
float a,b
{x1=(-b+sqrt(disc))/(2*a)
x2=(-b-sqrt(disc))/(2*a)
}
equal_to_zero(a,b)
flaot a,b
{x1=x2=-b/(2*a)}
smaller_than_zero(a,b)
float a,b
{p=-b/(2*a)
q=sqrt(-disc)/(2*a)
}
main()
{float a,b,c
scanf("%f,%f,%f",&a,&b,&c)
disc=b*b-4*a*c
if(fabs(disc)<=1e-5)
{equal_to_zero(a,b)
printf("x1=%5.2f\tx2=%5.2f\n",x1,x2)
}
else if(disc>0)
{greater_than_zero(a,b)
printf("x1=%5.2f\tx2=%5.2f\n",x1,x2)
}
else
{smaller_than_zero(a,b)
printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q)
}
}
7.3
main()
{int number
scanf("%d",&number)
if(prime(number))
printf("yes")
else
printf("no")
}
int prime(number)
int number
{int flag=1,n
for(n=2n<number/2&&flag==1n++)
if(number%n==0)
flag=0
return(flag)
}
7.4
#define N 3
int array[N][N]
convert(array)
int array[3][3]
{int i,j,t
for(i=0i<Ni++)
for(j=i+1j<Nj++)
{t=array[i][j]
array[i][j]=array[j][i]
array[j][i]=t
}
}
main()
{int i,j
for(i=0i<Ni++)
for(j=0j<Nj++)
scanf("%d",&array[i][j])
convert(array)
for(i=0i<Ni++)
{printf("\n")
for(j=0j<Nj++)
printf("%5d",array[i][j])
}
}
7.5
main()
{char str[100]
scanf("%s",str)
inverse(str)
printf("%s\n",str)
}
inverse(str)
char str[]
{char t
int i,j
for(i=0,j=strlen(str)i<strlen(str)/2i++,j--)
{t=str[i]
str[i]=str[j-1]
str[j-1]=t
}
}
7.6
char concate(str1,str2,str)
char str1[],str2[],str[]
{int i,j
for(i=0str1[i]!='\0'i++)
str[i]=str1[i]
for(j=0str2[j]!='\0'j++)
str[i+j]=str2[j]
str[i+j]='\0'
}
main()
{char s1[100],s2[100],s[100]
scanf("%s",s1)
scanf("%s",s2)
concate(s1,s2,s)
printf("\ns=%s",s)
}
7.7
main()
{char str[80],c[80]
void cpy()
gets(str)
cpy(str,c)
printf("\n%s\n",c)
}
void cpy(s,c)
char s[],c[]
{int i,j
for(i=0,j=0s[i]!='\0'i++)
if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||
s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')
{c[j]=s[i]j++}
c[j]='\0'
}
7.8
main()
{char str[80]
scanf("%s",str)
insert(str)
}
insert(str)
char str[]
{int i
for(i=strlen(str)i>0i--)
{str[i*2]=str[i]
str[i*2-1]=' '
}
printf("%s\n",str)
}
7.9
int alph,digit,space,others
main()
{char text[80]
gets(text)
alph=0,digit=0,space=0,others=0
count(text)
printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others)
}
count(str)
char str[]
{int i
for(i=0str[i]!='\0'i++)
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
alph++
else if(str[i]>='0'&&str[i]<='9')
digit++
else if(strcmp(str[i],' ')==0)
space++
else
others++
}
7.10
int alph(c)
char c
{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return(1)
else
return(0)
}
int longest(string)
char string[]
{int len=0,i,length=0,flag=1,place,point
for(i=0i<=strlen(string)i++)
if(alph(string[i]))
if(flag)
{point=i
flag=0
}
else
len++
else
{flag=1
if(len>length)
{length=len
place=point
len=0
}
}
return(place)
}
main()
{int i
char line[100]
gets(line)
for(i=longest(line)alph(line[i])i++)
printf("%c",line[i])
printf("\n")
}
7.11
#define N 10
char str[N]
main()
{int i,flag
for(flag=1flag==1)
{scanf("%
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)