1.5請(qǐng)參照本章例題,編寫一個(gè)C程序,輸出以下信息:
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),吳堡網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:吳堡等地區(qū)。吳堡做網(wǎng)站價(jià)格咨詢:028-86922220
************
Very Goodj!
************
解:
main()
{
printf(" ************ \n");
printf("\n");
printf(" Very Good! \n");
printf("\n");
printf(" ************\n");
}
1.6編寫一個(gè)程序,輸入a b c三個(gè)值,輸出其中最大者。
解:main()
{int a,b,c,max;
printf("請(qǐng)輸入三個(gè)數(shù)a,b,c:\n");
scanf("%d,%d,%d",a,b,c);
max=a;
if(maxb)
max=b;
if(maxc)
max=c;
printf("最大數(shù)為:%d",max);
}
第三章
3.3 請(qǐng)將下面各數(shù)用八進(jìn)制數(shù)和十六進(jìn)制數(shù)表示:
(1)10 (2)32 (3)75 (4)-617
(5)-111 (6)2483 (7)-28654 (8)21003
解:十 八 十六
(10)=(12)=(a)
(32)=(40)=20
(75)=(113)=4b
(-617)=(176627)=fd97
-111=177621=ff91
2483=4663=963
-28654=110022=9012
21003=51013=520b
3.5字符常量與字符串常量有什么區(qū)別?
解:字符常量是一個(gè)字符,用單引號(hào)括起來。字符串常量是由0個(gè)或若干個(gè)字符
而成,用雙引號(hào)把它們括起來,存儲(chǔ)時(shí)自動(dòng)在字符串最后加一個(gè)結(jié)束符號(hào)'\0'.
3.6寫出以下程序的運(yùn)行結(jié)果:
#includestdio.h
void main()
{
char c1='a',c2='b',c3='c',c4='\101',c5='\116';
printf("a%c b%c\tc%c\tabc\n",c1,c2,c3);
printf("\t\b%c %c\n",c4,c5);
解:程序的運(yùn)行結(jié)果為:
aabb cc abc
A N
3.7將"China"譯成密碼.密碼規(guī)律:用原來的字母后面第4個(gè)字母代替原來的字母,
例如,字母"A"后面第4個(gè)字母是"E",用"E"代替"A".因此,"China"應(yīng)譯為"Glmre".
請(qǐng)編一程序,用賦初值的議程使c1,c2,c3,c4,c5分別變成'G','1','m','r','e',并
輸出.
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);
}
3.8例3.6能否改成如下:
#includestdio.h
void main()
{
int c1,c2;(原為 char c1,c2)
c1=97;
c2=98;
printf("%c%c\n",c1,c2);
printf("%d%d\n",c1,c2);
}
解:可以.因?yàn)樵诳奢敵龅淖址秶鷥?nèi),用整型和字符型作用相同.
3.9求下面算術(shù)表達(dá)式的值.
(1)x+a%3*(int)(x+y)%2/4=2.5(x=2.5,a=7,y=4.7)
(2)(float)(a+b)/2+(int)x%(int)y=3.5(設(shè)a=2,b=3,x=3.5,y=2.5)
3.10寫出下面程序的運(yùn)行結(jié)果:
#includestdio.h
void main()
{
int i,j,m,n;
i=8;
j=10;
m=++i;
n=j++;
printf("%d,%d,%d,%d\n",i,j,m,n);
}
解:結(jié)果: 9,11,9,10
第4章
4.4.a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1='a',c2='b'.想得
到以下的輸出格式和結(jié)果,請(qǐng)寫出程序要求輸出的結(jié)果如下:
a= 3 b= 4 c= 5
x=1.200000,y=2.400000,z=-3.600000
x+y= 3.60 y+z=-1.20 z+x=-2.40
u= 51274 n= 128765
c1='a' or 97(ASCII)
c2='B' or 98(ASCII)
解:
main()
{
int a,b,c;
long int u,n;
float x,y,z;
char c1,c2;
a=3;b=4;c=5;
x=1.2;y=2.4;z=-3.6;
u=51274;n=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,c2);
printf("c2='%c' or %d(ASCII)\n",c2,c2);
}
4.5請(qǐng)寫出下面程序的輸出結(jié)果.
結(jié)果:
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,17777,ffff,-1
COMPUTER, COM
4.6用下面的scanf函數(shù)輸入數(shù)據(jù),使a=3,b=7,x=8.5,y=71.82,c1='A',c2='a',
問在鍵盤上如何輸入?
main()
{
int a,b;
float x,y;
char c1,c2;
scanf("a=%d b=%d,a,b);
scanf(" x=%f y=%e",x,y);
scanf(" c1=%c c2=%c",c1,c2);
}
解:可按如下方式在鍵盤上輸入:
a=3 b=7
x=8.5 y=71.82
c1=A c2=a
說明:在邊疆使用一個(gè)或多個(gè)scnaf函數(shù)時(shí),第一個(gè)輸入行末尾輸入的"回車"被第二
個(gè)scanf函數(shù)吸收,因此在第二\三個(gè)scanf函數(shù)的雙引號(hào)后設(shè)一個(gè)空格以抵消上行
入的"回車".如果沒有這個(gè)空格,按上面輸入數(shù)據(jù)會(huì)出錯(cuò),讀者目前對(duì)此只留有一
初步概念即可,以后再進(jìn)一步深入理解.
4.7用下面的scanf函數(shù)輸入數(shù)據(jù)使a=10,b=20,c1='A',c2='a',x=1.5,y=-3.75,z=57.8,請(qǐng)問
在鍵盤上如何輸入數(shù)據(jù)?
scanf("%5d%5d%c%c%f%f%*f %f",a,b,c1,c2,y,z);
解:
main()
{
int a,b;
float x,y,z;
char c1,c2;
scanf("%5d%5d%c%c%f%f",a,b,c1,c2,x,y,z);
}
運(yùn)行時(shí)輸入:
10 20Aa1.5 -3.75 +1.5,67.8
注解:按%5d格式的要求輸入a與b時(shí),要先鍵入三個(gè)空格,而后再打入10與20。%*f是用來禁止賦值的。在輸入時(shí),對(duì)應(yīng)于%*f的地方,隨意打入了一個(gè)數(shù)1.5,該值不會(huì)賦給任何變量。
3.8設(shè)圓半徑r=1.5,圓柱高h(yuǎn)=3,求圓周長,圓面積,圓球表面積,圓球體積,圓柱體積,用scanf輸入數(shù)據(jù),輸出計(jì)算結(jié)果,輸出時(shí)要求有文字說明,取小數(shù)點(diǎn)后兩位數(shù)字.請(qǐng)編程.
解:main()
{
float pi,h,r,l,s,sq,vq,vz;
pi=3.1415926;
printf("請(qǐng)輸入圓半徑r圓柱高h(yuǎn):\n");
scanf("%f,%f",r,h);
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
vq=4.0/3.0*pi*r*r*r;
vz=pi*r*r*h;
printf("圓周長為: =%6.2f\n",l);
printf("圓面積為: =%6.2f\n",s);
printf("圓球表面積為: =%6.2f\n",sq);
printf("圓球體積為: =%6.2f\n",vz);
}
4.9輸入一個(gè)華氏溫度,要求輸出攝氏溫度,公式為C=5/9(F-32),輸出要有文字說明,取兩位小數(shù).
解: main()
{
float c,f;
printf("請(qǐng)輸入一個(gè)華氏溫度:\n");
scanf("%f",f);
c=(5.0/9.0)*(f-32);
printf("攝氏溫度為:%5.2f\n",c);
}
第五章 邏輯運(yùn)算和判斷選取結(jié)構(gòu)
5.4有三個(gè)整數(shù)a,b,c,由鍵盤輸入,輸出其中最大的數(shù).
main()
{
int a,b,c;
printf("請(qǐng)輸入三個(gè)數(shù):");
scanf("%d,%d,%d",a,b,c);
if(ab)
if(bc)
printf("max=%d\n",c);
else
printf("max=%d\n",b);
else if(ac)
printf("max=%d\n",c);
else
printf("max-%d\n",a);
}
方法2:使用條件表達(dá)式.
main()
{int a,b,c,termp,max;
printf(" 請(qǐng)輸入 A,B,C: ");
scanf("%d,%d,%d",a,b,c);
printf("A=%d,B=%d,C=%d\n",a,b,c);
temp=(ab)?a:b;
max=(tempc)? temp:c;
printf(" A,B,C中最大數(shù)是%d,",max);
}
5.5 main()
{int x,y;
printf("輸入x:");
scanf("%d",x);
if(x1)
{y=x;
printf("X-%d,Y=X=%d \n",x,y);
}
else if(x10)
{y=2*x-1;
printf(" X=%d, Y=2*X-1=%d\n",x,y);
}
else
{y=3*x-11;
printf("X=5d, Y=3*x-11=%d \n",x,y);
}
}
5.7給一個(gè)不多于5位的正整數(shù),要求:1.求它是幾位數(shù)2.分別打印出每一位數(shù)字3.按逆序打印出各位數(shù)字.例如原數(shù)為321,應(yīng)輸出123.
main()
{
long int num;
int indiv,ten,hundred,housand,tenthousand,place;
printf("請(qǐng)輸入一個(gè)整數(shù)(0-99999):");
scanf("%ld",num);
if(num9999)
place=5;
else if(num999)
place=4;
else if(num99)
place=3;
else if(num9)
place=2;
else place=1;
printf("place=%d\n",place);
printf("每位數(shù)字為:");
ten_thousand=num/10000;
thousand=(num-tenthousand*10000)/1000;
hundred=(num-tenthousand*10000-thousand*1000)/100;
ten=(num-tenthousand*10000-thousand*1000-hundred*100)/10;
indiv=num-tenthousand*10000-thousand*1000-hundred*100-ten*10;
switch(place)
{case 5:printf("%d,%d,%d,%d,%d",tenthousand,thousand,hundred,ten,indiv);
printf("\n反序數(shù)字為:");
printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,tenthousand);
break;
case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
printf("\n反序數(shù)字為:");
printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
break;
case 3:printf("%d,%d,%d\n",hundred,ten,indiv);
printf("\n反序數(shù)字為:");
printf("%d%d%d\n",indiv,ten,hundred);
case 2:printf("%d,%d\n",ten,indiv);
printf("\n反序數(shù)字為:");
printf("%d%d\n",indiv,ten);
case 1:printf("%d\n",indiv);
printf("\n反序數(shù)字為:");
printf("%d\n",indiv);
}
}
5.8 1.if語句
main()
{long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
/*初始化變量*/
bon1=100000*0.1;
bon2=100000*0.075+bon1;
bon4=200000*0.05+bon2;
bon6=200000*0.03+bon4;
bon10=400000*0.015+bon6;
printf("請(qǐng)輸入利潤");
scanf("%ld",i);
/*計(jì)算*/
if(i=le5)
bonus=i*0.1;
else if(i2e5)
bonus=bon1+(1-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=le6)
bonus=bon6+(i-600000)*0.015;
else
bonus=bon10+(i-1000000)*0.01;
printf("獎(jiǎng)金是 %10.2f",bonus);
}
用switch語句編程序
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;
printf("請(qǐng)輸入利潤:");
scanf("%ld",i);
branch=i/100000;
if(branch10)
branch=10;
/*計(jì)算*/
switch(branch)
{case 0:bonus=i*0.1;break;
case 1:bonus=bon1+(i-100000)*0.075;break;
case 2:
case 3:bonus=bon2+(i-200000)*0.05;break;
case 4:
case 5:bonus=bon4+(i-400000)*0.03;break;
case 6:
case 7:
case 8:
case 9:bonus=bon6+(i-600000)*0.015;break;
case 10:bonus=bon10+(i-1000000)*0.01;
}
printf(" 獎(jiǎng)金是 %10.2f",bonus);
}
5.9 輸入四個(gè)整數(shù),按大小順序輸出.
main()
{int t,a,b,c,d;
printf("請(qǐng)輸入四個(gè)數(shù):");
scanf("%d,%d,%d,%d",a,b,c,d);
printf("\n\n a=%d,b=%d,c=%d,d=%d \n",a,b,c,d);
if(ab)
{t=a;a=b;b=t;}
if(ac)
{t=a;a=c;c=t;}
if(ad)
{t=a;a=d;d=t;}
if(bc)
{t=b;b=c;c=t;}
if(bd)
{t=b;b=d;d=t;}
if(cd)
{t=c;c=d;d=t;}
printf("\n 排序結(jié)果如下: \n");
printf(" %d %d %d %d \n",a,b,c,d);
}
5.10塔
main()
{
int h=10;
float x,y,x0=2,y0=2,d1,d2,d3,d4;
printf("請(qǐng)輸入一個(gè)點(diǎn)(x,y):");
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(d11 d21 d31 d41)
h=0;
printf("該點(diǎn)高度為%d",h);
}
第六章 循環(huán)語句
6.1輸入兩個(gè)正數(shù),求最大公約數(shù)最小公倍數(shù).
main()
{
int a,b,num1,num2,temp;
printf("請(qǐng)輸入兩個(gè)正整數(shù):\n");
scanf("%d,%d",num1,num2);
if(num1num2)
{
temp=num1;
num1=num2;
num2=temp;
}
a=num1,b=num2;
while(b!=0)
{
temp=a%b;
a=b;
b=temp;
}
printf("它們的最大公約數(shù)為:%d\n",a);
printf("它們的最小公倍數(shù)為:%d\n",num1*num2/2);
}
6.2輸入一行字符,分別統(tǒng)計(jì)出其中英文字母,空格,數(shù)字和其它字符的個(gè)數(shù).
解:
#include stdio.h
main()
{
char c;
int letters=0,space=0,degit=0,other=0;
printf("請(qǐng)輸入一行字符:\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("其中:字母數(shù)=%d 空格數(shù)=%d 數(shù)字?jǐn)?shù)=%d 其它字符數(shù)=%d\n",letters,space,
digit,other);
}
6.3求s(n)=a+aa+aaa+…+aa…a之值,其中工是一個(gè)數(shù)字.
解:
main()
{
int a,n,count=1,sn=0,tn=0;
printf("請(qǐng)輸入a和n的值:\n");
scanf("%d,%d",a,n);
printf("a=%d n=%d \n",a,n);
while(count=n)
{
tn=tn+a;
sn=sn+tn;
a=a*10;
++count;
}
printf("a+aa+aaa+…=%d\n",sn);
}
6.4 求1+2!+3!+4!+…+20!.
main()
{
float n,s=0,t=1;
for(n=1;n=20;n++)
{
t=t*n;
s=s+t;
}
printf("1!+2!+…+20!=%e\n",s);
}
6.5 main()
{
int N1=100,N2=50,N3=10;
float k;
float s1=0,s2=0,s3=0;
for(k=1;k=N1;k++)/*計(jì)算1到100的和*/
{
s1=s1+k;
}
for(k=1;k=N2;k++)/*計(jì)算1到50各數(shù)平方和*/
{
s2=s2+k*k;
}
for(k=1;k=N3;k++)
{
s3=s3+1/k;
}
printf("總和=%8.2f\n",s1+s2+s3);
}
6.6水仙開花
main()
{
int i,j,k,n;
printf(" '水仙花'數(shù)是:");
for(n=100;n1000;n++)
{
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("%d",n);
}
}
printf("\n");
}
6.7完數(shù)
main()
#include M 1000/*定義尋找范圍*/
main()
{
int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;
int i,j,n,s;
for(j=2;j=M;j++)
{
n=0;
s=j;
for(i=1;ij;i++)
{
if((j%i)==0)
{
if((j%i)==0)
{
n++;
s=s-i;
switch(n)/*將每個(gè)因子賦給k0,k1…k9*/
{
case 1:
k0=i;
break;
case 2:
k1=i;
break;
case 3:
k2=i;
break;
case 4:
k3=i;
break;
case 5:
k4=i;
break;
case 6:
k5=i;
break;
case 7:
k6=i;
break;
case 8:
k7=i;
break;
case 9:
k8=i;
break;
case 10:
k9=i;
break;
}
}
}
if(s==0)
{
printf("%d是一個(gè)'完數(shù)',它的因子是",j);
if(n1)
printf("%d,%d",k0,k1);
if(n2)
printf(",%d",k2);
if(n3)
printf(",%d",k3);
if(n4)
printf(",%d",k4);
if(n5)
printf(",%d",k5);
if(n6)
printf(",%d",k6);
if(n7)
printf(",%d",k7);
if(n8)
printf(",%d",k8);
if(n9)
printf(",%d",k9);
printf("\n");
}
}
方法二:此題用數(shù)組方法更為簡(jiǎn)單.
main()
{
static int k[10];
int i,j,n,s;
for(j=2;j=1000;j++)
{
n=-1;
s=j;
for(i=1;ij;i++)
{
if((j%i)==0)
{
n++;
s=s-i;
k[n]=i;/*將每個(gè)因子賦給k0,k1...k9*/
}
}
if(s==0)
{
printf("%d是一個(gè)完數(shù),它的因子是:",j);
for(i=0;in;i++)
printf("%d,",k[i]);
printf("%d\n",k[n]);
}
}
5.8 有一個(gè)分?jǐn)?shù)序列:2/1,3/2,5/3,8/5……求出這個(gè)數(shù)列的前20項(xiàng)之和.
解: main()
{
int n,t,number=20;
float a=2,b=1,s=0;
for(n=1;n=number;n++)
{
s=s+a/b;
t=a,a=a+b,b=t;
}
printf("總和=%9.6f\n",s);
}
6.9球反彈問題
main()
{
float sn=100.0,hn=sn/2;
int n;
for(n=2;n=10;n++)
{
sn=sn+2*hn;/*第n次落地時(shí)共經(jīng)過的米數(shù)*/
hn=hn/2;/*第n次反跳高度*/
}
printf("第10次落地時(shí)共經(jīng)過%f米 \n",sn);
printf("第10次反彈%f米.\n",hn);
}
6.10猴子吃桃
main()
{
int day,x1,x2;
day=9;
x2=1;
while(day0)
{
x1=(x2+1)*2;
x2=x1;
day--;
}
printf("桃子總數(shù)=%d\n",x1);
}
6.14打印圖案
main()
{
int i,j,k;
for(i=0;i=3;i++)
{
for(j=0;j=2-1;j++)
printf(" ");
for(k=0;k=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i=2;i++)
{
for(j=0;j=i;j++)
printf(" ");
for(k=0;k=4-2*i;k++)
printf("*");
printf("\n");
}
6.15乒乓比賽
main()
{
char i,j,k;/*i是a是對(duì)手;j是b是對(duì)手;k是c的對(duì)手*/
for(i='x';i='z';i++)
for(j='x';j='z';j++)
{
if(i!=j)
for(k='x';k='z';k++)
{
if(i!=kj!=k)
{if(i!='x' k!='x' k! ='z')
printf("順序?yàn)?\na-%c\tb--%c\tc--%c\n",i,j,k);
}
}
}
}
7.1用篩選法求100之內(nèi)的素?cái)?shù).
/*用篩選法求100之內(nèi)的素?cái)?shù)*/
#includemath.h
#define N 101
main()
{
int i,j,line,a[N];
for(i=2;iN;i++) a[i]=i;
for(i=2;isqrl(N);i++)
for(j=i+1;jN;j++)
{
if(a[i]!=0 a[j]!=0)
if(a[j]%a[i]==0)
a[j]=0;
printf("\n");
for(i=2,line=0;iN;i++)
{
if(a[i]!=0)
{
printf("%5d",a[i]);
line++;
if(line==10)
{
printf("\n");
line=0;}
}
}
7.2用選擇法對(duì)10個(gè)數(shù)排序.
/*選擇法排序.*/
#define N 10
main()
{ int i,j,min,temp,a[N];
/*輸入數(shù)據(jù)*/
printf("請(qǐng)輸入十個(gè)數(shù):\n");
for (i=0;iN;i++)
{ printf("a[%d]=",i);
scanf("%d",a[i]);
}
printf("\n");
for(i=0;iN;i++)
printf("%5d",a[i]);
printf("\n");
/*排序*/
for (i=0;iN-1;i++)
{ min=i;
for(j=i+1;jN;j++)
if(a[min]a[j]) min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
/*輸出*/
printf("\n排序結(jié)果如下:\n");
for(i=0;iN;i++)
printf("%5d",a[i]);
}
7.3對(duì)角線和:
/*計(jì)算矩陣對(duì)角線元素之和*/
main()
{
float a[3][3],sum=0;
int i,j;
printf("請(qǐng)輸入矩陣元素:\n");
for(i=0;i3;i++)
for(j=0;j3;j++)
scanf("%f",a[i][j]);
for(i=0;i3;i++)
sum=sum+a[i][i];
printf("對(duì)角元素之和=6.2f",sum);
}
7.4插入數(shù)據(jù)到數(shù)組
/*插入數(shù)據(jù)到數(shù)組*/
main()
{int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("初始數(shù)組如下:");
for (i=0;i10;i++)
printf("%5d",a[i]);
printf("\n");
printf("輸入插入數(shù)據(jù):");
scanf("%d",number);
end=a[9];
if(numberend)
a[10]=number;
else
{for(i=0;i10;i++)
{ if(a[i]number)
{temp1=a[i];
a[i]=number;
for(j=i+1;j11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;j11;i++)
printf("a%6d",a[i]);
}
7.5將一個(gè)數(shù)組逆序存放。
/*數(shù)組逆序存放*/
#define N 5
main()
{ int a[N]={8,6,5,4,1},i,temp;
printf("\n 初始數(shù)組:\n");
for(i=0;iN;i++)
printf("%4d",a[i]);
for(i=0;iN/2;i++)
{ temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
printf("\n 交換后的數(shù)組:\n");
for(i=0;iN;i++)
printf("%4d",a[i]);
}
7.6楊輝三角
/*打印楊輝三角形*/
#define N 11
main()
{ int i,j,a[N][N];
for(i=1;iN;i++)
{a[i][i]=1;
a[i][1]=1;
}
for(i=3;iN;i++)
for(j=2;j=i-1;j++)
a[i][j]=a[i01][j-1]+a[i-1][j];
for(i=1;iN;i++)
{ for(j=1;j=i;j++)
printf("%6d",a[i][j];
printf("\n");
}
printf("\n");
}
7.8鞍點(diǎn)
/*查找鞍點(diǎn)*/
#define N 10
#define M 10
main()
{ int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj;
printf("\n輸入行數(shù)n:");
scanf("%d",n);
printf("\n輸入列數(shù)m:");
scanf("%d",m);
for(i=0;in;i++)
{ printf("第%d行?\n",i);
for(j=0;jm,j++);
scanf("%d",a[i][j];
}
for(i=0;in;i++)
{ for(j=0;jm;j++)
printf("%5d",a[i][j]);
pritf("\n");
}
flag2=0;
for(i=0;in;i++)
{ max=a[i][0];
for(j=0;jm;j++)
if(a[i][j]max)
{ max=a[i][j];
maxj=j;
}
for (k=0,flag1=1;kn flag1;k++)
if(maxa[k][max])
flag1=0;
if(flag1)
{ printf("\n第%d行,第%d列的%d是鞍點(diǎn)\n",i,maxj,max);
flag2=1;
}
}
if(!flag2)
printf("\n 矩陣中無鞍點(diǎn)! \n");
}
7.9變量說明:top,bott:查找區(qū)間兩端點(diǎn)的下標(biāo);loca:查找成功與否的開關(guān)變量.
/*折半查找*/
#includestdio.h
#define N 15
main()
{ int i,j,number,top,bott,min,loca,a[N],flag;
char c;
printf("輸入15個(gè)數(shù)(a[i][i-1])\n);
scanf("%d",a[0]);
i=1;
while(iN)
{ scanf("%d",a[i]);
if(a[i]=a[i-1])
i++;
esle
{printf("請(qǐng)重輸入a[i]");
printf("必須大于%d\n",a[i-1]);
}
}
printf("\n");
for(i=0;iN;i++)
printf("%4d",a[i]);
printf("\n");
flag=1;
while(flag)
{
printf("請(qǐng)輸入查找數(shù)據(jù):");
scanf("%d",number);
loca=0;
top=0;
bott=N-1;
if((numbera[0])||(numbera[N-1]))
loca=-1;
while((loca==0)(top=bott))
{ min=(bott+top)/2;
if(number==a[min])
{ loca=min;
printf("%d位于表中第%d個(gè)數(shù)\n",number,loca+1);
}
else if(numbera[min])
bott=min-1;
else
top=min+1;
}
if(loca==0||loca==-1)
printf("%d不在表中\(zhòng)n",number);
printf("是否繼續(xù)查找?Y/N!\n");
c=getchar();
if(c=='N'||c=='n')
flag=0;
}
}
7.10/*統(tǒng)計(jì)字符數(shù)*/
main()
{ int i,j,uppn,lown,dign,span,othn;
char text[3][80];
uppn=lown=dign=span=othn=0;
for(i=0;i3;i++)
{ printf("\n請(qǐng)輸入第%d行:\n",i);
gets(text[i]);
for(j=0;j80 text[i][j]!='\0';j++)
{if(text[i][j]='A' text[i][j]='Z')
uppn+=1;
else if(text[i][j]='a' text[i][j]='z')
lown+=1;
else if(text[i][j]='1' text[i][j]='9')
dign+=1;
else if(text[i][j]=' ')
span+=1;
else
othn+=1;
}
}
for(i=0;i3;i++)
printf("%s=n",text[i]);
printf("大寫字母數(shù):%d\n",uppn);
printf("小寫字母數(shù):%d\n",lown);
printf("數(shù)字個(gè)數(shù):%d\n",dign);
printf("空格個(gè)數(shù):%d\n",span);
printf("其它字符:%d\n",othn);
}
7.11/*打印圖案*/
main()
{static char a[5]={'*','*','*','*','*'};
int i,j,k;
char space=' ';
for(i=0;i=5;i++)
{printf("\n");
for(j=1;j=3*i;j++)
printf("%lc",space);
for(k=0;k=5;k++)
printf("%3c",a[k];
}
}
7.12/*譯電文*/
#includestdio.h
main()
{int i,n;
char ch[80],tran[80];
printf("請(qǐng)輸入字符:");
gets(ch);
printf("\n密碼是%c",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;
printf("\n原文是:");
for(i=0;in;i++)
putchar(tran[i]);
}
7.13/*連接兩個(gè)字符串(不用'stract')*/
main()
{
char s1[80],s2[40];
int i=0,j=0;
printf("\n請(qǐng)輸入字符串1:");
scanf("%s",s1);
printf("\n請(qǐng)輸入字符串2:");
scanf("%s",s2);
while(s1[i]!='\0')
i++;
while(s2[j]!='\0')
s1[i++]=s2[j++];
s1[i]='\0';
printf("\n連接后字符串為:%s",s1);
}
7.14/*字符串比較*/
#includestdio.h
main()
{int i,resu;
char s1[100],s2[100];
printf("請(qǐng)輸入字符串1:\n");
gets(s1);
printf("\n 請(qǐng)輸入字符串2:\n");
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(" %s與%s比較結(jié)果是%d",s1,s2,resu);
}
7.15/*字符串復(fù)制*/
#includestdio.h
main()
{
char from[80],to[80];
int i;
printf("請(qǐng)輸入字符串");
scanf("%s",from);
for(i=0;i=strlen(from);i++)
to[i]=from[i];
printf("復(fù)制字符串為:%s\n",to);
}
第八章 函數(shù)
8.1(最小公倍數(shù)=u*v/最大公約數(shù).)
hcf(u,v)
int u,v;
(int a,b,t,r;
if(uv)
{t=u;u=v;v=t;}
a=u;b=v;
while((r=b%a)!=0)
{b=a;a=r;}
return(a);
}
lcd(u,v,h)
int u,v,h;
{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);
}
{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);
}
8.2求方程根
#includemath.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)
float a,b;
{x1=x2=(-b)/(2*a);}
smaller_than_zero(a,b)
float a,b;
{p=-b/(2*a);
最多就到8.2.。。。。。。
第十三章
13.4從鍵盤輸入一個(gè)字符串,將其中的小寫字母全部轉(zhuǎn)換成大寫字母,然后輸出到一個(gè)磁盤文件“test”中保存。輸入的字符串以“!”結(jié)束。
解:#include stdio.h
main()
{
File *fp;
Char str[100];
Int I=0;
If((fp=fopen(“test”,”w”)==NULL)
{printf(“Can not open the file\n”);
exit(0);
}
printf(“Input a string:\n”);
gets(str);
while(str[i]!=’!’)
{if (str[i]=’a’str[i]=’z’)
str[i]=str[I-32];
fputc(str[i],fp);
I++;
}
fclose(fp);
fp=fopen(“test”,”r”);
fgets(str,strlen(str)+1,fp);
printf(“%s\n”,str);
fclose(fp);
}
13.5有兩個(gè)磁盤文件”A”和”B”,各存放一行字母,要求把這兩個(gè)文件中的信息合并(按字母順序排列),輸出到一個(gè)新文件”C”中。
解:#includestdio.h
main()
{
FILE *fp;
Int I,j,n,i1;
Char c[100],t ,ch;
If((fp=fopen(“a1”,”r”))==NULL)
{printf(“can not open the file\n”);
exit(0);
}
printf(“\n file A:\n”);
for(I=0;(ch=fgetc(fp)!=EOF;I++)
{c[i]=ch;
putchar(c[i]);
}
fclose(fp);
i1=I;
if((fp=fopen(“b1”,”r”))==NULL)
{printf(“\n can not ipen the file”);
exit(0);
}
printf(“\n file B:\n”);
for(I=i1;(ch=fgenc(fp))!=EOF;I++)
{c[i]=ch;
putchar(c[i]);
}
fclose(fp);
n=I;
for(i=0;In;I++)
for(j=I+1;jn;j++)
if(c[i]c[j])
{t=c[i];
c[i]=c[j];
c[j]=t;
printf(“\n file C:\n”);
fp=fopen(“c1”,”w”);
for(I=0;In;I++)
{putc(c[i],fp);
putchar(c[i]);
}
fclose(fp);
}
13.6有5個(gè)學(xué)生,每個(gè)學(xué)生有3門課的成績(jī),從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號(hào)、姓名、三門課成績(jī)),計(jì)算出平均成績(jī),將原有數(shù)據(jù)和計(jì)算出的平均分?jǐn)?shù)存放在磁盤文件stud中。
解:
#includestdio.h
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}stu[5];
main()
{int I,j,sum;
FILE *fp;
For(I=0;I5;I++)
{printf(“\n input score of student%d:\n”,I+1);
printf(“NO.:”);
scanf(“%s”,stu[i].num);
printf(“name:”);
scanf(“%s”,stu[i].name);
sum=0;
for(j=0;j3;j++)
{printf(“score %d :”j+1);
scanf(“%d”,stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].ave=sum/3.0
}
fp=fopen(“stud”,”w”);
for(I=0;I5;I++)
if(fwrite(stu[i],sizeof(struct student),1,fp)!=1)
printf(“File write error\n”);
fclose(fp);
fp=fopen(“stud”,”r”);
for(I=0;I5;I++)
{fread(stu[i],sizeof(struct student),1,fp);
printf(“%s,%s,%d,%d,%d,%6.2f\n”,stu[i].num,stu[i].name,stu[i].score[0], stu[i].score[1], stu[i].score[2] ,stu[i].ave);
}
}
13.7將上題stud文件中的學(xué)生數(shù)據(jù)按平均分進(jìn)行排序處理,并將已排序的學(xué)生數(shù)據(jù)存入一個(gè)新文件stu-sort中。
解:
#include stdio.h
#define N 10
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}st[N],temp;
main()
{
FILE *fp;
Int I,j,n;
If((fp=fopen(“stud”,”r”))==NULL)
{printf(“can not open the file”);
exit(0);
}
printf(“\n file ‘stud’:”);
for(I=0;fread(st[i],sizef(struct student),1,fp)!=0;I++)
{printf(“\n%8s%8s”,st[i].num,,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.f”,st[i].ave);
fclose(fp);
n=I;
for(I=0;In;I++)
for(j=I+1;jn;j++)
if(st[i].avest[j].ave)
{temp=st[i];
st[i]=st[j];
st[j]=temp;
}
printf(“\nnow:”);
fp=fopen(“stu-sort”,”w”);
for(I=0;In;I++)
{fwrite(st[i],sizeof(struct student),1,fp);
printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
fclose(fp);
}
13.8將上題以排序的學(xué)生成績(jī)文件進(jìn)行插入處理。插入一個(gè)學(xué)生的3門課成績(jī),程序先計(jì)算新插入學(xué)生的平均成績(jī),然后將它按平均成績(jī)高低順序插入,插入后建立一個(gè)新文件。
解:#include stdio.h
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}st[10],s;
main()
{FILE *fp, * fp1 ;
int I,j,t,n;
printf(“\n NO.:”);
scanf(“%s”,s.num);
printf(“name:”);
scanf(“%s”,s.name);
printf(“score1,score2,score3:”);
scanf(“%d,%d,%d”,s. score[0], s. score[1], s. score[2]);
s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;
if((fp=fopen(“stu_sort”,”r”))==NULL)
{printf(“can not open file.”);
exit(0);
}
printf(“original data:\n”);
for(I=0;fread(st[i],sizeof(struct student),1,fp)!=0;I++)
{printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
n=I;
for(t=0;st[t].aves.avetn;t++);
printf(“\nnow:\n”);
fp1=fopen(“sort1.dat”,”w”);
for(I=p;jt;I++)
{fwrite(st[i],sizeof(stuct student),1,fp1);
print(“\n%8s%8s”,st[i],num,st[i].name);
for(j=0;j3;j++)
ptintf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
fwrite(s,sizeof(struct student),1,fp1);
printf(“\n%8s%7s%7d%7d%7d%10.2f”,s.num,s.name,s.score[0],s.score[1],s.score[2],s.ave);
for(I=t;In;I++)
{fwrite(st[i],sizeof(struct student),1,fp1);
printf(“\n %8s%8s”,st[i].num,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“10.2f”,st[i].ave);
fclose(fp);
fclose(fp1);
}
13.9上題結(jié)果仍存入原有的stu_sort文件而不另建立新文件。
解: #includestdio.h
struct student
{char num[10];
char name[8];
int score[3];
float ave;
}st[10],s;
main()
{FILE *fp, *fp1;
int I ,j,t,n;
printf(“\nNO.:”);
scanf(“%s’,s.num);
printf(“name:”);
scanf(“%s’,s.name);
printf(“score1,score2,score3:”);
scanf(“%d%d%d”,s.score[0]+s.score[1]+s.score[1], s.score[2]);
s.ave=( s.score[0]+ s.score[1]+ s.score[2])/3.0;
if((fp=fopen(“stu=sort”,”r”))==NULL)
{printf(“can not open the file.”);
exit(0);
}
printf(“original data:\n”);
for(I=0;fread(st[i],sizeof(struct student),1,fp)!=0;I++)
{printf(“\n%8s%8s”,st[i].num,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
fclose(fp);
n=I;
for(t=0;st[t].aves.ave+tn;t++);
ptintf(“\nnow:\n”);
fp=fopen(“stu_sort”,”w”);
for(I=0;It;I++)
{fwrite(st[i],sizeof(struct student),1,fp);
printf(“\n%9s%8s%8d%8d%8d%10.2f”,s.num,s.name,s.score[0],s.score[1] s.score[2] s.ave);
for(I=t;In;I++)
{fwrit(sr[i],sizeof(struct srudent),1,fp);
printf(“\n %8s%8s”,st[i].num,st[i].name);
for(j=0;j3;j++)
printf(“%8d”,st[i].score[j]);
printf(“%10.2f”,st[i].ave);
}
fclose(fp);
}
13.10 有一磁盤文件emploee,內(nèi)存放職工的數(shù)據(jù)。每個(gè)職工的數(shù)據(jù)包括:職工姓名、職工號(hào)、性別、年齡、住址、工資、健康狀況、文化程度。要求將職工名和工資的信息單獨(dú)抽出來另建一個(gè)簡(jiǎn)明的職工工資文件。
解:#includestdio.h
struct emploee
{char num[6];
char name[10];
char sex[2];
int age;
char addr[20];
int salary;
char health[8];
char class[10];
}en[10];
struct emp
{char name[10];
int salary;
}em-case[10];
main()
{FILE *fp1, *fp2;
int I,j;
if ((fp1=fopen(“emploee”,”r”))==NULL)
{printf(“can not open the file.”);
exit(0);
}
printf(“\n NO. name sex age addr salary health class\n”);
for(I=0;fread(em[i],sizeof(struct emploee),1,fp1)!=p;I++)
{printf(“\n%4s%8s%4s%6s%10s%6s%10s%8s”,em[i].num,em[i].name,em[i].sex, em[i].age, em[i].addr, em[i].salary, em[i].health, em[i].class);
strcpy(em_case[i].name, em[i].name);
em_case[i].salary=en[i].salary;
}
printf(“\n\n*****************************************”);
if((fp2=fopen(“emp_salary”,”wb”))==NULL)
{printf(“can not open the file.”);
exit(0);}
for(j=0;jI;j++)
{if(fwrite(en_case[j],sizeof(struct emp),1,fp2)!=1)
printf(“error!”);
printf(“\n %12s%10d”,em_case[j].name,em_case[j].salary);
}
printf(“\n*******************************************”);
fclose(fp1);
fclose(fp2);
}
13.11從上題的“職工工資文件”中刪區(qū)一個(gè)職工的數(shù)據(jù),再存回原文件。
解:#incude stdio.h
#incude string.h
struct emploee
{char name[10];
int salary;
}emp[20];
main()
{FILE *fp;
int I,j,n,flag;
char name[10];
int salary;
if((fp=fopen(“emp_salary”,”rb”))==NULL)
{printf(“can not open file.”);
exit(0);
}
printf(“\n original data:”);
for(I=0;fead(emp[i],sizeof(struct emploee),1,fp)!=0;I++)
printf(“\n %8s %7d”,emp[i].name,emp[i].salary);
fclose(fp);
n=I;
printf(“\n input name deleted:”);
scanf(“%s”,name);
for(flag=1,I=0;flagIn;I++)
{if(strcmp(name,emp[i].name)==0)
{for(j=I;jn-1;j++)
{strcmp(name,emp[i].name)==0
{for(j=I;jn-1;j++)
{strcpy(emp[j].name,emp[j+1].name);
emp[j].salary=emp[j+1].salary;
}
flag=0;
}
}
if(!flag)
n=n-1;
else
printf(“\n Now,the content of file:\n”);
fp=fopen(“emp-dalary”,”wb”);
for(I=p;In;I++)
fwrite(emp[i],sizeof(struct emploee),1,fp);
fclose(fp);
fp=fopen(“emp_salary”,”r”);
for(I=0;fread(emp[i],sezeof(struct emploee),1,fp)!=0;I++)
printf(“\n%8s%7d”,emp[i].name,emp[i].salary);
fclose(fp);
}
13.12 從鍵盤輸入若干行字符(每行長度不等),輸入后把它們存儲(chǔ)到一磁盤文件中。再從該文件中讀入這些數(shù)據(jù),將其中小寫字母轉(zhuǎn)換成大寫字母后在顯示屏上輸出。
解: #includestdio.h
main()
{int I,flag;
char str[80],c;
FILE *fp;
Fp=fopen(“text”,”w”);
Flag=1;
While(flag==1)
{printf(“\n Input string:\n”);
ges(str);
fprintf(fp,”%s”,str);
printf(“\nContinue?”);
c=getchar();
if((c==’N’)||(c==’n’))
flag=0;
getchar();
}
fcolse()fp;
fp=fopen(“text”,”r”);
while(fscanf(fp,”%s”,str)!=EOF)
{for(I=0;str[i]!=’\0’;I++)
if((str[i]=’a’) (str[i]=’z’))
str[i]-=32;
printf(“\n%s\n”,str);
}
fclose(fp);
}
第六章 循環(huán)語句
6.1輸入兩個(gè)正數(shù),求最大公約數(shù)最小公倍數(shù).
main()
{
int a,b,num1,num2,temp;
printf("請(qǐng)輸入兩個(gè)正整數(shù):\n");
scanf("%d,%d",num1,num2);
if(num1num2)
{
temp=num1;
num1=num2;
num2=temp;
}
a=num1,b=num2;
while(b!=0)
{
temp=a%b;
a=b;
b=temp;
}
printf("它們的最大公約數(shù)為:%d\n",a);
printf("它們的最小公倍數(shù)為:%d\n",num1*num2/2);
}
6.2輸入一行字符,分別統(tǒng)計(jì)出其中英文字母,空格,數(shù)字和其它字符的個(gè)數(shù).
解:
#include stdio.h
main()
{
char c;
int letters=0,space=0,degit=0,other=0;
printf("請(qǐng)輸入一行字符:\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("其中:字母數(shù)=%d 空格數(shù)=%d 數(shù)字?jǐn)?shù)=%d 其它字符數(shù)=%d\n",letters,space,
digit,other);
}
6.3求s(n)=a+aa+aaa+…+aa…a之值,其中工是一個(gè)數(shù)字.
解:
main()
{
int a,n,count=1,sn=0,tn=0;
printf("請(qǐng)輸入a和n的值:\n");
scanf("%d,%d",a,n);
printf("a=%d n=%d \n",a,n);
while(count=n)
{
tn=tn+a;
sn=sn+tn;
a=a*10;
++count;
}
printf("a+aa+aaa+…=%d\n",sn);
}
6.4 求1+2!+3!+4!+…+20!.
main()
{
float n,s=0,t=1;
for(n=1;n=20;n++)
{
t=t*n;
s=s+t;
}
printf("1!+2!+…+20!=%e\n",s);
}
6.5 main()
{
int N1=100,N2=50,N3=10;
float k;
float s1=0,s2=0,s3=0;
for(k=1;k=N1;k++)/*計(jì)算1到100的和*/
{
s1=s1+k;
}
for(k=1;k=N2;k++)/*計(jì)算1到50各數(shù)平方和*/
{
s2=s2+k*k;
}
for(k=1;k=N3;k++)
{
s3=s3+1/k;
}
printf("總和=%8.2f\n",s1+s2+s3);
}
6.6水仙開花
main()
{
int i,j,k,n;
printf(" '水仙花'數(shù)是:");
for(n=100;n1000;n++)
{
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("%d",n);
}
}
printf("\n");
}
6.7完數(shù)
main()
#include M 1000/*定義尋找范圍*/
main()
{
int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;
int i,j,n,s;
for(j=2;j=M;j++)
{
n=0;
s=j;
for(i=1;ij;i++)
{
if((j%i)==0)
{
if((j%i)==0)
{
n++;
s=s-i;
switch(n)/*將每個(gè)因子賦給k0,k1…k9*/
{
case 1:
k0=i;
break;
case 2:
k1=i;
break;
case 3:
k2=i;
break;
case 4:
k3=i;
break;
case 5:
k4=i;
break;
case 6:
k5=i;
break;
case 7:
k6=i;
break;
case 8:
k7=i;
break;
case 9:
k8=i;
break;
case 10:
k9=i;
break;
}
}
}
if(s==0)
{
printf("%d是一個(gè)‘完數(shù)’,它的因子是",j);
if(n1)
printf("%d,%d",k0,k1);
if(n2)
printf(",%d",k2);
if(n3)
printf(",%d",k3);
if(n4)
printf(",%d",k4);
if(n5)
printf(",%d",k5);
if(n6)
printf(",%d",k6);
if(n7)
printf(",%d",k7);
if(n8)
printf(",%d",k8);
if(n9)
printf(",%d",k9);
printf("\n");
}
}
方法二:此題用數(shù)組方法更為簡(jiǎn)單.
main()
{
static int k[10];
int i,j,n,s;
for(j=2;j=1000;j++)
{
n=-1;
s=j;
for(i=1;ij;i++)
{
if((j%i)==0)
{
n++;
s=s-i;
k[n]=i;/*將每個(gè)因子賦給k0,k1...k9*/
}
}
if(s==0)
{
printf("%d是一個(gè)完數(shù),它的因子是:",j);
for(i=0;in;i++)
printf("%d,",k[i]);
printf("%d\n",k[n]);
}
}
5.8 有一個(gè)分?jǐn)?shù)序列:2/1,3/2,5/3,8/5……求出這個(gè)數(shù)列的前20項(xiàng)之和.
解: main()
{
int n,t,number=20;
float a=2,b=1,s=0;
for(n=1;n=number;n++)
{
s=s+a/b;
t=a,a=a+b,b=t;
}
printf("總和=%9.6f\n",s);
}
6.9球反彈問題
main()
{
float sn=100.0,hn=sn/2;
int n;
for(n=2;n=10;n++)
{
sn=sn+2*hn;/*第n次落地時(shí)共經(jīng)過的米數(shù)*/
hn=hn/2;/*第n次反跳高度*/
}
printf("第10次落地時(shí)共經(jīng)過%f米 \n",sn);
printf("第10次反彈%f米.\n",hn);
}
6.10猴子吃桃
main()
{
int day,x1,x2;
day=9;
x2=1;
while(day0)
{
x1=(x2+1)*2;
x2=x1;
day--;
}
printf("桃子總數(shù)=%d\n",x1);
}
6.14打印圖案
main()
{
int i,j,k;
for(i=0;i=3;i++)
{
for(j=0;j=2-1;j++)
printf(" ");
for(k=0;k=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i=2;i++)
{
for(j=0;j=i;j++)
printf(" ");
for(k=0;k=4-2*i;k++)
printf("*");
printf("\n");
}
6.15乒乓比賽
main()
{
char i,j,k;/*i是a是對(duì)手;j是b是對(duì)手;k是c的對(duì)手*/
for(i='x';i='z';i++)
for(j='x';j='z';j++)
{
if(i!=j)
for(k='x';k='z';k++)
{
if(i!=kj!=k)
{if(i!='x' k!='x' k! ='z')
printf("順序?yàn)?\na-%c\tb--%c\tc--%c\n",i,j,k);
}
}
}
}
7.1用篩選法求100之內(nèi)的素?cái)?shù).
/*用篩選法求100之內(nèi)的素?cái)?shù)*/
#includemath.h
#define N 101
main()
{int i,j,line,a[N];
for(i=2;iN;i++) a[i]=i;
for(i=2;isqrl(N);i++)
for(j=i+1;jN;j++)
{if(a[i]!=0 a[j]!=0)
if(a[j]%a[i]==0)
a[j]=0;
printf("\n");
for(i=2,line=0;iN;i++)
{ if(a[i]!=0)
{printf("%5d",a[i]);
line++;
if(line==10)
{printf("\n");
line=0;}
}
}
7.2用選擇法對(duì)10個(gè)數(shù)排序.
/*選擇法排序.*/
#define N 10
main()
{ int i,j,min,temp,a[N];
/*輸入數(shù)據(jù)*/
printf("請(qǐng)輸入十個(gè)數(shù):\n");
for (i=0;iN;i++)
{ printf("a[%d]=",i);
scanf("%d",a[i]);
}
printf("\n");
for(i=0;iN;i++)
printf("%5d",a[i]);
printf("\n");
/*排序*/
for (i=0;iN-1;i++)
{ min=i;
for(j=i+1;jN;j++)
if(a[min]a[j]) min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
/*輸出*/
printf("\n排序結(jié)果如下:\n");
for(i=0;iN;i++)
printf("%5d",a[i]);
}
7.3對(duì)角線和:
/*計(jì)算矩陣對(duì)角線元素之和*/
main()
{
float a[3][3],sum=0;
int i,j;
printf("請(qǐng)輸入矩陣元素:\n");
for(i=0;i3;i++)
for(j=0;j3;j++)
scanf("%f",a[i][j]);
for(i=0;i3;i++)
sum=sum+a[i][i];
printf("對(duì)角元素之和=6.2f",sum);
}
7.4插入數(shù)據(jù)到數(shù)組
/*插入數(shù)據(jù)到數(shù)組*/
main()
{int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("初始數(shù)組如下:");
for (i=0;i10;i++)
printf("%5d",a[i]);
printf("\n");
printf("輸入插入數(shù)據(jù):");
scanf("%d",number);
end=a[9];
if(numberend)
a[10]=number;
else
{for(i=0;i10;i++)
{ if(a[i]number)
{temp1=a[i];
a[i]=number;
for(j=i+1;j11;j++)
{temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;j11;i++)
printf("a%6d",a[i]);
}
7.5將一個(gè)數(shù)組逆序存放。
/*數(shù)組逆序存放*/
#define N 5
main()
{ int a[N]={8,6,5,4,1},i,temp;
printf("\n 初始數(shù)組:\n");
for(i=0;iN;i++)
printf("%4d",a[i]);
for(i=0;iN/2;i++)
{ temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
printf("\n 交換后的數(shù)組:\n");
for(i=0;iN;i++)
printf("%4d",a[i]);
}
7.6楊輝三角
/*打印楊輝三角形*/
#define N 11
main()
{ int i,j,a[N][N];
for(i=1;iN;i++)
{a[i][i]=1;
a[i][1]=1;
}
for(i=3;iN;i++)
for(j=2;j=i-1;j++)
a[i][j]=a[i01][j-1]+a[i-1][j];
for(i=1;iN;i++)
{ for(j=1;j=i;j++)
printf("%6d",a[i][j];
printf("\n");
}
printf("\n");
}
7.8鞍點(diǎn)
/*查找鞍點(diǎn)*/
#define N 10
#define M 10
main()
{ int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj;
printf("\n輸入行數(shù)n:");
scanf("%d",n);
printf("\n輸入列數(shù)m:");
scanf("%d",m);
for(i=0;in;i++)
{ printf("第%d行?\n",i);
for(j=0;jm,j++);
scanf("%d",a[i][j];
}
for(i=0;in;i++)
{ for(j=0;jm;j++)
printf("%5d",a[i][j]);
pritf("\n");
}
flag2=0;
for(i=0;in;i++)
{ max=a[i][0];
for(j=0;jm;j++)
if(a[i][j]max)
{ max=a[i][j];
maxj=j;
}
for (k=0,flag1=1;kn flag1;k++)
if(maxa[k][max])
flag1=0;
if(flag1)
{ printf("\n第%d行,第%d列的%d是鞍點(diǎn)\n",i,maxj,max);
flag2=1;
}
}
if(!flag2)
printf("\n 矩陣中無鞍點(diǎn)! \n");
}
7.9變量說明:top,bott:查找區(qū)間兩端點(diǎn)的下標(biāo);loca:查找成功與否的開關(guān)變量.
/*折半查找*/
#includestdio.h
#define N 15
main()
{ int i,j,number,top,bott,min,loca,a[N],flag;
char c;
printf("輸入15個(gè)數(shù)(a[i][i-1])\n);
scanf("%d",a[0]);
i=1;
while(iN)
{ scanf("%d",a[i]);
if(a[i]=a[i-1])
i++;
esle
{printf("請(qǐng)重輸入a[i]");
printf("必須大于%d\n",a[i-1]);
}
}
printf("\n");
for(i=0;iN;i++)
printf("%4d",a[i]);
printf("\n");
flag=1;
while(flag)
{
printf("請(qǐng)輸入查找數(shù)據(jù):");
scanf("%d",number);
loca=0;
top=0;
bott=N-1;
if((numbera[0])||(numbera[N-1]))
loca=-1;
while((loca==0)(top=bott))
{ min=(bott+top)/2;
if(number==a[min])
{ loca=min;
printf("%d位于表中第%d個(gè)數(shù)\n",number,loca+1);
}
else if(numbera[min])
bott=min-1;
else
top=min+1;
}
if(loca==0||loca==-1)
printf("%d不在表中\(zhòng)n",number);
printf("是否繼續(xù)查找?Y/N!\n");
c=getchar();
if(c=='N'||c=='n')
flag=0;
}
}
7.10/*統(tǒng)計(jì)字符數(shù)*/
main()
{ int i,j,uppn,lown,dign,span,othn;
char text[3][80];
uppn=lown=dign=span=othn=0;
for(i=0;i3;i++)
{ printf("\n請(qǐng)輸入第%d行:\n",i);
gets(text[i]);
for(j=0;j80 text[i][j]!='\0';j++)
{if(text[i][j]='A' text[i][j]='Z')
uppn+=1;
else if(text[i][j]='a' text[i][j]='z')
lown+=1;
else if(text[i][j]='1' text[i][j]='9')
dign+=1;
else if(text[i][j]=' ')
span+=1;
else
othn+=1;
}
}
for(i=0;i3;i++)
printf("%s=n",text[i]);
printf("大寫字母數(shù):%d\n",uppn);
printf("小寫字母數(shù):%d\n",lown);
printf("數(shù)字個(gè)數(shù):%d\n",dign);
printf("空格個(gè)數(shù):%d\n",span);
printf("其它字符:%d\n",othn);
}
7.11/*打印圖案*/
main()
{static char a[5]={'*','*','*','*','*'};
int i,j,k;
char space=' ';
for(i=0;i=5;i++)
{printf("\n");
for(j=1;j=3*i;j++)
printf("%lc",space);
for(k=0;k=5;k++)
printf("%3c",a[k];
}
}
7.12/*譯電文*/
#includestdio.h
main()
{int i,n;
char ch[80],tran[80];
printf("請(qǐng)輸入字符:");
gets(ch);
printf("\n密碼是%c",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;
printf("\n原文是:");
for(i=0;in;i++)
putchar(tran[i]);
}
7.13/*連接兩個(gè)字符串(不用'stract')*/
main()
{
char s1[80],s2[40];
int i=0,j=0;
printf("\n請(qǐng)輸入字符串1:");
scanf("%s",s1);
printf("\n請(qǐng)輸入字符串2:");
scanf("%s",s2);
while(s1[i]!='\0')
i++;
while(s2[j]!='\0')
s1[i++]=s2[j++];
s1[i]='\0';
printf("\n連接后字符串為:%s",s1);
}
7.14/*字符串比較*/
#includestdio.h
main()
{int i,resu;
char s1[100],s2[100];
printf("請(qǐng)輸入字符串1:\n");
gets(s1);
printf("\n 請(qǐng)輸入字符串2:\n");
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(" %s與%s比較結(jié)果是%d",s1,s2,resu);
}
7.15/*字符串復(fù)制*/
#includestdio.h
main()
{
char from[80],to[80];
int i;
printf("請(qǐng)輸入字符串");
scanf("%s",from);
for(i=0;i=strlen(from);i++)
to[i]=from[i];
printf("復(fù)制字符串為:%s\n",to);
}
第八章 函數(shù)
8.1(最小公倍數(shù)=u*v/最大公約數(shù).)
hcf(u,v)
int u,v;
(int a,b,t,r;
if(uv)
{t=u;u=v;v=t;}
a=u;b=v;
while((r=b%a)!=0)
{b=a;a=r;}
return(a);
}
lcd(u,v,h)
int u,v,h;
{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);
}
{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);
}
8.2求方程根
#includemath.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)
float a,b;
{x1=x2=(-b)/(2*a);}
smaller_than_zero(a,b)
float a,b;
{p=-b/(2*a);
標(biāo)題名稱:譚浩強(qiáng)c語言函數(shù)章節(jié)答案 譚浩強(qiáng)c語言答案第五版pdf
標(biāo)題來源:http://jinyejixie.com/article6/dosjdog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、虛擬主機(jī)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)